예제 #1
0
 /**
  * manage the home section suggestions
  * @param  [string] $type, which is the reference point to build the suggest
  * @return [json] $suggest, that contain the products list to be displayed on home page
  */
 public static function suggest($type, $limit = 4)
 {
     $data = [];
     switch ($type) {
         case 'purchased':
             $data['preferences_key'] = 'product_purchased';
             $data['limit'] = $limit;
             break;
         case 'categories':
             $data['preferences_key'] = 'product_categories';
             $data['limit'] = $limit;
             $usr_prefe = UserController::getPreferences('', $data['preferences_key']);
             //look up for user preferences
             if (count($usr_prefe['tags']) == 0) {
                 $data['category'] = ProductsController::getRandCategoryId();
                 //if there is not info, we get a rand category id
             } else {
                 $data['category'] = $usr_prefe['tags'][mt_rand(0, count($usr_prefe['tags']) - 1)];
                 //if so, we get a rand user preferences category
             }
             break;
         case 'viewed':
             $data['preferences_key'] = 'product_viewed';
             $data['limit'] = $limit;
             break;
         case 'carousel':
             return ProductsController::getTopRated(0, $limit, false);
             break;
         default:
             $data['limit'] = $limit;
             $data['preferences_key'] = '';
             break;
     }
     $suggest = ProductsController::getSuggestions($data);
     //suggestion array
     return $suggest;
 }
예제 #2
0
 /**
  * To get the products suggestion, taking in account either the preference key, such as
  * (product_viewed, product_purchased, product_shared, product_categories, my_searches), or all of them.
  *
  * @param [array] $data, which is the suggest configuration
  *
  * @return [array] $products, which will contain all the suggestion for the user either in session or suggested
  */
 public static function getSuggestions($data)
 {
     $options = ['user_id' => '', 'preferences_key' => '', 'limit' => '4', 'category' => '', 'select' => '*'];
     $suggest_listed = Session::get('suggest-listed');
     if (count($suggest_listed)) {
         $suggest_listed = array_unique($suggest_listed);
     } else {
         $suggest_listed = [];
     }
     $data = $data + $options;
     $diff = 0;
     $productsHelper = new ProductsHelper();
     $needle['tags'] = [];
     // the suggestions based on one id (one product)
     if (is_int($data['preferences_key'])) {
         $data['preferences_key'] = [$data['preferences_key']];
     }
     // the suggestions based on a list of products
     if (is_array($data['preferences_key'])) {
         foreach ($data['preferences_key'] as $id) {
             $needleAux = Product::select('tags', 'name')->where('id', $id)->free()->orderBy('rate_count', 'desc')->first()->toArray();
             //extraction of tags and name of products
             $needle['tags'] = array_merge($needle['tags'], explode(',', trim($needleAux['tags'])), explode(' ', trim($needleAux['name'])));
         }
     } else {
         $needle = UserController::getPreferences($data['preferences_key']);
         //getting the user preferences
     }
     if (count($needle['tags']) > 0) {
         //by preferences
         if ($data['preferences_key'] == 'product_categories') {
             //look up by categories. If we want to get a specific category, we have to add "category" to data array
             \DB::enableQueryLog();
             $products[0] = Product::select($data['select'])->free()->whereNotIn('id', $suggest_listed)->inCategories('category_id', $needle['tags'])->orderBy('rate_count', 'desc')->take($data['limit'])->get()->toArray();
         } else {
             //look up by products tags and name
             $products[0] = Product::select($data['select'])->free()->whereNotIn('id', $suggest_listed)->like(['tags', 'name'], $needle['tags'])->orderBy('rate_count', 'desc')->take($data['limit'])->get()->toArray();
         }
     }
     $diff = $data['limit'] - (isset($products[0]) ? count($products[0]) : 0);
     //limit control
     //if we get suggestion results, we save those id
     if (isset($products[0])) {
         $productsHelper->setToHaystack($products[0]);
     }
     //by rate
     if ($diff > 0 && $diff <= $data['limit']) {
         $products[1] = Product::select($data['select'])->where($productsHelper->getFieldToSuggestions($data['preferences_key']), '>', '0')->whereNotIn('id', $suggest_listed)->free()->orderBy($productsHelper->getFieldToSuggestions($data['preferences_key']), 'DESC')->take($diff)->get()->toArray();
         $diff = $diff - count($products[1]);
         //limit control
     }
     //if we get suggestion results, we save those id
     if (isset($products[1])) {
         $productsHelper->setToHaystack($products[1]);
     }
     //by rand
     if ($diff > 0 && $diff <= $data['limit']) {
         $products[2] = Product::select($data['select'])->free()->whereNotIn('id', $suggest_listed)->orderByRaw('RAND()')->take($diff)->get()->toArray();
     }
     //if we get suggestion results, we save those id
     if (isset($products[2])) {
         $productsHelper->setToHaystack($products[2]);
     }
     //making one array to return
     $array = [];
     $products = array_values($products);
     for ($i = 0; $i < count($products); $i++) {
         if (count($products[$i]) > 0) {
             $array = array_merge($array, $products[$i]);
         }
     }
     return $array;
 }