diff --git a/composer.json b/composer.json index cc67bf6ff..ec0706a90 100644 --- a/composer.json +++ b/composer.json @@ -36,6 +36,7 @@ "webkul/laravel-attribute": "self.version", "webkul/laravel-cart": "self.version", "webkul/laravel-customer": "self.version", + "webkul/laravel-inventory": "self.version", "webkul/laravel-category": "self.version", "webkul/laravel-product": "self.version", "webkul/laravel-shop": "self.version", diff --git a/packages/Webkul/Cart/src/Cart.php b/packages/Webkul/Cart/src/Cart.php index aececafae..a91dc12b6 100644 --- a/packages/Webkul/Cart/src/Cart.php +++ b/packages/Webkul/Cart/src/Cart.php @@ -11,6 +11,9 @@ use Webkul\Cart\Repositories\CartItemRepository; //Customer repositories use Webkul\Customer\Repositories\CustomerRepository; +//Product Repository +use Webkul\Product\Repositories\ProductRepository; + use Cookie; /** @@ -33,7 +36,9 @@ class Cart { //Cookie expiry limit in minutes protected $minutes; - public function __construct(CartRepository $cart, CartItemRepository $cartItem, CustomerRepository $customer, $minutes = 150) { + protected $product; + + public function __construct(CartRepository $cart, CartItemRepository $cartItem, CustomerRepository $customer, $minutes = 150, ProductRepository $product) { $this->customer = $customer; @@ -42,89 +47,10 @@ class Cart { $this->cartItem = $cartItem; $this->minutes = $minutes; + + $this->product = $product; } - public function guestUnitAdd($id, $data) { - - //empty array for storing the products. - $products = array(); - - if(Cookie::has('cart_session_id')) { - - //getting the cart session id from cookie. - $cart_session_id = Cookie::get('cart_session_id'); - - //finding current cart instance in the database table. - $current_cart = $this->cart->findOneByField('session_id', $cart_session_id); - - //check there is any cart or not. - if(isset($current_cart)) { - $current_cart_id = $current_cart['id'] ?? $current_cart->id; - - $current_cart_session_id = $current_cart['session_id'] ?? $current_cart->session_id; - } else { - //if someone deleted handle flow to the normal. - $this->repairCart($cart_session_id, $id); - } - - //Matching the session id present in the cookie and database are same or not. - if((session()->get('cart_session_id') == Cookie::get('cart_session_id')) && ($current_cart_session_id == session()->get('cart_session_id'))) { - $current_cart_items = $this->cart->items($current_cart_id); - - $current_cart_products = array(); - - foreach($current_cart_items as $current_cart_item) { - array_push($current_cart_products, $this->cartItem->getProduct($current_cart_item->id)); - } - - //checking new product coming in the cart is new or previously added item. - foreach($current_cart_products as $key => $value) { - $product_id = $value; - - if($product_id == $id) { - //create status code to communicate with session flash. - $cartItemId = $this->cartItem->findOneByField('product_id', $id)->id; - - $cartItemQuantity = $this->cartItem->findOneByField('product_id', $id)->quantity; - - $this->cartItem->update(['quantity' => $cartItemQuantity + $data['quantity']], $cartItemId); - - session()->flash('error', 'Item Already In Cart'); - - //remove this its temporary. - dump('Item Already In Cart'); - - return redirect()->back(); - } - } - - //cart data being attached to the instace. - $cart_data = $this->cart->attach($current_cart_id, $id, $data['quantity'], $data['price']); - - //getting the products after being attached to cart instance. - $cart_products = $this->cart->items($current_cart_id); - - //storing the information in session. - session()->put('cart_data', [$current_cart, $cart_products]); - - session()->flash('Success', 'Item Added To Cart Successfully'); - - //return the control to the controller. - return redirect()->back(); - - } else { - //repair the cart, will remake the session. - //and add the product in the new cart instance. - $this->repairCart($cart_session_id, $id); - } - } else { - //function call - $this->createNewCart($id, $data); - } - } - - /*helpers*/ - /** * Create New Cart * Cart, Cookie & @@ -135,213 +61,76 @@ class Cart { public function createNewCart($id, $data) { - $fresh_cart_session_id = session()->getId(); + $cartData['channel_id'] = core()->getCurrentChannel()->id; - if(!auth()->guard('customer')->check()) - $data['session_id'] = $fresh_cart_session_id; + if(auth()->guard('customer')->check()) { + $data['customer_id'] = auth()->guard('customer')->user()->id; - $data['channel_id'] = core()->getCurrentChannel()->id; + $cartData['customer_full_name'] = auth()->guard('customer')->first_name .' '. auth()->guard('customer')->last_name; + } - if($cart = $this->cart->create($data)) { + if($cart = $this->cart->create($cartData)) { - $this->makeCartSession($fresh_cart_session_id); + $data['product_id'] = $id; - $new_cart_id = $cart->id ?? $cart['id']; - - $cart_product['product_id'] = $id; - - $cart_product['quantity'] = $data['quantity']; - - $cart_product['cart_id'] = $new_cart_id; - - $cart_product['price'] = $data['price']; - - if($cart_product = $this->cart->attach($new_cart_id, $cart_product['product_id'], $cart_product['quantity'], $cart_product['price'])) { - - session()->put('cart_data', [$cart, $cart_product]); + if($result = $cart->items()->create($data)) { + session()->put('cart', $cart); session()->flash('success', 'Item Added To Cart Successfully'); return redirect()->back(); } } - session()->flash('error', 'Some Error Occured'); + session()->flash('error', 'Some error occured'); return redirect()->back(); } - /** - * This makes session - * cart. - */ - public function makeCartSession($cart_session_id) { - - $fresh_cart_session_id = $cart_session_id; - - Cookie::queue('cart_session_id', $fresh_cart_session_id, $this->minutes); - - session()->put('cart_session_id', $fresh_cart_session_id); - } - - - /** - * Reset Session and - * Cookie values - * and sync database - * if present. - * - * @return mixed - */ - public function repairCart($cart_session_id ="null", $product_id = 0) { - - if($cart_session_id == session()->get('cart_session_id')) { - $data['session_id'] = $cart_session_id; - - $data['channel_id'] = core()->getCurrentChannel()->id; - - $cart = $this->cart->create($data); - - $cart_id = $cart['id'] ?? $cart->id; - - $this->cart->attach($cart_id, $product_id, 1); - - session()->flash('success', 'Item Added To Cart Successfully'); - - 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(); - } - } - - /** - * Function for guests - * user to remove the product - * in the cart. - * - * @return Mixed - */ - public function guestUnitRemove($id) { - - //remove the products here - if(Cookie::has('session_cart_id')) { - $products = unserialize(Cookie::get('session_cart_id')); - - foreach($products as $key => $value) { - if($value == $id) { - unset($products[$key]); - - array_push($products, $id); - - Cookie::queue('session_cart_id', serialize($products)); - - return redirect()->back(); - } - } - } - } - /* handle the after login event for the customers when their are pruoducts in the session or cookie of the logged in user. */ - public function add($id, $itemdata) { + public function add($id, $data) { - $products = array(); + if(session()->has('cart')) { + $cart = session()->get('cart'); - if(!auth()->guard('customer')->check()) { - throw new \Exception('This function is protected for auth customers only.'); - } + $cartItems = $cart->items; - $data['customer_id'] = auth()->guard('customer')->user()->id; + if(isset($cartItems)) { - $data['channel_id'] = core()->getCurrentChannel()->id; + foreach($cartItems as $cartItem) { + if($cartItem->product_id == $id) { + $prevQty = $cartItem->quantity; - $customer_cart = $this->cart->findOneByField('customer_id', $data['customer_id']); + $newQty = $data['quantity']; - //if there are products already in cart of that customer. - $customer_cart_id = $customer_cart->id ?? $customer_cart['id']; + $cartItem->update(['quantity' => $prevQty + $newQty]); - /** - * Check if their any - * instance of current - * customer in the cart - * table. - */ - if(isset($customer_cart)) { - $customer_cart_items = $this->cart->items($customer_cart_id); - - $customer_cart_products = array(); - - foreach($customer_cart_items as $customer_cart_item) { - array_push($customer_cart_products, $this->cartItem->getProduct($customer_cart_item->id)); - } - - if (isset($customer_cart_products)) { - - foreach ($customer_cart_products as $customer_cart_product) { - if($customer_cart_product == $id) { - $cartItemId = $this->cartItem->findOneByField('product_id', $id)->id; - - $cartItemQuantity = $this->cartItem->findOneByField('product_id', $id)->quantity; - - $this->cartItem->update(['quantity' => $cartItemQuantity + $itemdata['quantity']], $cartItemId); - - session()->flash('error', 'Item already exists in cart'); + session()->flash('success', "Product Quantity Successfully Updated"); return redirect()->back(); } } - //add the product in the cart - $this->cart->attach($customer_cart_id, $id, $itemdata['quantity'], $itemdata['price']); + $data['cart_id'] = $cart->id; - session()->flash('success', 'Item Added To Cart Successfully'); + $data['product_id'] = $id; - return redirect()->back(); + $cart->items()->create($data); + + session()->flash('success', 'Item Successfully Added To Cart'); } else { - $this->cart->destroy($customer_cart_id); - - session()->flash('error', 'Try Adding The Item Again'); - - dd('cart instance without any product found, delete it and create a new one for the current product id'); - - return redirect()->back(); + if(isset($cart)) { + $this->cart->delete($cart->id); + } else { + $this->createNewCart($id, $data); + } } } else { - /** - * this will work - * for logged in users - * and they do not have - * any cart instance - * found in the database. - */ - - 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']); - - session()->flash('success', 'Item Added To Cart Successfully'); - - return redirect()->back(); - - } else { - session()->flash('error', 'Cannot Add Item in Cart'); - - return redirect()->back(); - } + $this->createNewCart($id, $data); } } @@ -364,95 +153,68 @@ class Cart { */ public function mergeCart() { - //considering cookie as a source of truth. - if(Cookie::has('cart_session_id')) { - /* - Check for previous cart of customer and - pull products from that cart instance - and then check for unique products - and delete the record with session id - and increase the quantity of the products - that are added again before deleting the - guest cart record. - */ + if(session()->has('cart')) { + $cart = session()->get('cart'); - //To hold the customer ID which is currently logged in - $customer_id = auth()->guard('customer')->user()->id; + $cartItems = $cart->items; - //having the session id saved in the cart. - $cart_session_id = Cookie::get('cart_session_id'); + $customerCart = $this->cart->findOneByField('customer_id', auth()->guard('customer')->user()->id); - //pull the record from cart table for above session id. - $guest_cart = $this->cart->findOneByField('session_id', $cart_session_id); + if(isset($customerCart)) { + $customerCartItems = $this->cart->items($customerCart['id']); - if(!isset($guest_cart)) { - dd('Some One Deleted Cart'); + if(isset($customerCart)) { + foreach($customerCartItems as $customerCartItem) { - return redirect()->back(); - } - $guest_cart_items = $this->cart->items($guest_cart->id); + foreach($cartItems as $key => $cartItem) { - $guest_cart_products = array(); + if($cartItem->product_id == $customerCartItem->id) { - foreach($guest_cart_items as $guest_cart_item) { - array_push($guest_cart_products, $this->cartItem->getProduct($guest_cart_item->id)); - } + $customerItemQuantity = $customerCartItem->quantity; - //check if the current logged in customer is also - //having any previously saved cart instances. - $customer_cart = $this->cart->findOneByField('customer_id', $customer_id); + $cartItemQuantity = $cartItem->quantity; - if(isset($customer_cart)) { - $customer_cart_items = $this->cart->items($customer_cart->id); + $customerCartItem->update(['cart_id' => $customerCart->id, 'quantity' => $cartItemQuantity + $customerItemQuantity]); - $customer_cart_products = array(); + $cartItem->destroy(); - foreach($customer_cart_items as $customer_cart_item) { - array_push($customer_cart_products, $this->cartItem->getProduct($customer_cart_item->id)); - } - - foreach($guest_cart_products as $key => $guest_cart_product) { - foreach($customer_cart_products as $customer_cart_product) { - if($guest_cart_product == $customer_cart_product) { - $cartItemId = $this->cartItem->findWhere(['cart_id'=> $guest_cart->id, 'product_id' => $guest_cart_product])[0]->id; - - $cartItemQuantity = $this->cartItem->findWhere(['cart_id'=> $guest_cart->id, 'product_id' => $guest_cart_product])[0]->quantity; - - $customerItemId = $this->cartItem->findWhere(['cart_id'=> $customer_cart->id, 'product_id' => $customer_cart_product])[0]->id; - - $customerItemQuantity = $this->cartItem->findWhere(['cart_id'=> $customer_cart->id, 'product_id' => $customer_cart_product])[0]->quantity; - - $this->cartItem->delete($cartItemId); - - $this->cartItem->update(['quantity' => $cartItemQuantity + $customerItemQuantity], $customerItemId); - - unset($guest_cart_products[$key]); + unset($cartItems[$key]); + } } } + + foreach($cartItems as $cartItem) { + $cartItem->update(['cart_id' => $customerCart->id]); + } + + $this->cart->delete($cart->id); + + return redirect()->back(); } - //insert the new products here. - foreach ($guest_cart_products as $key => $guest_cart_product) { - // dd($this->cartItem->findWhere(['cart_id'=> $guest_cart->id, 'product_id' => $guest_cart_product])[0]); - $cartItemId = $this->cartItem->findWhere(['cart_id'=> $guest_cart->id, 'product_id' => $guest_cart_product])[0]->id; - - $this->cartItem->update(['cart_id' => $customer_cart->id], $cartItemId); - } - - //detach with guest cart records - $this->cart->deleteParent($guest_cart->id); - - Cookie::queue(Cookie::forget('cart_session_id')); - - return redirect()->back(); } else { - //this will just update the customer id column in the cart table - $this->cart->update(['customer_id' => $customer_id], $guest_cart->id); - - Cookie::queue(Cookie::forget('cart_session_id')); + foreach($cartItems as $cartItem) { + $this->cart->update(['customer_id' => auth()->guard('customer')->user()->id], $cart->id); + } return redirect()->back(); } + } else { + return redirect()->back(); + } + } + + /** + * Destroys the session + * maintained for cart + * on customer logout. + * + * @return Mixed + */ + public function destroyCart() { + if(session()->has('cart')) { + session()->forget('cart'); + + return redirect()->back(); } - return redirect()->back(); } } \ No newline at end of file diff --git a/packages/Webkul/Cart/src/Database/Migrations/2018_09_05_150444_create_cart_table.php b/packages/Webkul/Cart/src/Database/Migrations/2018_09_05_150444_create_cart_table.php index 6a2195acc..9809f692d 100644 --- a/packages/Webkul/Cart/src/Database/Migrations/2018_09_05_150444_create_cart_table.php +++ b/packages/Webkul/Cart/src/Database/Migrations/2018_09_05_150444_create_cart_table.php @@ -22,6 +22,22 @@ class CreateCartTable extends Migration $table->foreign('channel_id')->references('id')->on('channels'); $table->string('coupon_code')->nullable(); $table->boolean('is_gift')->nullable(); + $table->integer('items_count')->nullable(); + $table->decimal('items_qty', 12, 4)->nullable(); + $table->string('global_currency_code')->nullable(); + $table->string('base_currency_code')->nullable(); + $table->string('store_currency_code')->nullable(); + $table->string('quote_currency_code')->nullable(); + $table->decimal('grand_total', 12, 4)->nullable(); + $table->decimal('base_grand_total', 12, 4)->nullable(); + $table->decimal('sub_total', 12, 4)->nullable(); + $table->decimal('base_sub_total', 12, 4)->nullable(); + $table->decimal('sub_total_with_discount', 12, 4)->nullable(); + $table->decimal('base_sub_total_with_discount', 12, 4)->nullable(); + $table->string('checkout_method')->nullable(); + $table->boolean('is_guest')->nullable(); + $table->string('customer_full_name')->nullable(); + $table->dateTime('conversion_time')->nullable(); $table->timestamps(); }); } diff --git a/packages/Webkul/Cart/src/Database/Migrations/2018_09_05_150915_create_cart_items_table.php b/packages/Webkul/Cart/src/Database/Migrations/2018_09_05_150915_create_cart_items_table.php index 0d739e52e..5fe74cc90 100644 --- a/packages/Webkul/Cart/src/Database/Migrations/2018_09_05_150915_create_cart_items_table.php +++ b/packages/Webkul/Cart/src/Database/Migrations/2018_09_05_150915_create_cart_items_table.php @@ -23,6 +23,15 @@ class CreateCartItemsTable extends Migration $table->integer('tax_category_id')->unsigned()->nullable(); $table->foreign('tax_category_id')->references('id')->on('tax_categories'); $table->string('coupon_code')->nullable(); + $table->decimal('weight', 12,4)->nullable(); + $table->decimal('price', 12,4)->nullable(); + $table->decimal('base_price', 12,4)->nullable(); + $table->decimal('custom_price', 12,4)->nullable(); + $table->decimal('discount_percent', 12,4)->nullable(); + $table->decimal('discount_amount', 12,4)->nullable(); + $table->decimal('base_discount_amount', 12,4)->nullable(); + $table->boolean('no_discount')->nullable()->default(0); + $table->json('additional')->nullable(); $table->timestamps(); }); } diff --git a/packages/Webkul/Cart/src/Http/Controllers/CartController.php b/packages/Webkul/Cart/src/Http/Controllers/CartController.php index 4dce35386..cc77f47c9 100644 --- a/packages/Webkul/Cart/src/Http/Controllers/CartController.php +++ b/packages/Webkul/Cart/src/Http/Controllers/CartController.php @@ -68,7 +68,7 @@ class CartController extends Controller public function add($id) { $data = request()->input(); - // dd($data); + if(!isset($data['is_configurable']) || !isset($data['product']) ||!isset($data['quantity'])) { session()->flash('error', 'Cannot Product Due to User\'s miscreancy in system\'s integrity'); @@ -83,11 +83,7 @@ class CartController extends Controller $data['price'] = $this->product->findOneByField('id', $data['selected_configurable_option'])->price; } - if(auth()->guard('customer')->check()) { - Cart::add($id, $data); - } else { - Cart::guestUnitAdd($id, $data); - } + Cart::add($id, $data); return redirect()->back(); } @@ -111,18 +107,28 @@ class CartController extends Controller * @return Array */ public function test() { - $cartItems = $this->cart->items(75); + $cart = $this->cart->findOneByField('id', 110); - $products = array(); - foreach($cartItems as $cartItem) { - $cartItemId = $cartItem->id; + $items = $cart->items; - $this->cart->updateItem(75, $cartItemId, 'quantity', $cartItem->quantity+1); + $allProdQty = array(); - array_push($products, ['product_id' => $this->cartItem->getProduct($cartItemId), 'quantity' => $cartItem->quantity]); + $allProdQty1 = array(); + + $totalQty = 0; + + foreach($items as $item) { + $inventories = $item->product->inventories; + + foreach($inventories as $inventory) { + $totalQty = $totalQty + $inventory->qty; + } + + array_push($allProdQty1, $totalQty); + + $allProdQty[$item->product->id] = $totalQty; } - dd($products); - return response()->json($products, 200); + dd($allProdQty, $allProdQty1); } } \ 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..362bad43c 100644 --- a/packages/Webkul/Cart/src/Repositories/CartRepository.php +++ b/packages/Webkul/Cart/src/Repositories/CartRepository.php @@ -65,7 +65,18 @@ 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]); + } + /** + * Create Cart Item + * Through Cart. + * + * @return Collection + */ + public function createItem($cartId, $data) { + $cart = $this->model->findOrFail($cartId); + + return $cart->items()->create($data); } /** diff --git a/packages/Webkul/Customer/src/Http/Controllers/SessionController.php b/packages/Webkul/Customer/src/Http/Controllers/SessionController.php index 1ad764b39..afa24027a 100644 --- a/packages/Webkul/Customer/src/Http/Controllers/SessionController.php +++ b/packages/Webkul/Customer/src/Http/Controllers/SessionController.php @@ -72,7 +72,7 @@ class SessionController extends Controller { auth()->guard('customer')->logout(); - Event::fire('customer.after.logout', 1); + Event::fire('customer.after.logout', $id); return redirect()->route($this->_config['redirect']); } diff --git a/packages/Webkul/Customer/src/Http/Listeners/CustomerEventsHandler.php b/packages/Webkul/Customer/src/Http/Listeners/CustomerEventsHandler.php index bb6bba8ec..c8411d440 100644 --- a/packages/Webkul/Customer/src/Http/Listeners/CustomerEventsHandler.php +++ b/packages/Webkul/Customer/src/Http/Listeners/CustomerEventsHandler.php @@ -32,6 +32,7 @@ class CustomerEventsHandler { //use this when there is very uttermost need to use it. public function onCustomerLogout($event) { + Cart::destroyCart(); } /** diff --git a/packages/Webkul/Shop/src/Resources/assets/js/components/cart-dropdown.vue b/packages/Webkul/Shop/src/Resources/assets/js/components/cart-dropdown.vue index 9b051a3ba..bea9aa806 100644 --- a/packages/Webkul/Shop/src/Resources/assets/js/components/cart-dropdown.vue +++ b/packages/Webkul/Shop/src/Resources/assets/js/components/cart-dropdown.vue @@ -34,7 +34,6 @@ -