Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Cart.php, fix CartItem::fromAttributes call #5

Merged
merged 3 commits into from
Apr 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,16 @@ The shoppingcart gives you the following methods to use:

Adding an item to the cart is really simple, you just use the `add()` method, which accepts a variety of parameters.

In its most basic form you can specify the id, name, quantity, price of the product you'd like to add to the cart.
In its most basic form you can specify the id, name, quantity, price and weight of the product you'd like to add to the cart.

```php
Cart::add('293ad', 'Product 1', 1, 9.99);
Cart::add('293ad', 'Product 1', 1, 9.99, 550);
```

As an optional fifth parameter you can pass it options, so you can add multiple items with the same id, but with (for instance) a different size.

```php
Cart::add('293ad', 'Product 1', 1, 9.99, 'weight' => 550, ['size' => 'large']);
Cart::add('293ad', 'Product 1', 1, 9.99, 550, ['size' => 'large']);
```

**The `add()` method will return an CartItem instance of the item you just added to the cart.**
Expand Down
10 changes: 6 additions & 4 deletions src/Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,19 +105,20 @@ public function currentInstance()
* @param mixed $name
* @param int|float $qty
* @param float $price
* @param float $weight
* @param array $options
*
* @return \Gloudemans\Shoppingcart\CartItem
*/
public function add($id, $name = null, $qty = null, $price = null, array $options = [])
public function add($id, $name = null, $qty = null, $price = null, $weight = null, array $options = [])
{
if ($this->isMulti($id)) {
return array_map(function ($item) {
return $this->add($item);
}, $id);
}

$cartItem = $this->createCartItem($id, $name, $qty, $price, $options);
$cartItem = $this->createCartItem($id, $name, $qty, $price, $weight, $options);

return $this->addCartItem($cartItem);
}
Expand Down Expand Up @@ -696,11 +697,12 @@ protected function getContent()
* @param mixed $name
* @param int|float $qty
* @param float $price
* @param float $weight
* @param array $options
*
* @return \Gloudemans\Shoppingcart\CartItem
*/
private function createCartItem($id, $name, $qty, $price, array $options)
private function createCartItem($id, $name, $qty, $price, $weight, array $options)
{
if ($id instanceof Buyable) {
$cartItem = CartItem::fromBuyable($id, $qty ?: []);
Expand All @@ -710,7 +712,7 @@ private function createCartItem($id, $name, $qty, $price, array $options)
$cartItem = CartItem::fromArray($id);
$cartItem->setQuantity($id['qty']);
} else {
$cartItem = CartItem::fromAttributes($id, $name, $price, $options);
$cartItem = CartItem::fromAttributes($id, $name, $price, $weight, $options);
$cartItem->setQuantity($qty);
}

Expand Down
13 changes: 13 additions & 0 deletions tests/CartTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1149,6 +1149,19 @@ public function it_does_calculate_correct_results_with_rational_qtys()
$this->assertEquals(2.50, $cart->tax(2)); // tax of 5 Bucks
}

/** @test */
public function it_does_allow_adding_cart_items_with_weight_and_options()
{
// https://github.com/bumbummen99/LaravelShoppingcart/pull/5
$cart = $this->getCart();

$cartItem = $cart->add('293ad', 'Product 1', 1, 9.99, 550, ['size' => 'large']);

$this->assertEquals(550, $cartItem->weight);
$this->assertTrue($cartItem->options->has('size'));
$this->assertEquals('large', $cartItem->options->size);
}

/**
* Get an instance of the cart.
*
Expand Down