From 2b0f9d50aae2a32d6a8bcb3738c15d895ce49342 Mon Sep 17 00:00:00 2001 From: prashant-webkul Date: Thu, 20 Sep 2018 20:42:11 +0530 Subject: [PATCH] Cart Refactored More Optimized --- packages/Webkul/Cart/src/Cart.php | 38 ++++++++++++++++--- .../src/Http/Controllers/CartController.php | 17 ++------- packages/Webkul/Cart/src/Models/Cart.php | 2 +- packages/Webkul/Cart/src/Models/CartItem.php | 2 +- .../Cart/src/Repositories/CartRepository.php | 4 ++ public/themes/default/assets/js/shop.js | 20 +++------- 6 files changed, 49 insertions(+), 34 deletions(-) diff --git a/packages/Webkul/Cart/src/Cart.php b/packages/Webkul/Cart/src/Cart.php index aececafae..9849fccae 100644 --- a/packages/Webkul/Cart/src/Cart.php +++ b/packages/Webkul/Cart/src/Cart.php @@ -98,8 +98,15 @@ class Cart { } } + //related record + $related['product_id'] = $id; + + $related['quantity'] = $data['quantity']; + + $related['price'] = $data['price']; + //cart data being attached to the instace. - $cart_data = $this->cart->attach($current_cart_id, $id, $data['quantity'], $data['price']); + $cart_data = $this->cart->createItem($current_cart_id, $related); //getting the products after being attached to cart instance. $cart_products = $this->cart->items($current_cart_id); @@ -156,7 +163,14 @@ class Cart { $cart_product['price'] = $data['price']; - if($cart_product = $this->cart->attach($new_cart_id, $cart_product['product_id'], $cart_product['quantity'], $cart_product['price'])) { + //related item + $related['product_id'] = $cart_product['product_id']; + + $related['quantity'] = $cart_product['quantity']; + + $related['price'] = $cart_product['price']; + + if($cart_product = $this->cart->createItem($new_cart_id, $related)) { session()->put('cart_data', [$cart, $cart_product]); @@ -303,9 +317,16 @@ class Cart { return redirect()->back(); } } - //add the product in the cart - $this->cart->attach($customer_cart_id, $id, $itemdata['quantity'], $itemdata['price']); + //related item + $related['product_id'] = $id; + + $related['quantity'] = $itemdata['quantity']; + + $related['price'] = $itemdata['price']; + + //add the product in the cart + $this->cart->createItem($customer_cart_id, $related); session()->flash('success', 'Item Added To Cart Successfully'); @@ -331,7 +352,14 @@ class Cart { if($new_cart = $this->cart->create($data)) { $new_cart_id = $new_cart->id ?? $new_cart['id']; - $this->cart->attach($new_cart_id, $id, $itemdata['quantity'], $itemdata['price']); + //related item + $related['product_id'] = $id; + + $related['quantity'] = $itemdata['quantity']; + + $related['price'] = $itemdata['price']; + + $this->cart->createItem($new_cart_id, $related); session()->flash('success', 'Item Added To Cart Successfully'); diff --git a/packages/Webkul/Cart/src/Http/Controllers/CartController.php b/packages/Webkul/Cart/src/Http/Controllers/CartController.php index 4dce35386..cff3ac7ba 100644 --- a/packages/Webkul/Cart/src/Http/Controllers/CartController.php +++ b/packages/Webkul/Cart/src/Http/Controllers/CartController.php @@ -111,18 +111,9 @@ class CartController extends Controller * @return Array */ public function test() { - $cartItems = $this->cart->items(75); - - $products = array(); - foreach($cartItems as $cartItem) { - $cartItemId = $cartItem->id; - - $this->cart->updateItem(75, $cartItemId, 'quantity', $cartItem->quantity+1); - - array_push($products, ['product_id' => $this->cartItem->getProduct($cartItemId), 'quantity' => $cartItem->quantity]); - } - - dd($products); - return response()->json($products, 200); + $data['product_id'] = 60; + $data['quantity'] = 7; + $data['price'] = 1600.00; + dd($this->cart->createItem(80, $data)); } } \ No newline at end of file diff --git a/packages/Webkul/Cart/src/Models/Cart.php b/packages/Webkul/Cart/src/Models/Cart.php index 519507600..eb1d92950 100644 --- a/packages/Webkul/Cart/src/Models/Cart.php +++ b/packages/Webkul/Cart/src/Models/Cart.php @@ -11,7 +11,7 @@ class Cart extends Model { protected $table = 'cart'; - protected $fillable = ['customer_id', 'session_id', 'channel_id', 'coupon_code', 'is_gift']; + protected $fillable = ['customer_id', 'session_id', 'channel_id', 'coupon_code', 'is_gift', 'global_currency_code', 'base_currency_code', 'store_currency_code', 'quote_currency_code', 'grand_total', 'base_grand_total', 'sub_total', 'base_sub_total', 'sub_total_with_discount', 'base_sub_total_with_discount', 'checkout_method', 'is_guest', 'customer_full_name', 'conversion_time']; protected $hidden = ['coupon_code']; diff --git a/packages/Webkul/Cart/src/Models/CartItem.php b/packages/Webkul/Cart/src/Models/CartItem.php index d864ae6f8..c54ba0756 100644 --- a/packages/Webkul/Cart/src/Models/CartItem.php +++ b/packages/Webkul/Cart/src/Models/CartItem.php @@ -9,7 +9,7 @@ class CartItem extends Model { protected $table = 'cart_items'; - protected $fillable = ['product_id','quantity','cart_id','tax_category_id','coupon_code']; + protected $fillable = ['product_id','quantity','cart_id','tax_category_id','coupon_code', 'weight', 'price', 'base_price', 'discount_percent', 'discount_amount', 'base_discount_amount', 'no_discount', 'custom_price', 'additional']; public function product() { return $this->hasOne('Webkul\Product\Models\Product', 'id', 'product_id'); diff --git a/packages/Webkul/Cart/src/Repositories/CartRepository.php b/packages/Webkul/Cart/src/Repositories/CartRepository.php index 2ba0cde9f..e2183d828 100644 --- a/packages/Webkul/Cart/src/Repositories/CartRepository.php +++ b/packages/Webkul/Cart/src/Repositories/CartRepository.php @@ -65,7 +65,11 @@ class CartRepository extends Repository */ public function attach($cart_id, $product_id, $quantity, $price) { return $this->model->findOrFail($cart_id)->with_products()->attach($cart_id, ['product_id' => $product_id, 'cart_id' => $cart_id, 'quantity' => $quantity, 'price' => $price]); + } + public function createItem($cartId, $data) { + $cart = $this->model->findOrFail($cartId); + return $cart->items()->create($data); } /** diff --git a/public/themes/default/assets/js/shop.js b/public/themes/default/assets/js/shop.js index 0a239a215..f5c835848 100644 --- a/public/themes/default/assets/js/shop.js +++ b/public/themes/default/assets/js/shop.js @@ -31177,20 +31177,12 @@ var render = function() { var _h = _vm.$createElement var _c = _vm._self._c || _h return _c("li", [ - _c( - "a", - { - attrs: { - href: "bagisto/public/categories/" + this.item["translations"][0].slug - } - }, - [ - _vm._v(_vm._s(this.item["translations"][0].name) + " "), - _vm.haveChildren && _vm.item.parent_id != null - ? _c("i", { staticClass: "icon dropdown-right-icon" }) - : _vm._e() - ] - ), + _c("a", { attrs: { href: this.item["translations"][0].slug } }, [ + _vm._v(_vm._s(this.item["translations"][0].name) + " "), + _vm.haveChildren && _vm.item.parent_id != null + ? _c("i", { staticClass: "icon dropdown-right-icon" }) + : _vm._e() + ]), _vm._v(" "), _vm.haveChildren ? _c(