From a631a0767427348352d5fa0de787d39e51b4023f Mon Sep 17 00:00:00 2001 From: prashant-webkul Date: Fri, 12 Oct 2018 12:51:39 +0530 Subject: [PATCH] Bug fixes and cart operations optimization --- .gitignore | 1 + packages/Webkul/Checkout/src/Cart.php | 70 +++-- .../Controllers/RegistrationController.php | 4 +- .../src/Http/Controllers/CartController.php | 4 +- .../Shop/src/Resources/assets/sass/app.scss | 30 +- .../Webkul/Shop/src/Resources/lang/en/app.php | 6 +- .../views/customers/signup/index.blade.php | 2 +- .../views/layouts/header/index.blade.php | 13 +- .../views/products/list/card.blade.php | 8 +- ..._30_064755_create_tax_categories_table.php | 0 ...18_08_30_065042_create_tax_rates_table.php | 0 ...08_30_065840_create_tax_mappings_table.php | 0 public/themes/default/assets/css/shop.css | 37 ++- public/themes/default/assets/js/shop.js | 287 +++++++++--------- 14 files changed, 270 insertions(+), 192 deletions(-) rename packages/Webkul/Tax/src/{database/migrations => Database/Migrations}/2018_08_30_064755_create_tax_categories_table.php (100%) rename packages/Webkul/Tax/src/{database/migrations => Database/Migrations}/2018_08_30_065042_create_tax_rates_table.php (100%) rename packages/Webkul/Tax/src/{database/migrations => Database/Migrations}/2018_08_30_065840_create_tax_mappings_table.php (100%) diff --git a/.gitignore b/.gitignore index b8f14e562..033ba29c8 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ /public/css /public/js /public/vendor +/public/themes/* /storage/*.key /vendor /.idea diff --git a/packages/Webkul/Checkout/src/Cart.php b/packages/Webkul/Checkout/src/Cart.php index 6f489bd68..f3397f054 100644 --- a/packages/Webkul/Checkout/src/Cart.php +++ b/packages/Webkul/Checkout/src/Cart.php @@ -94,27 +94,24 @@ class Cart { { $product = $this->product->findOneByField('id', $productId); - unset($data['_token']); + // dd($product); - //Check if the product is saleable + //Check if the product's information is proper or not. if(!isset($data['product']) || !isset($data['quantity'])) { session()->flash('error', trans('shop::app.checkout.cart.integrity.missing_fields')); - return redirect()->back(); + return false; } else { if($product->type == 'configurable' && !isset($data['super_attribute'])) { session()->flash('error', trans('shop::app.checkout.cart.integrity.missing_options')); - return redirect()->back(); + return false; } } $child = $childData = null; if($product->type == 'configurable') { - //Check if the product is salable $child = $this->product->findOneByField('id', $data['selected_configurable_option']); - - //child row data $childData = [ 'product_id' => $data['selected_configurable_option'], 'sku' => $child->sku, @@ -123,8 +120,8 @@ class Cart { ]; } - $price = ($product->type == 'configurable' ? $child->price : $product->price); + $parentData = [ 'sku' => $product->sku, 'product_id' => $productId, @@ -153,14 +150,20 @@ class Cart { */ public function add($id, $data) { + $itemData = $this->prepareItemData($id, $data); + if($itemData == false) { + return false; + } + if($cart = $this->getCart()) { $product = $this->product->find($id); $cartItems = $cart->items; - if($cartItems->count()) { + //check the isset conditions as collection empty object will mislead the condition and errorhandling case. + if(isset($cartItems) && $cartItems->count()) { foreach($cartItems as $cartItem) { if($product->type == "simple") { @@ -173,7 +176,7 @@ class Cart { if($canBe == false) { session()->flash('warning', trans('shop::app.checkout.cart.quantity.inventory_warning')); - return redirect()->back(); + return false; } $cartItem->update([ @@ -186,7 +189,7 @@ class Cart { session()->flash('success', trans('shop::app.checkout.cart.quantity.success')); - return redirect()->back(); + return true; } } else if($product->type == "configurable") { if($cartItem->type == "configurable") { @@ -205,7 +208,7 @@ class Cart { if($canBe == false) { session()->flash('warning', trans('shop::app.checkout.cart.quantity.inventory_warning')); - return redirect()->back(); + return false; } $parent->update([ @@ -218,7 +221,7 @@ class Cart { session()->flash('success', trans('shop::app.checkout.cart.quantity.success')); - return redirect()->back(); + return true; } } } @@ -257,12 +260,17 @@ class Cart { * @param integer $id * @param array $data * - * @return Response + * @return Booleans */ public function createNewCart($id, $data) { $itemData = $this->prepareItemData($id, $data); + //if the item data is not valid to be processed it will be returning false + if($itemData == false) { + return false; + } + $cartData['channel_id'] = core()->getCurrentChannel()->id; //auth user details else they will be set when the customer is guest @@ -276,9 +284,16 @@ class Cart { $cartData['is_guest'] = 1; } + //set the currency column with the respective currency + $cartData['global_currency_code'] = core()->getBaseCurrencyCode(); + $cartData['base_currency_code'] = core()->getBaseCurrencyCode(); + $cartData['channel_currency_code'] = core()->getBaseCurrencyCode(); + $cartData['cart_currency_code'] = core()->getBaseCurrencyCode(); + //set the cart items and quantity $cartData['items_count'] = 1; $cartData['items_qty'] = $data['quantity']; + //create the cart instance in the database if($cart = $this->cart->create($cartData)) { $itemData['parent']['cart_id'] = $cart->id; $product = $this->product->find($id); @@ -332,14 +347,19 @@ class Cart { $guestCart = session()->get('cart'); + //when the logged in customer is not having any of the cart instance previously and are active. if(!isset($cart)) { - $guestCart->update(['customer_id' => auth()->guard('customer')->user()->id, 'is_guest' => 0]); + $guestCart->update([ + 'customer_id' => auth()->guard('customer')->user()->id, + 'is_guest' => 0, + 'customer_first_name' => auth()->guard('customer')->user()->first_name, + 'customer_last_name' => auth()->guard('customer')->user()->last_name, + 'customer_email' => auth()->guard('customer')->user()->email + ]); session()->forget('cart'); - session()->put('cart', $guestCart); - - return redirect()->back(); + return true; } $cartItems = $cart->items; @@ -360,9 +380,8 @@ class Cart { $canBe = $this->canAddOrUpdate($cartItem->id, $prevQty + $newQty); if($canBe == false) { - session()->flash('warning', 'The requested quantity is not available, please try back later.'); - - return redirect()->back(); + $this->cartItem->delete($guestCartItem->id); + continue; } $cartItem->update([ @@ -388,9 +407,8 @@ class Cart { $canBe = $this->canAddOrUpdate($cartItem->child->id, $prevQty + $newQty); if($canBe == false) { - session()->flash('warning', 'The requested quantity is not available, please try back later.'); - - return redirect()->back(); + $this->cartItem->delete($guestCartItem->id); + continue; } $cartItem->update([ @@ -404,9 +422,9 @@ class Cart { $guestCartItems->forget($key); //child will be deleted first - $this->cartItem->delete($guestCartItemChild->id); + // $this->cartItem->delete($guestCartItemChild->id); - //then parent will get deleted + //then parent will also delete the child if any $this->cartItem->delete($guestCartItem->id); } } diff --git a/packages/Webkul/Customer/src/Http/Controllers/RegistrationController.php b/packages/Webkul/Customer/src/Http/Controllers/RegistrationController.php index 656bb73fc..fa9ac1f22 100644 --- a/packages/Webkul/Customer/src/Http/Controllers/RegistrationController.php +++ b/packages/Webkul/Customer/src/Http/Controllers/RegistrationController.php @@ -51,7 +51,7 @@ class RegistrationController extends Controller */ public function create(Request $request) { - + // dd(request()->input()); $request->validate([ 'first_name' => 'string|required', @@ -65,8 +65,6 @@ class RegistrationController extends Controller $data['password'] = bcrypt($data['password']); - // $registrationData = $request->except('_token'); - if ($this->customer->create($data)) { session()->flash('success', 'Account created successfully.'); diff --git a/packages/Webkul/Shop/src/Http/Controllers/CartController.php b/packages/Webkul/Shop/src/Http/Controllers/CartController.php index d168e2252..052d3cc50 100644 --- a/packages/Webkul/Shop/src/Http/Controllers/CartController.php +++ b/packages/Webkul/Shop/src/Http/Controllers/CartController.php @@ -88,7 +88,7 @@ class CartController extends Controller public function add($id) { $data = request()->input(); - + // dd($data); Cart::add($id, $data); return redirect()->back(); @@ -120,7 +120,7 @@ class CartController extends Controller return redirect()->back(); } } - + Cart::update($data); return redirect()->back(); diff --git a/packages/Webkul/Shop/src/Resources/assets/sass/app.scss b/packages/Webkul/Shop/src/Resources/assets/sass/app.scss index 14b93495e..a04ed07d9 100644 --- a/packages/Webkul/Shop/src/Resources/assets/sass/app.scss +++ b/packages/Webkul/Shop/src/Resources/assets/sass/app.scss @@ -279,7 +279,7 @@ section.slider-block { flex-direction: row; justify-content: flex-end; align-items: center; - cursor: pointer; + // cursor: pointer; ul.account-dropdown-container { float: right; @@ -289,6 +289,32 @@ section.slider-block { display: flex; flex-direction: row; margin-right: 14px; + + .dropdown-list { + width: 300px; + padding: 25px; + } + + .dropdown-container { + padding: 0px; + overflow: hidden; + } + + ul.account-dropdown-list { + display: flex; + flex-direction: row; + justify-content: space-between; + align-items: center; + + li > a { + color: white; + } + + .btn { + min-width: 100px; + text-align: center; + } + } } } @@ -334,7 +360,7 @@ section.slider-block { } .dropdown-cart > .dropdown-header i{ - cursor: pointer; + // cursor: pointer; float: right; height: 22px; width: 22px; diff --git a/packages/Webkul/Shop/src/Resources/lang/en/app.php b/packages/Webkul/Shop/src/Resources/lang/en/app.php index 5711f9dc2..c3b84bd97 100644 --- a/packages/Webkul/Shop/src/Resources/lang/en/app.php +++ b/packages/Webkul/Shop/src/Resources/lang/en/app.php @@ -4,7 +4,11 @@ return [ 'home' => [ 'page-title' => 'Bagisto - Home', 'featured-products' => 'Featured Products', - 'new-products' => 'New Products' + 'new-products' => 'New Products', + + 'product-card' => [ + 'add-to-cart' => 'ADD TO CART' + ] ], 'customer' => [ diff --git a/packages/Webkul/Shop/src/Resources/views/customers/signup/index.blade.php b/packages/Webkul/Shop/src/Resources/views/customers/signup/index.blade.php index 63fc6bca8..d05a98d60 100644 --- a/packages/Webkul/Shop/src/Resources/views/customers/signup/index.blade.php +++ b/packages/Webkul/Shop/src/Resources/views/customers/signup/index.blade.php @@ -50,7 +50,7 @@
- + {{ __('shop::app.customer.signup-form.agree') }} {{ __('shop::app.customer.signup-form.terms') }} & {{ __('shop::app.customer.signup-form.conditions') }} {{ __('shop::app.customer.signup-form.using') }}. diff --git a/packages/Webkul/Shop/src/Resources/views/layouts/header/index.blade.php b/packages/Webkul/Shop/src/Resources/views/layouts/header/index.blade.php index 89efa8623..c6e7dd9e1 100644 --- a/packages/Webkul/Shop/src/Resources/views/layouts/header/index.blade.php +++ b/packages/Webkul/Shop/src/Resources/views/layouts/header/index.blade.php @@ -28,14 +28,15 @@
- @guest + @guest('customer') @endguest @auth('customer') -