| <?php |
| <?php |
| |
| |
| namespace App\Jobs; |
| namespace App\Jobs; |
| |
| |
| use Illuminate\Bus\Queueable; |
| use Illuminate\Bus\Queueable; |
| use Illuminate\Queue\SerializesModels; |
| use Illuminate\Queue\SerializesModels; |
| use Illuminate\Queue\InteractsWithQueue; |
| use Illuminate\Queue\InteractsWithQueue; |
| use Illuminate\Contracts\Queue\ShouldQueue; |
| use Illuminate\Contracts\Queue\ShouldQueue; |
| use Illuminate\Foundation\Bus\Dispatchable; |
| use Illuminate\Foundation\Bus\Dispatchable; |
| use Config; |
| use Config; |
| use OhMyBrew\BasicShopifyAPI; |
| use OhMyBrew\BasicShopifyAPI; |
| use DB; |
| use DB; |
| use Log; |
| use Log; |
| |
| |
| |
| |
| class TagUserJob implements ShouldQueue |
| class TagUserJob implements ShouldQueue |
| { |
| { |
| use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; |
| use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; |
| |
| |
| |
| |
| public $recommendation; |
| public $recommendation; |
| public $tagQuestion; |
| public $tagQuestion; |
| public $sessionID; |
| public $sessionID; |
| public $shopify_api_version; |
| public $shopify_api_version; |
| /** |
| /** |
| * Create a new job instance. |
| * Create a new job instance. |
| * |
| * |
| * @return void |
| * @return void |
| */ |
| */ |
| public function __construct($recommendation, $tagQuestion, $sessionID) |
| public function __construct($recommendation, $tagQuestion, $sessionID) |
| { |
| { |
| $this->recommendation = $recommendation; |
| $this->recommendation = $recommendation; |
| $this->tagQuestion = $tagQuestion; |
| $this->tagQuestion = $tagQuestion; |
| $this->sessionID = $sessionID; |
| $this->sessionID = $sessionID; |
| $this->shopify_api_version = env('SHOPIFY_API_VERSION', true); |
| $this->shopify_api_version = env('SHOPIFY_API_VERSION', true); |
| } |
| } |
| |
| |
| /** |
| /** |
| * Execute the job. |
| * Execute the job. |
| * |
| * |
| * @return void |
| * @return void |
| */ |
| */ |
| public function handle() |
| public function handle() |
| { |
| { |
| |
| |
| $recommendation = json_decode($this->recommendation); |
| $recommendation = json_decode($this->recommendation); |
| if (isset($recommendation->Result)) { |
| if (isset($recommendation->Result)) { |
| $recommendation = $recommendation->Result; |
| $recommendation = $recommendation->Result; |
| } |
| } |
| |
| |
| $newTags = $this->getRecommendationTags($recommendation); |
| $newTags = $this->getRecommendationTags($recommendation); |
| |
| |
| $api = new BasicShopifyAPI(); |
| $api = new BasicShopifyAPI(); |
| |
| |
| $password = Config::get('shopify.password'); |
| $password = Config::get('shopify.password'); |
| $shopDomain = Config::get('shopify.shopDomain'); |
| $shopDomain = Config::get('shopify.shopDomain'); |
| |
| |
| $api->setShop($shopDomain); |
| $api->setShop($shopDomain); |
| $api->setAccessToken($password); |
| $api->setAccessToken($password); |
| |
| |
| $quizUser = DB::table('quiz_user')->where('session_id', '=', $this->sessionID)->get(); |
| $quizUser = DB::table('quiz_user')->where('session_id', '=', $this->sessionID)->get(); |
| |
| |
| if(isset($quizUser[0]->user_id)) |
| if(isset($quizUser[0]->user_id)) |
| { |
| { |
| $userId = $quizUser[0]->user_id; |
| $userId = $quizUser[0]->user_id; |
| |
| |
| $userEmail = DB::table('users')->where('id', '=', $userId)->get(); |
| $userEmail = DB::table('users')->where('id', '=', $userId)->get(); |
| $emailId = ($userEmail[0]->user_email); |
| $emailId = ($userEmail[0]->user_email); |
| |
| |
| $params = NULL; |
| $params = NULL; |
| |
| |
| $result = $api->rest('GET', |
| $result = $api->rest('GET', |
| htmlentities('/admin/api/'.$this->shopify_api_version.'/customers/search.json?query=email:'.$emailId), $params); |
| htmlentities('/admin/api/'.$this->shopify_api_version.'/customers/search.json?query=email:'.$emailId), $params); |
| |
| |
| if(!empty($result->body) && isset($result->body->customers[0])) |
| if(!empty($result->body) && isset($result->body->customers[0])) |
| { |
| { |
| $customerId = $result->body->customers[0]->id; |
| $customerId = $result->body->customers[0]->id; |
| $tags = $result->body->customers[0]->tags; |
| $tags = $result->body->customers[0]->tags; |
| |
| |
| $newTags = $this->setNewtags($tags, $newTags); |
| $newTags = $this->setNewtags($tags, $newTags); |
| |
| |
| $params = array( |
| $params = array( |
| 'customer' => array( |
| 'customer' => array( |
| 'id' => $customerId, |
| 'id' => $customerId, |
| 'tags' => $newTags |
| 'tags' => $newTags |
| ) |
| ) |
| ); |
| ); |
| |
| |
| $try = 1; |
| $try = 1; |
| $retry = false; |
| $retry = false; |
| do{ |
| do{ |
| $result = $api->rest('PUT', '/admin/api/'.$this->shopify_api_version.'/customers/'.$customerId.'.json', $params); |
| $result = $api->rest('PUT', '/admin/api/'.$this->shopify_api_version.'/customers/'.$customerId.'.json', $params); |
| if(!isset($result->body->customer->id)){ |
| if(!isset($result->body->customer->id)){ |
| $retry = true; |
| $retry = true; |
| $try++; |
| $try++; |
| } |
| } |
| } while($retry && $try <=3); |
| } while($retry && $try <=3); |
| } |
| } |
| |
| |
| } |
| } |
| } |
| } |
| |
| |
| function getRecommendationTags($recommendation){ |
| function getRecommendationTags($recommendation){ |
| |
| |
| $allergens = $recommendation->Allergens; |
| $allergens = $recommendation->Allergens; |
| $deficiencies = $recommendation->Deficiencies; |
| $deficiencies = $recommendation->Deficiencies; |
| $recDetails = $recommendation->Recommendation; |
| $recDetails = $recommendation->Recommendation; |
| $sessionID = $this->sessionID; |
| $sessionID = $this->sessionID; |
| |
| |
| $tag = ''; |
| $tag = ''; |
| $allergensTag = ''; |
| $allergensTag = ''; |
| $deficiencyTag = ''; |
| $deficiencyTag = ''; |
| $recommendationTag = ''; |
| $recommendationTag = ''; |
| |
| |
| foreach($allergens as $key => $data) |
| foreach($allergens as $key => $data) |
| { |
| { |
| if($data == true) |
| if($data == true) |
| { |
| { |
| $allergensTag.= "myAllergens:".$key.","; |
| $allergensTag.= "myAllergens:".$key.","; |
| } |
| } |
| } |
| } |
| |
| |
| foreach($deficiencies as $key => $data) |
| foreach($deficiencies as $key => $data) |
| { |
| { |
. | if($data == false) |
| if($data == true) |
| { |
| { |
| $deficiencyTag.= "myDeficiencies:".$key.","; |
| $deficiencyTag.= "myDeficiencies:".$key.","; |
| } |
| } |
| } |
| } |
| |
| |
| foreach($recDetails as $value) |
| foreach($recDetails as $value) |
| { |
| { |
| $name = $value->Name; |
| $name = $value->Name; |
| if($name == 'Hair, Skin & Nails'){ |
| if($name == 'Hair, Skin & Nails'){ |
| $name = 'Hair|Skin|Nails'; |
| $name = 'Hair|Skin|Nails'; |
| } |
| } |
| $recommendationTag.= " myRecommendations:".$name.","; |
| $recommendationTag.= " myRecommendations:".$name.","; |
| } |
| } |
| |
| |
| $tag .= "Gender:".$recommendation->SexProfile.","; |
| $tag .= "Gender:".$recommendation->SexProfile.","; |
| |
| |
| if($recommendation->North37 == '1'){ |
| if($recommendation->North37 == '1'){ |
| $tag .= 'North37,'; |
| $tag .= 'North37,'; |
| } |
| } |
| |
| |
| if($recommendation->ProstateInterest == '1'){ |
| if($recommendation->ProstateInterest == '1'){ |
| $tag .= 'ProstateInterest,'; |
| $tag .= 'ProstateInterest,'; |
| } |
| } |
| |
| |
| if($recommendation->PrenatalInterest == '1'){ |
| if($recommendation->PrenatalInterest == '1'){ |
| $tag .= 'PrenatalInterest,'; |
| $tag .= 'PrenatalInterest,'; |
| } |
| } |
| |
| |
| if($recommendation->PostnatalInterest == '1'){ |
| if($recommendation->PostnatalInterest == '1'){ |
| $tag .= 'PostnatalInterest,'; |
| $tag .= 'PostnatalInterest,'; |
| } |
| } |
| |
| |
| if($recommendation->HealthyMentrualCycleInterest == '1'){ |
| if($recommendation->HealthyMentrualCycleInterest == '1'){ |
| $tag .= 'HealthyMentrualCycleInterest,'; |
| $tag .= 'HealthyMentrualCycleInterest,'; |
| } |
| } |
| |
| |
| if($recommendation->PerimenopauseInterest == '1'){ |
| if($recommendation->PerimenopauseInterest == '1'){ |
| $tag .= 'PerimenopauseInterest,'; |
| $tag .= 'PerimenopauseInterest,'; |
| } |
| } |
| |
| |
| if($recommendation->MenopauseInterest == '1'){ |
| if($recommendation->MenopauseInterest == '1'){ |
| $tag .= 'MenopauseInterest,'; |
| $tag .= 'MenopauseInterest,'; |
| } |
| } |
| |
| |
| if($recommendation->PlanningPregnancy == '1'){ |
| if($recommendation->PlanningPregnancy == '1'){ |
| $tag .= 'PlanningPregnancy,'; |
| $tag .= 'PlanningPregnancy,'; |
| } |
| } |
| |
| |
| if($recommendation->TakingMedication == '1'){ |
| if($recommendation->TakingMedication == '1'){ |
| $tag .= 'TakingMedication,'; |
| $tag .= 'TakingMedication,'; |
| } |
| } |
| |
| |
| if($recommendation->BloodClottingIssue == '1'){ |
| if($recommendation->BloodClottingIssue == '1'){ |
| $tag .= 'BloodClottingIssue,'; |
| $tag .= 'BloodClottingIssue,'; |
| } |
| } |
| |
| |
| if($recommendation->Pregnant == '1'){ |
| if($recommendation->Pregnant == '1'){ |
| $tag .= 'Pregnant,'; |
| $tag .= 'Pregnant,'; |
| } |
| } |
| |
| |
| if($recommendation->Breastfeeding == '1'){ |
| if($recommendation->Breastfeeding == '1'){ |
| $tag .= 'Breastfeeding,'; |
| $tag .= 'Breastfeeding,'; |
| } |
| } |
| |
| |
| $tag .= $allergensTag; |
| $tag .= $allergensTag; |
| $tag .= $deficiencyTag; |
| $tag .= $deficiencyTag; |
| $tag .= $recommendationTag; |
| $tag .= $recommendationTag; |
| if(isset($this->tagQuestion[14])) |
| if(isset($this->tagQuestion[14])) |
| { |
| { |
| foreach($this->tagQuestion[14] as $data) |
| foreach($this->tagQuestion[14] as $data) |
| { |
| { |
| if($data == 'Hair, Skin or Nails'){ |
| if($data == 'Hair, Skin or Nails'){ |
| $data = 'Hair|Skin|Nails'; |
| $data = 'Hair|Skin|Nails'; |
| } |
| } |
| $tag .= "myHealthInterests:".$data.","; |
| $tag .= "myHealthInterests:".$data.","; |
| } |
| } |
| } |
| } |
| if(isset($this->tagQuestion[32])) |
| if(isset($this->tagQuestion[32])) |
| { |
| { |
| foreach($this->tagQuestion[32] as $data) |
| foreach($this->tagQuestion[32] as $data) |
| { |
| { |
| $tag .= "myInterests-Activities:".$data.","; |
| $tag .= "myInterests-Activities:".$data.","; |
| } |
| } |
| } |
| } |
| $tag .= "QuizCompleted"; |
| $tag .= "QuizCompleted"; |
| |
| |
| return $tag; |
| return $tag; |
| } |
| } |
| |
| |
| function setNewtags($tags, $recommendationTags){ |
| function setNewtags($tags, $recommendationTags){ |
| $tagExploded = explode(',', $tags); |
| $tagExploded = explode(',', $tags); |
| |
| |
| $tagCollection = array('myAllergens', 'myDeficiencies', 'myRecommendations', |
| $tagCollection = array('myAllergens', 'myDeficiencies', 'myRecommendations', |
| 'Gender', 'North37', 'ProstateInterest', 'PrenatalInterest', 'PostnatalInterest', |
| 'Gender', 'North37', 'ProstateInterest', 'PrenatalInterest', 'PostnatalInterest', |
| 'HealthyMentrualCycleInterest', 'PerimenopauseInterest', 'MenopauseInterest', |
| 'HealthyMentrualCycleInterest', 'PerimenopauseInterest', 'MenopauseInterest', |
| 'PlanningPregnancy', 'TakingMedication', 'BloodClottingIssue', 'Pregnant', |
| 'PlanningPregnancy', 'TakingMedication', 'BloodClottingIssue', 'Pregnant', |
| 'Breastfeeding', 'myHealthInterests', 'myInterests-Activities' |
| 'Breastfeeding', 'myHealthInterests', 'myInterests-Activities' |
| ); |
| ); |
| if(!empty($tagExploded)) |
| if(!empty($tagExploded)) |
| { |
| { |
| foreach($tagExploded as $key => $tag) |
| foreach($tagExploded as $key => $tag) |
| { |
| { |
| foreach($tagCollection as $recTag) |
| foreach($tagCollection as $recTag) |
| { |
| { |
| if(strpos(trim($tag), $recTag) !== FALSE) |
| if(strpos(trim($tag), $recTag) !== FALSE) |
| { |
| { |
| unset($tagExploded[$key]); |
| unset($tagExploded[$key]); |
| } |
| } |
| else |
| else |
| { |
| { |
| continue; |
| continue; |
| } |
| } |
| } |
| } |
| } |
| } |
| } |
| } |
| |
| |
| $newTags = implode(",", $tagExploded).",".$recommendationTags; |
| $newTags = implode(",", $tagExploded).",".$recommendationTags; |
| return $newTags; |
| return $newTags; |
| |
| |
| } |
| } |
| } |
| } |
| |
| |