public function addToOrder($destination, $productId, Request $request)
{
$quantity = $request->get('quantity');
//if there is not quantity requested, the value is 1 as defect
if (!$quantity) {
$quantity = 1;
}
//making sure if the product requested exists, otherwise, we throw a http exception
try {
$product = Product::findOrFail($productId);
} catch (ModelNotFoundException $e) {
throw new NotFoundHttpException();
}
$user = \Auth::user();
//checking if the user is logged
if ($user) {
$basicCart = Order::ofType($destination)->where('user_id', $user->id)->first();
if (!$basicCart) {
$basicCart = new Order();
$basicCart->user_id = $user->id;
$basicCart->type = $destination;
$basicCart->status = 'open';
$basicCart->save();
$log = Log::create(['action_type_id' => '1', 'details' => $destination, 'source_id' => $basicCart->id, 'user_id' => $user->id]);
}
//if the request has an email address, we keep it, otherwise we use the user one
if ($request->has('email')) {
$v = Validator::make($request->all(), ['email' => 'required|email']);
if ($v->fails()) {
$email = $user->email;
} else {
$email = $request->input('email');
}
} else {
$email = $user->email;
}
//creating visrtual order
if ($destination != 'wishlist') {
$this->addToCartVirtualsProduct($product, $email, $basicCart->id, $quantity);
}
//checking if the user already has a product so it can be added
$orderDetail = OrderDetail::where('order_id', $basicCart->id)->where('product_id', $product->id)->first();
//creating the order detail
if ($orderDetail) {
$orderDetail->price = $product->price;
$orderDetail->quantity = $orderDetail->quantity + $quantity;
} else {
$orderDetail = new OrderDetail();
$orderDetail->order_id = $basicCart->id;
$orderDetail->product_id = $product->id;
$orderDetail->price = $product->price;
$orderDetail->quantity = $quantity;
$orderDetail->status = 1;
}
//saving detail order
$orderDetail->save();
$log = Log::create(['action_type_id' => '4', 'details' => $basicCart->id, 'source_id' => $orderDetail->id, 'user_id' => $user->id]);
//callback url
if ($destination == 'wishlist') {
Session::push('message', trans('store.productAddedToWishList'));
return redirect()->route('orders.show_wish_list');
} elseif ($destination == 'later') {
Session::push('message', trans('store.productsSavedForLater'));
return redirect()->route('products.show', [$productId]);
} else {
Session::push('message', trans('store.productAdded'));
return redirect()->route('orders.show_cart');
}
} else {
/**
* $user_cart is used to keep track the user shopping cart.
*
* @var [array]
*/
$user_cart = Session::get('user.cart');
/*
* $user_cart is used to keep track the user shopping cart details
* @var [array]
*/
$user_cart_content = Session::get('user.cart_content');
//Checking if the user has a session cart, otherwise, it's created
if (!is_array($user_cart)) {
$user_cart = [];
}
//Checking if the user has content in the shopping cart, otherwise, it's added.
if (!is_array($user_cart_content)) {
$user_cart_content = [];
}
//Checking if the user has the product in the shopping cart, otherwise, it's pushed up
if (!in_array($productId, $user_cart)) {
array_push($user_cart, $productId);
}
//Checking if the user has quantity on the selected product, if so, it's quantity is updated, otherwise, 1 is added as defect.
if (!isset($user_cart_content[$productId])) {
$user_cart_content[$productId] = $quantity;
} else {
$user_cart_content[$productId] += $quantity;
}
//Saving session for user logged
Session::put('user.cart_content', $user_cart_content);
Session::put('user.cart', $user_cart);
Session::save();
Session::push('message', trans('store.productAdded'));
if ($destination == 'wishlist') {
/*
* flashWishList lets you save the product wished after login action.
* This var will be delete automatic in show show wishlist route.
*/
Session::put('flashWishList.productId', $productId);
Session::put('flashWishList.quantity', $quantity);
Session::save();
return redirect()->route('orders.show_wish_list');
} else {
return redirect()->route('orders.show_cart');
}
}
}