Skip to content

Commit

Permalink
Fixed weight integration, added testcase, fixed weird readme code exa…
Browse files Browse the repository at this point in the history
…mple
  • Loading branch information
bumbummen99 committed Apr 16, 2019
1 parent 4d302fb commit 279e820
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
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, null, $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

1 comment on commit 279e820

@giuliogatto
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please insert this commit in the composer distributed version of the library? I am still getting the old code with the weight integration bug installing with composer. Thanks!

Please sign in to comment.