Skip to content

Commit

Permalink
Merge pull request #16 from binafy/add-methods-for-increase-decrease-…
Browse files Browse the repository at this point in the history
…quantity

[1.x] Add methods for increase decrease quantity
  • Loading branch information
milwad-dev authored Jul 6, 2024
2 parents 1cf4450 + 4499942 commit f55bcac
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 6 deletions.
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
- [Decrease Quantity](#decrease-quantity)
- [Contributors](#contributors)
- [Security](#security)
- [Changelog](#changelog)
Expand Down Expand Up @@ -245,6 +247,24 @@ $cart = Cart::query()->firstOrCreate(['user_id' => $user->id]);
$cart->emptyCart();
```

<a name="increase-quantity"></a>
### 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
```

<a name="decrease-quantity"></a>
### 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
```

<a name="contributors"></a>
## Contributors

Expand All @@ -265,4 +285,4 @@ The changelog can be found in the `CHANGELOG.md` file of the GitHub repository.
<a name="license"></a>
## 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.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down
36 changes: 33 additions & 3 deletions src/Models/Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
Expand All @@ -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());

Expand All @@ -144,4 +144,34 @@ public function emptyCart(): Cart

return $this;
}

/**
* Increase the quantity of the item.
*/
public function increaseQuantity(Model $item, int $quantity = 1): static
{
$item = $this->items()->firstWhere('itemable_id', $item->getKey());
if (! $item) {
throw new \RuntimeException('The item not found');
}

$item->increment('quantity', $quantity);

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;
}
}
112 changes: 112 additions & 0 deletions tests/Feature/CartUpdateQuantityTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php

use Binafy\LaravelCart\Models\Cart;
use Binafy\LaravelCart\Models\CartItem;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\SetUp\Models\Product;
use Tests\SetUp\Models\User;

use function Pest\Laravel\assertDatabaseHas;

/*
* Use `RefreshDatabase` for delete migration data for each test.
*/
uses(RefreshDatabase::class);

test('can increase 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' => 1,
]);

$cart->items()->save($cartItem);

assertDatabaseHas('cart_items', ['quantity' => 1]);

// Increase quantity
$cart->increaseQuantity($product, 2);

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

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

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

0 comments on commit f55bcac

Please sign in to comment.