From 9cea4133f75907498b9c0a8a6c91151b072f4533 Mon Sep 17 00:00:00 2001 From: prashant-webkul Date: Fri, 14 Sep 2018 18:45:49 +0530 Subject: [PATCH] Case of Cart Merging Left --- app/Providers/AppServiceProvider.php | 2 - config/database.php | 2 +- packages/Webkul/Cart/src/Cart.php | 384 ++++++++++-------- .../src/Http/Controllers/CartController.php | 4 + .../Cart/src/Repositories/CartRepository.php | 23 +- .../Http/Listeners/CustomerEventsHandler.php | 2 +- .../Shop/src/Resources/assets/sass/app.scss | 42 +- public/themes/default/assets/css/shop.css | 46 +-- public/themes/default/assets/js/shop.js | 4 +- .../themes/default/assets/mix-manifest.json | 2 +- 10 files changed, 257 insertions(+), 254 deletions(-) diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 706c00313..64e2c2f45 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -3,7 +3,6 @@ namespace App\Providers; use Illuminate\Support\ServiceProvider; -use Illuminate\Support\Facades\Schema; class AppServiceProvider extends ServiceProvider { @@ -14,7 +13,6 @@ class AppServiceProvider extends ServiceProvider */ public function boot() { - Schema::defaultStringLength(191); } /** diff --git a/config/database.php b/config/database.php index cab5d068f..e486bd20d 100644 --- a/config/database.php +++ b/config/database.php @@ -51,7 +51,7 @@ return [ 'collation' => 'utf8mb4_unicode_ci', 'prefix' => '', 'strict' => true, - 'engine' => null, + 'engine' => 'InnoDB ROW_FORMAT=DYNAMIC', ], 'pgsql' => [ diff --git a/packages/Webkul/Cart/src/Cart.php b/packages/Webkul/Cart/src/Cart.php index 6c2306843..322c571ea 100644 --- a/packages/Webkul/Cart/src/Cart.php +++ b/packages/Webkul/Cart/src/Cart.php @@ -47,43 +47,152 @@ class Cart { $minutes = 10; - if(Cookie::get('current_session_id')) { + if(Cookie::has('cart_session_id')) { $cart_session_id = Cookie::get('cart_session_id'); - $cart = $this->cart->getOneByField('session_id', $cart_session_id); + $current_cart = $this->cart->findOneByField('session_id', $cart_session_id); - $cartId = $cart->id ?? $cart['id']; + // dd('Cookie = ',$cart_session_id, 'Session = ', session()->get('cart_session_id'), 'DB = ', $current_cart->session_id); - $products = $this->getProducts($id); + if(isset($current_cart)) { + $current_cart_id = $current_cart['id'] ?? $current_cart->id; - foreach($products as $key => $value) { - if($value == $id) { - dd('product already in the cart'); - - return redirect()->back(); - } + $current_cart_session_id = $current_cart['session_id'] ?? $current_cart->session_id; + } else { + $this->repairCart($cart_session_id, $id); } - if($this->cartProduct->create($id)) { - session()->flash('Success', 'Product Added To Cart'); + + if((session()->get('cart_session_id') == Cookie::get('cart_session_id')) && ($current_cart_session_id == session()->get('cart_session_id'))) { + $current_cart_products = array(); + + $current_cart_products = $this->cart->getProducts($current_cart_id); + + foreach($current_cart_products as $key => $value) { + + $product_id = $value['id'] ?? $value->id; + + if($product_id == $id) { + session()->flash('error', 'Item Already In Cart'); + + dump('Item Already In Cart'); + + return redirect()->back(); + } + } + + $cart_data = $this->cart->attach($current_cart_id, $id, 1); + + $cart_products = $this->cart->getProducts($current_cart_id); + + session()->put('cart_data', [$current_cart, $cart_products]); + + session()->flash('Success', 'Item Added In Cart'); + + dump($cart_products); + + return redirect()->back(); + + } else { + // throw new \Exception('Error, Many or Few Session discrepancies found.'); + + $this->repairCart($cart_session_id, $id); + } + } else { + $this->createNewCart($id); + } + } + + /*helpers*/ + public function makeCartSession($to_process) { + session()->put('cart_session_id', $to_process); + + return session()->get('cart_session_id'); + } + + /** + * Create New Cart + * Cart, Cookie & + * Session. + * + * @return mixed + */ + + public function createNewCart($id) { + $minutes = 120; + + $fresh_cart_session_id = session()->getId(); + + Cookie::queue('cart_session_id', $fresh_cart_session_id, $minutes); + + $cart_session_id = $this->makeCartSession($fresh_cart_session_id); + + $data['session_id'] = $fresh_cart_session_id; + + $data['channel_id'] = core()->getCurrentChannel()->id; + + if($cart = $this->cart->create($data)) { + + $new_cart_id = $cart->id ?? $cart['id']; + + $cart_product['product_id'] = $id; + + $cart_product['quantity'] = 1; + + $cart_product['cart_id'] = $new_cart_id; + + if($cart_product = $this->cart->attach($new_cart_id, $cart_product['product_id'], $cart_product['quantity'])) { + + session()->put('cart_data', [$cart, $cart_product]); + + session()->flash('success', 'Product Added To Cart'); return redirect()->back(); } - return redirect()->back(); + } + session()->flash('error', 'Some Error Occured'); - } else { - Cookie::queue('cart_session_id', session()->id(), $minutes); + return redirect()->back(); + } - $data['session_id'] = Cookie::get('cart_session_id'); + /** + * Reset Session and + * Cookie values + * and sync database + * if present. + * + * @return mixed + */ + + public function repairCart($cart_session_id, $product_id) { + + if($cart_session_id == session()->get('cart_session_id')) { + $data['session_id'] = $cart_session_id; $data['channel_id'] = core()->getCurrentChannel()->id; - if($this->cart->create($data)) { - if($this->cartProduct->create($product)) { - session()->flash('Success', 'Product Added To Cart'); - } - } + $cart = $this->cart->create($data); + + $cart_id = $cart['id'] ?? $cart->id; + + $this->cart->attach($cart_id, $product_id, 1); + + session()->flash('success', 'Product Added In Cart'); + + return redirect()->back(); + + } else { + + Cookie::queue(Cookie::forget('cart_session_id')); + + session()->forget('cart_session_id'); + + session()->regenerate(); + + session()->flash('error', 'Please Try Adding Product Again.'); + + return redirect()->back(); } } @@ -165,108 +274,6 @@ class Cart { return redirect()->back(); } - // } - - // //case when cookie has products and customer also have data in cart tables - // if(isset($customerCart) && isset($cartCookie)) { - // //for unsetting the cookie for the cart uncomment after all done - // // Cookie::queue(Cookie::forget('session_c')); - - // //check if there is any repetition in the products - // $customerCartId = $customerCart->id ?? $customerCart['id']; - - // //to check if the customer is also having some products saved in the pivot - // //for the products. - // $customerCartProducts = $this->cart->getProducts($customerCartId); - - // //if there are products already in cart of that customer - // if(isset($customerCartProducts)) { - - // foreach($customerCartProducts as $previousCartProduct) { - - // dump($customerCartProducts); - - // foreach($cookieProducts as $key => $cookieProduct) { - - // if($previousCartProduct->id == $cookieProduct) { - - // unset($cookieProducts[$key]); - // } - // } - // } - // } - - // /*if the above block executes it will remove duplicates - // else product in cookies will be stored in the database.*/ - - // foreach($cookieProducts as $key => $cookieProduct) { - - // $product['product_id'] = $cookieProduct; - - // $product['quantity'] = 1; - - // $product['cart_id'] = $customerCartId; - - // $this->cartProduct->create($product); - // } - - // dump('Products in the cart synced.'); - - // return redirect()->back(); - - // } else if(isset($customerCart) && !isset($cartCookie)) { - // //case when there is no data in guest's cart - - // $customerCartId = $customerCart->id ?? $customerCart['id']; - - // $customerCartProducts = $this->cart->getProducts($customerCartId); - - // foreach($customerCartProducts as $previousCartProduct) { - - // if($previousCartProduct->id == $id) { - - // dd('product already in the cart::AUTH'); - - // return redirect()->back(); - // } - // } - // $product['product_id'] = $id; - - // $product['quantity'] = 1; - - // $product['cart_id'] = $customerCartId; - - // $this->cartProduct->create($product); - - // dump('new item added in the cart'); - - // return redirect()->back(); - - // } else if(!isset($customerCart) && isset($cartCookie)) { - - // $products = unserialize(Cookie::get('session_c')); - - // if ($cart = $this->cart->create($data)) { - - // foreach($products as $product) { - - // $product['product_id'] = $id; - - // $product['quantity'] = 1; - - // $product['cart_id'] = $cart; - - // $this->cartProduct->create($product); - // } - // return redirect()->back(); - - // } else { - // session()->flash('error', 'Cannot Add Your Items To Cart'); - - // return redirect()->back(); - // } - // } - // } } /** @@ -286,79 +293,104 @@ class Cart { * with the existing data of cart * in the cart tables; */ - public function handleMerge() { + public function mergeCart() { - $cart_session_id = Cookie::get('cart_session_id'); + if(Cookie::has('cart_session_id')) { - $cart = $this->cart->findOneByField('session_id', $cart_session_id); + $cart_session_id = Cookie::get('cart_session_id'); - $cartId = $cart->id ?? $cart['id']; + $current_cart = $this->cart->findOneByField('session_id', $cart_session_id); - $currentCartProducts = $this->cart->getProducts($cartId); + //it is impossible to not have an entry in cart table and cart_products. + //will later handle the exceoption. + $current_cart_id = $current_cart['id'] ?? $current_cart->id; - $data['customer_id'] = auth()->guard('customer')->user()->id; + $current_cart_session_id = $current_cart['session_id'] ?? $current_cart->session_id; - $data['channel_id'] = core()->getCurrentChannel()->id; + $current_cart_products = $this->cart->getProducts($current_cart_id); - $customerCart = $this->cart->findOneByField('customer_id', $data['customer_id']); + $customer_id = auth()->guard('customer')->user()->id; //working - if(isset($customerCart)) { + if($cart_session_id == $current_cart_session_id) { + $current_cart_products = array(); - $customerCartId = $customerCart->id ?? $customerCart['id']; + $customer_cart = $this->cart->findByField(['customer_id'=> $customer_id]); - $customerCartProducts = $this->cart->getProducts($customerCartId); + //check previous saved cart of customer. + if(!$customer_cart->isEmpty()) { - if(isset($customerCartProducts)) { + $customer_cart_id = $customer_cart->id; - foreach($customerCartProducts as $previousCartProduct) { + $customer_cart_products = $this->cart->getProducts($customer_cart_id); - foreach($currentCartProducts as $key => $currentCartProduct) { + foreach($current_cart_products as $key => $value) { - if($previousCartProduct->id == $currentCartProduct) { + $product_id = $value['id'] ?? $value->id; - unset($currentCartProducts[$key]); + foreach($current_cart_products as $key => $current_cart_product) { + + $current_product_id = $current_cart_product['id'] ?? $current_cart_product->id; + + if($current_product_id == $product_id) { + + unset($current_cart_products[$key]); + } } } + + + foreach($current_cart_products as $current_cart_product) { + + $current_cart_product_id = $current_cart_product['id'] ?? $current_cart_product->id; + + $this->cart->attach($current_cart_id, $current_cart_product_id, 1); + } + + $this->cart->update(['customer_id' => $customer_id], $current_cart_id); + + $customer_cart = $this->cart->findOneByField('customer_id', $customer_id); + + $customer_cart_id = $customer_cart->id; + + if($this->cart->getProducts($customer_cart_id) && isset($current_cart_products)) { + foreach($current_cart_products as $key => $value) { + + array_push($cart_products, $current_cart_product); + } + } + + session()->put('cart_data', [$customer_cart, $cart_products]); + + session()->flash('Success', 'Item Added In Cart'); + + dump($cart_products); + + return redirect()->back(); + } else { + + $session_id = session()->getId(); + + $customer_id = auth()->guard('customer')->user()->id; + + $updated_cart = $this->cart->update(['customer_id' => $customer_id, 'session_id' => $session_id], $current_cart_id); + + $updated_cart_products = $this->cart->getProducts($updated_cart->id); + + Cookie::queue('cart_session_id', $session_id, 120); + + session()->put('cart_session_id', $session_id); + + session('cart_data', [$updated_cart, $updated_cart_products]); + + return redirect()->back(); } + } else { + throw new \Exception('Error, Session discrepancies found.'); + + $this->repairCart($cart_session_id, $id); } - - /* - 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($currentCartProducts as $key => $currentCartProduct) { - - $product['product_id'] = $currentCartProduct; - - $product['quantity'] = 1; //initialize the qty from the params passed from post - - $product['cart_id'] = $customerCartId; - - $this->cartProduct->create($product); - } - - //forget the cart cookie that holds the cart session id - Cookie::queue(Cookie::forget('cart_session_id')); - return redirect()->back(); } else { - - if($cart = $this->cart->create($data)) { - - foreach($currentCartProducts as $currentCartProduct) { - - $product['product_id'] = $currentCartProduct; - - $product['quantity'] = 1; - - $product['cart_id'] = $cart->id; - - $this->cartProduct->create($product); - } - } - //forget the Cookie - Cookie::queue(Cookie::forget('session_c')); + throw new \Exception('Nothing found'); return redirect()->back(); } diff --git a/packages/Webkul/Cart/src/Http/Controllers/CartController.php b/packages/Webkul/Cart/src/Http/Controllers/CartController.php index cbb144943..972719384 100644 --- a/packages/Webkul/Cart/src/Http/Controllers/CartController.php +++ b/packages/Webkul/Cart/src/Http/Controllers/CartController.php @@ -66,6 +66,8 @@ class CartController extends Controller } else { Cart::guestUnitAdd($id); } + + return redirect()->back(); } public function remove($id) { @@ -75,5 +77,7 @@ class CartController extends Controller } else { Cart::guestUnitRemove($id); } + + return redirect()->back(); } } \ No newline at end of file diff --git a/packages/Webkul/Cart/src/Repositories/CartRepository.php b/packages/Webkul/Cart/src/Repositories/CartRepository.php index d75299717..0ba0e4b07 100644 --- a/packages/Webkul/Cart/src/Repositories/CartRepository.php +++ b/packages/Webkul/Cart/src/Repositories/CartRepository.php @@ -61,29 +61,18 @@ class CartRepository extends Repository * Method to attach * associations * - * @return mixed + * @return Eloquent */ - // public function onlyAttach($id, $taxRates) { + public function attach($cart_id, $product_id, $quantity) { - // foreach($taxRates as $key => $value) { - - // $this->model->findOrFail($id)->tax_rates()->attach($id, ['tax_category_id' => $id, 'tax_rate_id' => $value]); - // } - // } + $this->model->findOrFail($cart_id)->with_products()->attach($cart_id, ['product_id' => $product_id, 'cart_id' => $cart_id, 'quantity' => $quantity]); + } /** * Method to detach - * and attach the * associations * - * @return mixed - */ - // public function syncAndDetach($id, $taxRates) { - // $this->model->findOrFail($id)->tax_rates()->detach(); - - // foreach($taxRates as $key => $value) { - // $this->model->findOrFail($id)->tax_rates()->attach($id, ['tax_category_id' => $id, 'tax_rate_id' => $value]); - // } - // } + * @return Eloquent + */ } \ No newline at end of file diff --git a/packages/Webkul/Customer/src/Http/Listeners/CustomerEventsHandler.php b/packages/Webkul/Customer/src/Http/Listeners/CustomerEventsHandler.php index d4b4e26b1..c14b422d2 100644 --- a/packages/Webkul/Customer/src/Http/Listeners/CustomerEventsHandler.php +++ b/packages/Webkul/Customer/src/Http/Listeners/CustomerEventsHandler.php @@ -27,7 +27,7 @@ class CustomerEventsHandler { * check emptiness and then * do the appropriate actions. */ - Cart::handleMerge(); + Cart::mergeCart(); } /** diff --git a/packages/Webkul/Shop/src/Resources/assets/sass/app.scss b/packages/Webkul/Shop/src/Resources/assets/sass/app.scss index 40c9b97e0..575420e13 100644 --- a/packages/Webkul/Shop/src/Resources/assets/sass/app.scss +++ b/packages/Webkul/Shop/src/Resources/assets/sass/app.scss @@ -8,6 +8,8 @@ body { margin: 0; padding: 0; font-weight: 500; + max-width: 100%; + width: auto; color: $font-color; font-size: $font-size-base; } @@ -23,8 +25,8 @@ body { .header-top { margin-bottom: 16px; display: flex; - max-width: 80%; - width: 100%; + max-width: 100%; + width: auto; margin-left: auto; margin-right: auto; align-items: center; @@ -187,7 +189,8 @@ body { display: block; font-size:16px; height: 48px; - width: 80%; + max-width: 100%; + width: auto; margin-left: auto; margin-right: auto; } @@ -610,10 +613,11 @@ body { section.slider-block { display: block; + margin-left: auto; + margin-right: auto; margin-bottom: 5%; div.slider-content { - width: 80%; height: 500px; margin-left: auto; margin-right: auto; @@ -763,54 +767,42 @@ section.slider-block { .main-container-wrapper { - margin-left: 10%; - margin-right: 10%; + max-width: 80%; + width: auto; + margin-left: auto; + margin-right: auto; .content-container { display: inline-block; width: 100%; } - .main { - display: inline-block; - width: 75% - } - .product-grid { display: grid; grid-gap: 30px; &.max-2-col { - grid-template-columns: repeat(2, minmax(250px, 1fr)); + grid-template-columns: repeat(2, minmax(auto, 1fr)); } &.max-3-col { - grid-template-columns: repeat(3, minmax(250px, 1fr)); + grid-template-columns: repeat(3, minmax(auto, 1fr)); } &.max-4-col { - grid-template-columns: repeat(4, minmax(250px, 1fr)); - } - - .product-card { - display: flex; - flex-flow: column; - justify-content: center; + grid-template-columns: repeat(4, minmax(auto, 1fr)); } } .product-card { .product-image { - background: #F2F2F2; margin-bottom: 10px; - // width: 380px; - // max-width: 100%; - // text-align: center; img { align-self: center; - width: 100%; + max-width: 100%; + width: auto; margin-bottom: 14px; } } diff --git a/public/themes/default/assets/css/shop.css b/public/themes/default/assets/css/shop.css index 9e29cbb81..064cd197d 100644 --- a/public/themes/default/assets/css/shop.css +++ b/public/themes/default/assets/css/shop.css @@ -27,6 +27,8 @@ body { margin: 0; padding: 0; font-weight: 500; + max-width: 100%; + width: auto; color: #242424; font-size: 16px; } @@ -45,8 +47,8 @@ body { display: -webkit-box; display: -ms-flexbox; display: flex; - max-width: 80%; - width: 100%; + max-width: 100%; + width: auto; margin-left: auto; margin-right: auto; -webkit-box-align: center; @@ -260,7 +262,8 @@ body { display: block; font-size: 16px; height: 48px; - width: 80%; + max-width: 100%; + width: auto; margin-left: auto; margin-right: auto; } @@ -645,11 +648,12 @@ body { section.slider-block { display: block; + margin-left: auto; + margin-right: auto; margin-bottom: 5%; } section.slider-block div.slider-content { - width: 80%; height: 500px; margin-left: auto; margin-right: auto; @@ -806,8 +810,10 @@ section.slider-block div.slider-content div.slider-control .light-right-icon { } .main-container-wrapper { - margin-left: 10%; - margin-right: 10%; + max-width: 80%; + width: auto; + margin-left: auto; + margin-right: auto; } .main-container-wrapper .content-container { @@ -815,50 +821,32 @@ section.slider-block div.slider-content div.slider-control .light-right-icon { width: 100%; } -.main-container-wrapper .main { - display: inline-block; - width: 75%; -} - .main-container-wrapper .product-grid { display: grid; grid-gap: 30px; } .main-container-wrapper .product-grid.max-2-col { - grid-template-columns: repeat(2, minmax(250px, 1fr)); + grid-template-columns: repeat(2, minmax(auto, 1fr)); } .main-container-wrapper .product-grid.max-3-col { - grid-template-columns: repeat(3, minmax(250px, 1fr)); + grid-template-columns: repeat(3, minmax(auto, 1fr)); } .main-container-wrapper .product-grid.max-4-col { - grid-template-columns: repeat(4, minmax(250px, 1fr)); -} - -.main-container-wrapper .product-grid .product-card { - display: -webkit-box; - display: -ms-flexbox; - display: flex; - -webkit-box-orient: vertical; - -webkit-box-direction: normal; - -ms-flex-flow: column; - flex-flow: column; - -webkit-box-pack: center; - -ms-flex-pack: center; - justify-content: center; + grid-template-columns: repeat(4, minmax(auto, 1fr)); } .main-container-wrapper .product-card .product-image { - background: #F2F2F2; margin-bottom: 10px; } .main-container-wrapper .product-card .product-image img { -ms-flex-item-align: center; align-self: center; - width: 100%; + max-width: 100%; + width: auto; margin-bottom: 14px; } diff --git a/public/themes/default/assets/js/shop.js b/public/themes/default/assets/js/shop.js index aee93be85..92989e592 100644 --- a/public/themes/default/assets/js/shop.js +++ b/public/themes/default/assets/js/shop.js @@ -10648,7 +10648,7 @@ return jQuery; "use strict"; /* WEBPACK VAR INJECTION */(function(global, setImmediate) {/*! - * Vue.js v2.5.17 + * Vue.js v2.5.16 * (c) 2014-2018 Evan You * Released under the MIT License. */ @@ -15737,7 +15737,7 @@ Object.defineProperty(Vue, 'FunctionalRenderContext', { value: FunctionalRenderContext }); -Vue.version = '2.5.17'; +Vue.version = '2.5.16'; /* */ diff --git a/public/themes/default/assets/mix-manifest.json b/public/themes/default/assets/mix-manifest.json index b7e923adb..2141a2aae 100644 --- a/public/themes/default/assets/mix-manifest.json +++ b/public/themes/default/assets/mix-manifest.json @@ -1,4 +1,4 @@ { "/js/shop.js": "/js/shop.js", "/css/shop.css": "/css/shop.css" -} +} \ No newline at end of file