Do not take this pull very reactive

This commit is contained in:
prashant-webkul 2018-09-13 19:10:01 +05:30
parent b22e82b8cf
commit 84a687239d
1 changed files with 22 additions and 17 deletions

View File

@ -51,9 +51,9 @@ class Cart {
$cart_session_id = Cookie::get('cart_session_id'); $cart_session_id = Cookie::get('cart_session_id');
$cart = $this->cart->getOneByField('session_id'. $cart_session_id); $cart = $this->cart->getOneByField('session_id', $cart_session_id);
$cartId = $cart->id ?? $cart['id'] ; $cartId = $cart->id ?? $cart['id'];
$products = $this->getProducts($id); $products = $this->getProducts($id);
@ -67,6 +67,7 @@ class Cart {
if($this->cartProduct->create($id)) { if($this->cartProduct->create($id)) {
session()->flash('Success', 'Product Added To Cart'); session()->flash('Success', 'Product Added To Cart');
return redirect()->back(); return redirect()->back();
} }
return redirect()->back(); return redirect()->back();
@ -287,14 +288,14 @@ class Cart {
*/ */
public function handleMerge() { public function handleMerge() {
// $productsInCookie = unserialize(Cookie::get('session_c'));
$cart_session_id = Cookie::get('cart_session_id'); $cart_session_id = Cookie::get('cart_session_id');
$cart = $this->cart->findOneByField('session_id'); $cart = $this->cart->findOneByField('session_id', $cart_session_id);
$cartId = $cart->id ?? $cart['id']; $cartId = $cart->id ?? $cart['id'];
$currentCartProducts = $this->cart->getProducts($cartId);
$data['customer_id'] = auth()->guard('customer')->user()->id; $data['customer_id'] = auth()->guard('customer')->user()->id;
$data['channel_id'] = core()->getCurrentChannel()->id; $data['channel_id'] = core()->getCurrentChannel()->id;
@ -311,39 +312,43 @@ class Cart {
foreach($customerCartProducts as $previousCartProduct) { foreach($customerCartProducts as $previousCartProduct) {
foreach($productsInCookie as $key => $productInCookie) { foreach($currentCartProducts as $key => $currentCartProduct) {
if($previousCartProduct->id == $productInCookie) { if($previousCartProduct->id == $currentCartProduct) {
unset($productsInCookie[$key]); unset($currentCartProducts[$key]);
} }
} }
} }
} }
/*if the above block executes it will remove duplicates /*
else product in cookies will be stored in the database.*/ if the above block executes it will remove duplicates
else product in cookies will be stored in the database.
*/
$this->cart->findOneByField('session_id', $cart_session_id)->update($data['customer_id']);
foreach($productsInCookie as $key => $cookieProduct) { foreach($currentCartProducts as $key => $currentCartProduct) {
$product['product_id'] = $cookieProduct; $product['product_id'] = $currentCartProduct;
$product['quantity'] = 1; $product['quantity'] = 1; //initialize the qty from the params passed from post
$product['cart_id'] = $customerCartId; $product['cart_id'] = $customerCartId;
$this->cartProduct->create($product); $this->cartProduct->create($product);
} }
//forget that cookie here. //forget the cart cookie that holds the cart session id
Cookie::queue(Cookie::forget('session_c')); Cookie::queue(Cookie::forget('cart_session_id'));
return redirect()->back();
} else { } else {
if($cart = $this->cart->create($data)) { if($cart = $this->cart->create($data)) {
foreach($productsInCookie as $productInCookie) { foreach($currentCartProducts as $currentCartProduct) {
$product['product_id'] = $cookieProduct; $product['product_id'] = $currentCartProduct;
$product['quantity'] = 1; $product['quantity'] = 1;