Cart Refactored More Optimized

This commit is contained in:
prashant-webkul 2018-09-20 20:42:11 +05:30
parent 8f28bd162e
commit 2b0f9d50aa
6 changed files with 49 additions and 34 deletions

View File

@ -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');

View File

@ -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));
}
}

View File

@ -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'];

View File

@ -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');

View File

@ -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);
}
/**

View File

@ -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(