mirror of
https://github.com/kevin-DL/LaravelShoppingcart.git
synced 2026-01-11 18:54:33 +00:00
Added Cart merge functionality
Added Readme for merge
This commit is contained in:
@@ -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:
|
||||
|
||||
68
src/Cart.php
68
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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user