From 4cfd3e99b0c4d23da87130443e2c713e6c742a44 Mon Sep 17 00:00:00 2001 From: Patrick Henninger Date: Fri, 28 Dec 2018 03:16:29 +0100 Subject: [PATCH] Added Cart merge functionality Added Readme for merge --- README.md | 6 ++++ src/Cart.php | 68 ++++++++++++++++++++++++++++++++++++++++------ tests/CartTest.php | 36 ++++++++++++++++++++++++ 3 files changed, 102 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 4a5985c..9720b54 100644 --- a/README.md +++ b/README.md @@ -477,6 +477,12 @@ If you want to retrieve the cart from the database and restore it, all you have // To restore a cart instance named 'wishlist' Cart::instance('wishlist')->restore('username'); +### Merge the cart +If you want to merge the cart with another one from the database, all you have to do is call the `merge($identifier)` where `$identifier` is the key you specified for the `store` method. You can also define if you want to keep the discount and tax rates of the items. + + // Merge the contents of 'savedcart' into 'username'. + Cart::instance('username')->merge('savedcart', $keepDiscount, $keepTaxrate); + ## Exceptions The Cart package will throw exceptions if something goes wrong. This way it's easier to debug your code using the Cart package or to handle the error based on the type of exceptions. The Cart packages can throw the following exceptions: diff --git a/src/Cart.php b/src/Cart.php index 8df2a57..ce15705 100644 --- a/src/Cart.php +++ b/src/Cart.php @@ -118,19 +118,38 @@ class Cart $cartItem = $this->createCartItem($id, $name, $qty, $price, $options); + return $this->addCartItem($cartItem);; + } + + /** + * Add an item to the cart. + * + * @param \Gloudemans\Shoppingcart\CartItem $item Item to add to the Cart + * @param bool $keepDiscount Keep the discount rate of the Item + * @param bool $keepTax Keep the Tax rate of the Item + * @return \Gloudemans\Shoppingcart\CartItem The CartItem + */ + public function addCartItem( $item, $keepDiscount = false, $keepTax = false ) + { + if (!$keepDiscount) + $item->setDiscountRate( $this->discount ); + + if (!$keepTax) + $item->setTaxRate( $this->taxRate ); + $content = $this->getContent(); - if ($content->has($cartItem->rowId)) { - $cartItem->qty += $content->get($cartItem->rowId)->qty; + if ($content->has($item->rowId)) { + $item->qty += $content->get($item->rowId)->qty; } - $content->put($cartItem->rowId, $cartItem); + $content->put($item->rowId, $item); - $this->events->fire('cart.added', $cartItem); + $this->events->fire('cart.added', $item); $this->session->put($this->instance, $content); - return $cartItem; + return $item; } /** @@ -248,6 +267,18 @@ class Cart return $content->sum('qty'); } + /** + * Get the number of items instances in the cart. + * + * @return int|float + */ + public function countInstances() + { + $content = $this->getContent(); + + return $content->count(); + } + /** * Get the total price of the items in the cart. * @@ -566,6 +597,30 @@ class Cart ->where('identifier', $identifier)->delete(); } + /** + * Merges the contents of another cart into this cart. + * + * @param mixed $identifier Identifier of the Cart to merge with. + * @param bool $keepDiscount Keep the discount of the CartItems. + * @param bool $keepTax Keep the tax of the CartItems. + * @return void + */ + public function merge( $identifier, $keepDiscount = false, $keepTax = false ) + { + if( ! $this->storedCartWithIdentifierExists($identifier)) { + return; + } + + $stored = $this->getConnection()->table($this->getTableName()) + ->where('identifier', $identifier)->first(); + + $storedContent = unserialize($stored->content); + + foreach ($storedContent as $cartItem) { + $this->addCartItem($cartItem, $keepDiscount, $keepTax); + } + } + /** * Magic method to make accessing the total, tax and subtotal properties possible. * @@ -627,9 +682,6 @@ class Cart $cartItem->setQuantity($qty); } - $cartItem->setTaxRate($this->taxRate); - $cartItem->setDiscountRate( $this->discount ); - return $cartItem; } diff --git a/tests/CartTest.php b/tests/CartTest.php index 213ab84..eb2e24b 100644 --- a/tests/CartTest.php +++ b/tests/CartTest.php @@ -961,6 +961,42 @@ class CartTest extends TestCase $this->assertEquals('24.21', $cartItem->total(2)); } + /** @test */ + public function it_can_merge_multiple_carts() + { + $this->artisan('migrate', [ + '--database' => 'testing', + ]); + + Event::fake(); + + $cart = $this->getCartDiscount(50); + $cart->add( new BuyableProduct(1, 'Item', 10.00), 1); + $cart->add( new BuyableProduct(2, 'Item 2', 10.00), 1); + $cart->store('test'); + + $cart2 = $this->getCart(); + $cart2->instance('test2'); + $cart2->setGlobalTax(0); + $cart2->setGlobalDiscount(0); + + $this->assertEquals('0', $cart2->countInstances()); + + $cart2->merge('test'); + + $this->assertEquals('2', $cart2->countInstances()); + $this->assertEquals(20, $cart2->totalFloat()); + + $cart3 = $this->getCart(); + $cart3->instance('test3'); + $cart3->setGlobalTax(0); + $cart3->setGlobalDiscount(0); + + $cart3->merge('test', true); + + $this->assertEquals(10, $cart3->totalFloat()); + } + /** * Get an instance of the cart. *