Added Cart merge functionality

Added Readme for merge
This commit is contained in:
Patrick Henninger
2018-12-28 03:16:29 +01:00
parent 3ebe09f6ab
commit 4cfd3e99b0
3 changed files with 102 additions and 8 deletions

View File

@@ -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:

View File

@@ -118,19 +118,38 @@ class Cart
$cartItem = $this->createCartItem($id, $name, $qty, $price, $options);
$content = $this->getContent();
if ($content->has($cartItem->rowId)) {
$cartItem->qty += $content->get($cartItem->rowId)->qty;
return $this->addCartItem($cartItem);;
}
$content->put($cartItem->rowId, $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 );
$this->events->fire('cart.added', $cartItem);
if (!$keepTax)
$item->setTaxRate( $this->taxRate );
$content = $this->getContent();
if ($content->has($item->rowId)) {
$item->qty += $content->get($item->rowId)->qty;
}
$content->put($item->rowId, $item);
$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;
}

View File

@@ -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.
*