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] | ||
리턴 | [array] |
/** * [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); } }
/** * Show the contents of the user Cart. * * @return view for orders.cart */ public function showCart() { $user = \Auth::user(); /** * $suggest-listed keeps tracking listed products to control the suggestion view */ Session::forget('suggest-listed'); /** * $totalAmount saves the shopping cart total amount * @var decimal */ $totalAmount = 0; /** * $totalItems saves the shopping cart total items * @var integer */ $totalItems = 0; if ($user) { /** * $cart has all the shopping cart information, which comes from an type of order called "cart" * @var [type] */ $cart = Order::ofType('cart')->where('user_id', $user->id)->with('details')->first(); /** * $laterCart has all the shopping cart (saved for later) information, which comes from an type of order called "later" * @var [type] */ $laterCart = Order::ofType('later')->where('user_id', $user->id)->with('details')->first(); /** * $validation_message keeps the message for those items that has a different stock since they were added to a shopping cart * @var array */ $validation_message = []; if ($cart) { foreach ($cart->details as $detail) { $totalItems += $detail->quantity; $totalAmount += $detail->quantity * $detail->price; if ($detail->quantity > $detail->product->stock) { $detail->quantity = $detail->product->stock; $detail->save(); $validation_message[] = trans('store.cart_view.item_changed_stock1') . ' ' . $detail->product->name . ' ' . trans('store.cart_view.item_changed_stock2'); } //saving the product listed to not show it on suggestion view Session::push('suggest-listed', $detail->product_id); } //saving the changes made to suggest-listed session var Session::save(); } //if there are validation messages to show, they'll be saved in message session var if (count($validation_message) > 0) { Session::push('message', $validation_message); } } else { /** * $session_cart keeps saved all the items added to the shopping cart befor the user ins logged * @var [array] */ $session_cart = Session::get('user.cart'); if (is_array($session_cart)) { $session_details = Session::get('user.cart_content'); $cart_details = []; $validation_message = []; foreach ($session_details as $id => $quantity) { $product = Product::find($id); $totalAmount += $product->price; if ($quantity > $product->stock) { $quantity = $product->stock; $validation_message[] = trans('store.cart_view.item_changed_stock1') . ' ' . $product->name . ' ' . trans('store.cart_view.item_changed_stock2'); } $cart_details[] = ['id' => 0, 'order_id' => 0, 'product_id' => $product->id, 'price' => $product->price, 'quantity' => $quantity, 'product' => ['id' => $product->id, 'name' => $product->name, 'description' => $product->description, 'price' => $product->price, 'stock' => $product->stock, 'type' => $product->type, 'features' => ['images' => [$product->FirstImage]]]]; Session::push('suggest-listed', $product->id); } if (count($validation_message) > 0) { Session::push('message', $validation_message); } $cart = ['id' => 0, 'user_id' => 0, 'details' => $cart_details]; $totalItems = count($cart_details); } else { $cart = ['id' => 0, 'user_id' => 0, 'details' => []]; } $laterCart = []; } $panel = array('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.cart', compact('cart', 'user', 'panel', 'laterCart', 'suggestions', 'totalItems', 'totalAmount')); }
/** * 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; }
/** * [Search products in auto complete fields] * @param Request $request [Request laravel] * @return [type] [json array] */ public function searchAll(Request $request) { $crit = $request->get('crit'); $suggest = $request->get('suggest'); $group = $request->get('group'); $response['products'] = array('results' => null, 'suggestions' => null); $crit = str_replace(' ', '%', trim($crit)); $crit = str_replace('%%', '%', $crit); if ($crit != '') { if ($suggest) { $response['products']['categories'] = Category::select('id', 'name')->search($crit, null, true)->actives()->where('type', 'store')->orderBy('name')->take(3)->get(); } $response['products']['results'] = Product::where(function ($query) use($crit) { $query->where('name', 'like', '%' . $crit . '%')->orWhere('description', 'like', '%' . $crit . '%'); })->select('id', 'name', 'products_group')->actives()->free()->orderBy('rate_val', 'desc'); if ($group) { $response['products']['results']->where(function ($query) use($group) { $query->where('products_group', '<>', $group)->orWhereNull('products_group'); })->where('id', '<>', $group); } $response['products']['results'] = $response['products']['results']->take(5)->get(); $deep = ''; if ($suggest) { $crit = str_replace('%', '', $crit); for ($i = 0; $i < strlen($crit); $i++) { $deep .= ' ' . $crit[$i]; } } if (!$response['products']['results']->count() && strlen($crit) > 2) { $response['products']['results'] = Product::select('id', 'name', 'products_group')->search($deep, null, true)->actives()->free()->orderBy('rate_val', 'desc'); if ($group) { $response['products']['results']->where(function ($query) use($group) { $query->where('products_group', '<>', $group)->orWhereNull('products_group'); })->where('id', '<>', $group); } $response['products']['results'] = $response['products']['results']->take(5)->get(); } if ($suggest) { $response['products']['suggestions'] = ProductsController::getSuggestions(['user_id' => \Auth::id(), 'preferences_key' => 'my_searches', 'limit' => 3, 'select' => ['id', 'name', 'features']]); if (!$response['products']['categories']->count() && strlen($crit) > 2) { $response['products']['categories'] = Category::select('id', 'name')->search($deep, null, true)->actives()->where('type', 'store')->orderBy('name')->take(3)->get(); } } } $response['products']['categories_title'] = trans('globals.suggested_categories'); $response['products']['suggestions_title'] = trans('globals.suggested_products'); $response['products']['results_title'] = trans('globals.searchResults'); if ($request->wantsJson()) { return json_encode($response); } else { if (env('APP_DEBUG', false)) { dd($response); } } }