From 4d302fb4a20714e7f6b63cda2c1c67b29a231444 Mon Sep 17 00:00:00 2001 From: Pascal Kousbroek Date: Fri, 29 Mar 2019 15:39:17 +0100 Subject: [PATCH 1/3] Update Cart.php, fix CartItem::fromAttributes call The CartItem::fromAttributes call is missing the $weight parameter, and the CartItemOptions don't work any more because of this. --- src/Cart.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Cart.php b/src/Cart.php index cbb2699..51e2add 100644 --- a/src/Cart.php +++ b/src/Cart.php @@ -710,7 +710,7 @@ class Cart $cartItem = CartItem::fromArray($id); $cartItem->setQuantity($id['qty']); } else { - $cartItem = CartItem::fromAttributes($id, $name, $price, $options); + $cartItem = CartItem::fromAttributes($id, $name, $price, null, $options); $cartItem->setQuantity($qty); } From 279e820760e8ccaf1a3b1a7ffe6f48b7083d243d Mon Sep 17 00:00:00 2001 From: Patrick Henninger Date: Tue, 16 Apr 2019 12:10:06 +0200 Subject: [PATCH 2/3] Fixed weight integration, added testcase, fixed weird readme code example --- README.md | 6 +++--- src/Cart.php | 10 ++++++---- tests/CartTest.php | 13 +++++++++++++ 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index c227e1c..b0340e3 100644 --- a/README.md +++ b/README.md @@ -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.** diff --git a/src/Cart.php b/src/Cart.php index 51e2add..0e2269c 100644 --- a/src/Cart.php +++ b/src/Cart.php @@ -105,11 +105,12 @@ class Cart * @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) { @@ -117,7 +118,7 @@ class Cart }, $id); } - $cartItem = $this->createCartItem($id, $name, $qty, $price, $options); + $cartItem = $this->createCartItem($id, $name, $qty, $price, $weight, $options); return $this->addCartItem($cartItem); } @@ -696,11 +697,12 @@ class Cart * @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 ?: []); @@ -710,7 +712,7 @@ class Cart $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); } diff --git a/tests/CartTest.php b/tests/CartTest.php index b51863d..8fb366d 100644 --- a/tests/CartTest.php +++ b/tests/CartTest.php @@ -1149,6 +1149,19 @@ class CartTest extends TestCase $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. * From f4673140f791efa883eb8802058ca944faf03949 Mon Sep 17 00:00:00 2001 From: Patrick Henninger Date: Tue, 16 Apr 2019 12:12:11 +0200 Subject: [PATCH 3/3] Codestyle --- tests/CartTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/CartTest.php b/tests/CartTest.php index 8fb366d..8c278e5 100644 --- a/tests/CartTest.php +++ b/tests/CartTest.php @@ -1155,7 +1155,7 @@ class CartTest extends TestCase // https://github.com/bumbummen99/LaravelShoppingcart/pull/5 $cart = $this->getCart(); - $cartItem = $cart->add('293ad', 'Product 1', 1, 9.99, 550, ['size' => 'large']); + $cartItem = $cart->add('293ad', 'Product 1', 1, 9.99, 550, ['size' => 'large']); $this->assertEquals(550, $cartItem->weight); $this->assertTrue($cartItem->options->has('size'));