mirror of
https://github.com/kevin-DL/LaravelShoppingcart.git
synced 2026-01-11 18:54:33 +00:00
minor additions
This commit is contained in:
35
src/Cart.php
35
src/Cart.php
@@ -42,7 +42,7 @@ class Cart
|
||||
*
|
||||
* @var float
|
||||
*/
|
||||
private $discount;
|
||||
private $discount = 0;
|
||||
|
||||
/**
|
||||
* Cart constructor.
|
||||
@@ -54,7 +54,6 @@ class Cart
|
||||
{
|
||||
$this->session = $session;
|
||||
$this->events = $events;
|
||||
$this->discount = 0;
|
||||
|
||||
$this->instance(self::DEFAULT_INSTANCE);
|
||||
}
|
||||
@@ -349,13 +348,39 @@ class Cart
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the global discount percentage for the cart
|
||||
* Set the discount rate for the cart item with the given rowId.
|
||||
*
|
||||
* @param string $rowId
|
||||
* @param int|float $taxRate
|
||||
* @return void
|
||||
*/
|
||||
public function setDiscount($rowId, $discount)
|
||||
{
|
||||
$cartItem = $this->get($rowId);
|
||||
|
||||
$cartItem->setDiscountRate($discount);
|
||||
|
||||
$content = $this->getContent();
|
||||
|
||||
$content->put($cartItem->rowId, $cartItem);
|
||||
|
||||
$this->session->put($this->instance, $content);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the global discount percentage for the cart.
|
||||
* This will set the discount for all cart items.
|
||||
*
|
||||
* @param float $discount
|
||||
*/
|
||||
public function setDiscount(float $discount)
|
||||
public function setGlobalDiscount(float $discount)
|
||||
{
|
||||
$this->discount = $discount;
|
||||
if ($this->content && $this->content->count()) {
|
||||
$this->content->each(function ($item, $key) {
|
||||
$item->setDiscountRate($this->discount);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -480,7 +505,7 @@ class Cart
|
||||
}
|
||||
|
||||
$cartItem->setTaxRate(config('cart.tax'));
|
||||
$cartItem->setDiscountRate( $this->discount * 100 );
|
||||
$cartItem->setDiscountRate( $this->discount );
|
||||
|
||||
return $cartItem;
|
||||
}
|
||||
|
||||
@@ -779,7 +779,7 @@ class CartTest extends TestCase
|
||||
{
|
||||
$this->setConfigFormat(2, ',', '');
|
||||
|
||||
$cart = $this->getCartDiscount( 0.5 );
|
||||
$cart = $this->getCartDiscount( 50 );
|
||||
|
||||
$cart->add(new BuyableProduct(1, 'Some title', 2000.00), 2);
|
||||
|
||||
@@ -887,7 +887,7 @@ class CartTest extends TestCase
|
||||
/** @test */
|
||||
public function it_can_calculate_all_values()
|
||||
{
|
||||
$cart = $this->getCartDiscount(0.5);
|
||||
$cart = $this->getCartDiscount( 50 );
|
||||
|
||||
$cart->add(new BuyableProduct(1, 'First item', 10.00), 2);
|
||||
|
||||
@@ -941,7 +941,7 @@ class CartTest extends TestCase
|
||||
private function getCartDiscount( float $discount = 0 )
|
||||
{
|
||||
$cart = $this->getCart();
|
||||
$cart->setDiscount( 0.5 );
|
||||
$cart->setGlobalDiscount( 50 );
|
||||
|
||||
return $cart;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user