From 2d15d9ce111c069b24e3d6a8d92d96b01fe49671 Mon Sep 17 00:00:00 2001 From: Milwad Khosravi Date: Wed, 3 Jul 2024 10:22:58 +0330 Subject: [PATCH 01/12] add `increaseQuantity` --- src/Models/Cart.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/Models/Cart.php b/src/Models/Cart.php index 8c1163f..fafd0fc 100644 --- a/src/Models/Cart.php +++ b/src/Models/Cart.php @@ -144,4 +144,19 @@ public function emptyCart(): Cart return $this; } + + /** + * Increase the quantity of the item. + */ + public function increaseQuantity(Model $item, int $quantity = 1): static + { + $item = $this->items()->find($item->getKey()); + if (! $item) { + throw new \RuntimeException('The item not found'); + } + + $item->increment('quantity', $quantity); + + return $this; + } } From dab6533a1642f6a31b210a9b96407292d259cda9 Mon Sep 17 00:00:00 2001 From: Milwad Khosravi Date: Wed, 3 Jul 2024 10:23:25 +0330 Subject: [PATCH 02/12] add `decreaseQuantity` --- src/Models/Cart.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/Models/Cart.php b/src/Models/Cart.php index fafd0fc..5613d51 100644 --- a/src/Models/Cart.php +++ b/src/Models/Cart.php @@ -159,4 +159,19 @@ public function increaseQuantity(Model $item, int $quantity = 1): static return $this; } + + /** + * Decrease the quantity of the item. + */ + public function decreaseQuantity(Model $item, int $quantity = 1): static + { + $item = $this->items()->find($item->getKey()); + if (! $item) { + throw new \RuntimeException('The item not found'); + } + + $item->decrement('quantity', $quantity); + + return $this; + } } From d3a6d2f479f25c0736ae54f1725b4c38ab31a1c1 Mon Sep 17 00:00:00 2001 From: Milwad Khosravi Date: Wed, 3 Jul 2024 10:44:59 +0330 Subject: [PATCH 03/12] create CartUpdateQuantityTest --- ...4_05_31_103315_create_cart_items_table.php | 2 +- src/Models/Cart.php | 8 ++-- tests/Feature/CartUpdateQuantityTest.php | 41 +++++++++++++++++++ 3 files changed, 46 insertions(+), 5 deletions(-) create mode 100644 tests/Feature/CartUpdateQuantityTest.php diff --git a/database/migrations/2024_05_31_103315_create_cart_items_table.php b/database/migrations/2024_05_31_103315_create_cart_items_table.php index 2b3ae5b..9a1d272 100644 --- a/database/migrations/2024_05_31_103315_create_cart_items_table.php +++ b/database/migrations/2024_05_31_103315_create_cart_items_table.php @@ -20,7 +20,7 @@ public function up(): void $table->foreignId($cartForeignName)->constrained($cartTableName)->cascadeOnDelete(); $table->morphs('itemable'); - $table->unsignedInteger('quantity'); + $table->unsignedInteger('quantity')->default(1); $table->timestamps(); }); diff --git a/src/Models/Cart.php b/src/Models/Cart.php index 5613d51..cd2e62f 100644 --- a/src/Models/Cart.php +++ b/src/Models/Cart.php @@ -90,7 +90,7 @@ public function calculatedPriceByQuantity(): int /** * Store multiple items. */ - public function storeItems(array $items): Cart + public function storeItems(array $items): static { foreach ($items as $item) { $this->storeItem($item); @@ -102,7 +102,7 @@ public function storeItems(array $items): Cart /** * Store cart item in cart. */ - public function storeItem(Model|array $item): Cart + public function storeItem(Model|array $item): static { if (is_array($item)) { $item['itemable_id'] = $item['itemable']->getKey(); @@ -124,7 +124,7 @@ public function storeItem(Model|array $item): Cart /** * Remove a single item from the cart */ - public function removeItem(Model $item): Cart + public function removeItem(Model $item): static { $itemToDelete = $this->items()->find($item->getKey()); @@ -150,7 +150,7 @@ public function emptyCart(): Cart */ public function increaseQuantity(Model $item, int $quantity = 1): static { - $item = $this->items()->find($item->getKey()); + $item = $this->items()->firstWhere('itemable_id', $item->getKey()); if (! $item) { throw new \RuntimeException('The item not found'); } diff --git a/tests/Feature/CartUpdateQuantityTest.php b/tests/Feature/CartUpdateQuantityTest.php new file mode 100644 index 0000000..06eac75 --- /dev/null +++ b/tests/Feature/CartUpdateQuantityTest.php @@ -0,0 +1,41 @@ +create(['name' => 'Milwad', 'email' => 'milwad.dev@gmail.comd']); + $product = Product::query()->create(['title' => 'Product 1']); + + // Create cart + $cart = Cart::query()->firstOrCreate(['user_id' => $user->id]); + + // Store item to cart + $cartItem = new CartItem([ + 'itemable_id' => $product->id, + 'itemable_type' => $product::class, + 'quantity' => 1, + ]); + + $cart->items()->save($cartItem); + + + assertDatabaseHas('cart_items', ['quantity' => 1]); + + // Increase quantity + $cart->increaseQuantity($product, 2); + + assertDatabaseHas('cart_items', ['quantity' => 3]); +}); + From 14c03f3f6167ed2d5c8e21e6408f80a6260fc8ff Mon Sep 17 00:00:00 2001 From: Milwad Khosravi Date: Wed, 3 Jul 2024 10:45:31 +0330 Subject: [PATCH 04/12] add `can decrease quantity of the item in cart` test --- tests/Feature/CartUpdateQuantityTest.php | 25 +++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/tests/Feature/CartUpdateQuantityTest.php b/tests/Feature/CartUpdateQuantityTest.php index 06eac75..1ba5332 100644 --- a/tests/Feature/CartUpdateQuantityTest.php +++ b/tests/Feature/CartUpdateQuantityTest.php @@ -30,7 +30,6 @@ $cart->items()->save($cartItem); - assertDatabaseHas('cart_items', ['quantity' => 1]); // Increase quantity @@ -39,3 +38,27 @@ assertDatabaseHas('cart_items', ['quantity' => 3]); }); +test('can decrease quantity of the item in cart', function () { + $user = User::query()->create(['name' => 'Milwad', 'email' => 'milwad.dev@gmail.comd']); + $product = Product::query()->create(['title' => 'Product 1']); + + // Create cart + $cart = Cart::query()->firstOrCreate(['user_id' => $user->id]); + + // Store item to cart + $cartItem = new CartItem([ + 'itemable_id' => $product->id, + 'itemable_type' => $product::class, + 'quantity' => 3, + ]); + + $cart->items()->save($cartItem); + + assertDatabaseHas('cart_items', ['quantity' => 3]); + + // Increase quantity + $cart->decreaseQuantity($product, 2); + + assertDatabaseHas('cart_items', ['quantity' => 1]); +}); + From d95efdd7285aa2b91e491bac7a8556954aa73e31 Mon Sep 17 00:00:00 2001 From: Milwad Khosravi Date: Wed, 3 Jul 2024 10:54:43 +0330 Subject: [PATCH 05/12] add `can not decrease quantity of the item in cart when item not found` test --- tests/Feature/CartUpdateQuantityTest.php | 25 ++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tests/Feature/CartUpdateQuantityTest.php b/tests/Feature/CartUpdateQuantityTest.php index 1ba5332..9a191e1 100644 --- a/tests/Feature/CartUpdateQuantityTest.php +++ b/tests/Feature/CartUpdateQuantityTest.php @@ -62,3 +62,28 @@ assertDatabaseHas('cart_items', ['quantity' => 1]); }); +test('can not decrease quantity of the item in cart when item not found', function () { + $user = User::query()->create(['name' => 'Milwad', 'email' => 'milwad.dev@gmail.comd']); + $product1 = Product::query()->create(['title' => 'Product 1']); + + // Create cart + $cart = Cart::query()->firstOrCreate(['user_id' => $user->id]); + + // Store item to cart + $cartItem = new CartItem([ + 'itemable_id' => $product1->id, + 'itemable_type' => $product1::class, + 'quantity' => 3, + ]); + + $cart->items()->save($cartItem); + + assertDatabaseHas('cart_items', ['quantity' => 3]); + + // Increase quantity + $product2 = Product::query()->create(['title' => 'Product 2']); + $cart->decreaseQuantity($product2, 2); + + assertDatabaseHas('cart_items', ['quantity' => 3]); +})->expectExceptionMessage('The item not found'); + From f81a2100f88375f46e6af040b2bd6de095edf812 Mon Sep 17 00:00:00 2001 From: Milwad Khosravi Date: Wed, 3 Jul 2024 10:55:17 +0330 Subject: [PATCH 06/12] add `can not increase quantity of the item in cart when item not found` test --- tests/Feature/CartUpdateQuantityTest.php | 25 ++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tests/Feature/CartUpdateQuantityTest.php b/tests/Feature/CartUpdateQuantityTest.php index 9a191e1..07dbf8b 100644 --- a/tests/Feature/CartUpdateQuantityTest.php +++ b/tests/Feature/CartUpdateQuantityTest.php @@ -62,6 +62,31 @@ assertDatabaseHas('cart_items', ['quantity' => 1]); }); +test('can not increase quantity of the item in cart when item not found', function () { + $user = User::query()->create(['name' => 'Milwad', 'email' => 'milwad.dev@gmail.comd']); + $product1 = Product::query()->create(['title' => 'Product 1']); + + // Create cart + $cart = Cart::query()->firstOrCreate(['user_id' => $user->id]); + + // Store item to cart + $cartItem = new CartItem([ + 'itemable_id' => $product1->id, + 'itemable_type' => $product1::class, + 'quantity' => 1, + ]); + + $cart->items()->save($cartItem); + + assertDatabaseHas('cart_items', ['quantity' => 1]); + + // Increase quantity + $product2 = Product::query()->create(['title' => 'Product 2']); + $cart->increaseQuantity($product2, 2); + + assertDatabaseHas('cart_items', ['quantity' => 1]); +})->expectExceptionMessage('The item not found'); + test('can not decrease quantity of the item in cart when item not found', function () { $user = User::query()->create(['name' => 'Milwad', 'email' => 'milwad.dev@gmail.comd']); $product1 = Product::query()->create(['title' => 'Product 1']); From 7e90b620901c286c189c943b0a8173a138b5db58 Mon Sep 17 00:00:00 2001 From: Milwad Khosravi Date: Wed, 3 Jul 2024 11:06:12 +0330 Subject: [PATCH 07/12] Update composer.json --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 82083b8..f5d5311 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "binafy/laravel-cart", - "description": "Laravel Card is designed to be highly customizable and flexible, making it easy to adapt to your specific use case.", + "description": "Laravel Cart is a customizable package for adding shopping cart functionality to Laravel applications", "keywords": [ "binafy", "milwad", From 22a52f6f49a8aca500661abd821007014da3c680 Mon Sep 17 00:00:00 2001 From: Milwad Khosravi Date: Wed, 3 Jul 2024 11:08:15 +0330 Subject: [PATCH 08/12] add `Increase Quantity` in readme --- README.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/README.md b/README.md index 64c62a3..ad2a081 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,8 @@ - [Store Item For a Cart](#store-item-for-a-cart) - [Delete Item From Cart](#delete-item-from-cart) - [Delete All Items From Cart](#delete-all-items-from-cart) + - [Increase Quantity](#increase-quantity) + - [Delete All Items From Cart](#delete-all-items-from-cart) - [Contributors](#contributors) - [Security](#security) - [Changelog](#changelog) @@ -245,6 +247,15 @@ $cart = Cart::query()->firstOrCreate(['user_id' => $user->id]); $cart->emptyCart(); ``` + +### Increase Quantity + +If you may to increase the quantity of item in cart, you can use `increaseQuantity` method: + +```php +$cart->increaseQuantity(item: $item, quantity: 2); // By default quantity is 1 +``` + ## Contributors From f0a91c79d1b765eca5ee01cb7168e706303cf116 Mon Sep 17 00:00:00 2001 From: Milwad Khosravi Date: Wed, 3 Jul 2024 11:08:41 +0330 Subject: [PATCH 09/12] add `Decrease Quantity` in readme --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.md b/README.md index ad2a081..8628586 100644 --- a/README.md +++ b/README.md @@ -256,6 +256,15 @@ If you may to increase the quantity of item in cart, you can use `increaseQuanti $cart->increaseQuantity(item: $item, quantity: 2); // By default quantity is 1 ``` + +### Decrease Quantity + +If you may to decrease the quantity of item in cart, you can use `decreaseQuantity` method: + +```php +$cart->decreaseQuantity(item: $item, quantity: 2); // By default quantity is 1 +``` + ## Contributors From 4194159d85e9b990d28ce31d19f9eacd7e989a55 Mon Sep 17 00:00:00 2001 From: Milwad Khosravi Date: Wed, 3 Jul 2024 11:09:12 +0330 Subject: [PATCH 10/12] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 8628586..63c7f97 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ - [Delete Item From Cart](#delete-item-from-cart) - [Delete All Items From Cart](#delete-all-items-from-cart) - [Increase Quantity](#increase-quantity) - - [Delete All Items From Cart](#delete-all-items-from-cart) + - [Decrease Quantity](#decrease-quantity) - [Contributors](#contributors) - [Security](#security) - [Changelog](#changelog) @@ -256,7 +256,7 @@ If you may to increase the quantity of item in cart, you can use `increaseQuanti $cart->increaseQuantity(item: $item, quantity: 2); // By default quantity is 1 ``` - + ### Decrease Quantity If you may to decrease the quantity of item in cart, you can use `decreaseQuantity` method: From 255634d9c48a29a7377a0ac9d2bf02a1e9f581d4 Mon Sep 17 00:00:00 2001 From: Milwad Khosravi Date: Wed, 3 Jul 2024 11:09:35 +0330 Subject: [PATCH 11/12] Update CartUpdateQuantityTest.php --- tests/Feature/CartUpdateQuantityTest.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/Feature/CartUpdateQuantityTest.php b/tests/Feature/CartUpdateQuantityTest.php index 07dbf8b..a4a8b14 100644 --- a/tests/Feature/CartUpdateQuantityTest.php +++ b/tests/Feature/CartUpdateQuantityTest.php @@ -6,7 +6,6 @@ use Tests\SetUp\Models\Product; use Tests\SetUp\Models\User; -use function Pest\Laravel\assertDatabaseCount; use function Pest\Laravel\assertDatabaseHas; /* @@ -111,4 +110,3 @@ assertDatabaseHas('cart_items', ['quantity' => 3]); })->expectExceptionMessage('The item not found'); - From 449994226bdef15d3026fdc3dafb9d731cca88da Mon Sep 17 00:00:00 2001 From: Milwad Khosravi Date: Wed, 3 Jul 2024 14:12:17 +0330 Subject: [PATCH 12/12] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 63c7f97..b02fa6d 100644 --- a/README.md +++ b/README.md @@ -285,4 +285,4 @@ The changelog can be found in the `CHANGELOG.md` file of the GitHub repository. ## License -The MIT License (MIT). Please see [License File](https://github.com/binafy/laravel-cart/blob/0.x-dev/LICENSE) for more information. +The MIT License (MIT). Please see [License File](https://github.com/binafy/laravel-cart/blob/1.x/LICENSE) for more information.