App\Http\Controllers\ProductsController::getSuggestions PHP Method

getSuggestions() public static method

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.
public static getSuggestions ( $data ) : [array]
return [array]
    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;
    }

Usage Example

Example #1
0
 /**
  * [searchAll description]
  * @param  Request $request [description]
  * @return [type]           [description]
  */
 public function searchAll(Request $request)
 {
     $crit = $request->get('crit');
     $response['products'] = array('results' => null, 'suggestions' => null);
     if ($crit != '') {
         $response['products']['results'] = Product::where('status', 1)->search($crit)->Free()->take(5)->get();
     }
     $response['products']['suggestions'] = ProductsController::getSuggestions(['user_id' => \Auth::id(), 'preferences_key' => 'my_searches', 'limit' => 3]);
     if ($request->wantsJson()) {
         return json_encode($response);
     }
 }
All Usage Examples Of App\Http\Controllers\ProductsController::getSuggestions