minor additions

This commit is contained in:
Patrick Henninger
2018-12-21 16:56:54 +01:00
parent 9e4c6c4a71
commit d947fe8a57
2 changed files with 33 additions and 8 deletions

View File

@@ -42,7 +42,7 @@ class Cart
* *
* @var float * @var float
*/ */
private $discount; private $discount = 0;
/** /**
* Cart constructor. * Cart constructor.
@@ -54,7 +54,6 @@ class Cart
{ {
$this->session = $session; $this->session = $session;
$this->events = $events; $this->events = $events;
$this->discount = 0;
$this->instance(self::DEFAULT_INSTANCE); $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 * @param float $discount
*/ */
public function setDiscount(float $discount) public function setGlobalDiscount(float $discount)
{ {
$this->discount = $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->setTaxRate(config('cart.tax'));
$cartItem->setDiscountRate( $this->discount * 100 ); $cartItem->setDiscountRate( $this->discount );
return $cartItem; return $cartItem;
} }

View File

@@ -779,7 +779,7 @@ class CartTest extends TestCase
{ {
$this->setConfigFormat(2, ',', ''); $this->setConfigFormat(2, ',', '');
$cart = $this->getCartDiscount( 0.5 ); $cart = $this->getCartDiscount( 50 );
$cart->add(new BuyableProduct(1, 'Some title', 2000.00), 2); $cart->add(new BuyableProduct(1, 'Some title', 2000.00), 2);
@@ -887,7 +887,7 @@ class CartTest extends TestCase
/** @test */ /** @test */
public function it_can_calculate_all_values() 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); $cart->add(new BuyableProduct(1, 'First item', 10.00), 2);
@@ -941,7 +941,7 @@ class CartTest extends TestCase
private function getCartDiscount( float $discount = 0 ) private function getCartDiscount( float $discount = 0 )
{ {
$cart = $this->getCart(); $cart = $this->getCart();
$cart->setDiscount( 0.5 ); $cart->setGlobalDiscount( 50 );
return $cart; return $cart;
} }