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 int
*/
$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 = ['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'));
}