app\helpers\productsHelper::setToHaystack PHP Method

setToHaystack() public method

Build the not in id array to be evaluated in suggestions.
public setToHaystack ( $products, $field = 'id' ) : [array]
return [array]
    public function setToHaystack($products, $field = 'id')
    {
        if (empty(Session::get('suggest-listed'))) {
            Session::put('suggest-listed', []);
        }
        if (count($products) > 0) {
            foreach ($products as $value) {
                if (!in_array($value[$field], Session::get('suggest-listed'))) {
                    Session::push('suggest-listed', $value[$field]);
                }
            }
        }
        Session::save();
    }

Usage Example

Example #1
0
 /**
  * Show the contents of a wish list
  *
  * @return view for orders.wish
  */
 public function showWishList($id = '')
 {
     /**
      * Checking if there is a product in flashWishList, if so, it will be saved into the basic wish list.
      * This process happens just before the user add a product to wishlist being not logged.
      * flashWishList will be deleted after being used into addToOrder method
      */
     if (Session::get('flashWishList') !== null) {
         $this->addToOrder('wishlist', Session::get('flashWishList.productId'), new Request());
         Session::forget('flashWishList');
         Session::save();
     }
     //saving added to wishlist message (it happens when the product is added to wishlist, and the method call this one)
     Session::forget('suggest-listed');
     if (Session::has('message')) {
         Session::push('message', Session::get('message'));
     }
     $user = \Auth::user();
     $productsHelper = new productsHelper();
     $suggestions = [];
     $hasWishList = true;
     $hasLaterCart = true;
     $wishListName = trans('store.basic_wish_list');
     if ($user) {
         /**
          * it is used to verify whether the order required exist or not.
          * if the order exists, its content is returned, otherwise,
          * the basic wish list is retrieved
          * @var string
          */
         $order = '';
         /**
          * validating if there's a order requested.
          * if it fails, there will be an 404 exception threw
          */
         try {
             $order = Order::findOrFail($id);
         } catch (ModelNotFoundException $e) {
             if (trim($id) != '') {
                 throw new NotFoundHttpException();
             }
         }
         //if the user requires a specific wish list, its details will be provided
         if ($order) {
             $cart = Order::ofType('wishlist')->with('details')->where('user_id', $user->id)->where('id', $order->id)->first();
             /**
              * $wishListName will have the wish list name to be showed in the view
              * @var string
              */
             $wishListName = $cart ? $cart->description : $wishListName;
         } else {
             $cart = Order::ofType('wishlist')->with('details')->where('user_id', $user->id)->first();
         }
         /**
          * listing the user wish lists saved in his account.
          * if there was a specific wish list requiered, it will be excluded from the directory list
          */
         $wishLists = Order::select(['id', 'user_id', 'description'])->ofType('wishlist')->with('details')->where('description', '<>', '')->where('user_id', $user->id)->where('id', '<>', $cart ? $cart->id : '')->take(5)->get();
         //products list saved for later
         $laterCart = Order::ofType('later')->with('details')->where('user_id', $user->id)->first();
         //evaluating wish list
         if ($cart) {
             if ($cart->details && $cart->details->count() > 0) {
                 //saving the ids selected to not include them into suggestions.
                 $productsHelper->setToHaystack($cart->details, 'product_id');
             } else {
                 $hasWishList = false;
             }
         } else {
             $hasWishList = false;
         }
         //evaluating wish list
         if ($laterCart) {
             if ($laterCart->details && $laterCart->details->count() > 0) {
                 //saving the ids selected to not include them into suggestions.
                 $productsHelper->setToHaystack($laterCart->details, 'product_id');
             } else {
                 $hasLaterCart = false;
             }
         } else {
             $hasLaterCart = false;
         }
     } else {
         return redirect()->route('/auth/login');
     }
     $panel = ['center' => ['width' => '12']];
     //suggestions based on cart content
     $suggestions = ProductsController::getSuggestions(['preferences_key' => Session::get('suggest-listed'), 'limit' => 4]);
     Session::forget('suggest-listed');
     return view('orders.wish', compact('cart', 'user', 'panel', 'suggestions', 'cart', 'laterCart', 'wishLists', 'wishListName', 'hasWishList', 'hasLaterCart'));
 }