diff --git a/src/CartItem.php b/src/CartItem.php index 8b3d69c..d2f2bc6 100644 --- a/src/CartItem.php +++ b/src/CartItem.php @@ -282,7 +282,9 @@ public function getDiscount($format = true) { $amount = 0; - if (app('laracart')->findCoupon($this->code)) { + if($this->coupon) { + $amount = $this->coupon->forItem($this); + } else if(app('laracart')->findCoupon($this->code)) { $amount = $this->discount; } diff --git a/src/LaraCart.php b/src/LaraCart.php index 6717e77..22b198b 100644 --- a/src/LaraCart.php +++ b/src/LaraCart.php @@ -869,8 +869,7 @@ private function removeCouponFromItems($code = null) foreach ($this->getItems() as $item) { if (isset($item->code) && (empty($code) || $item->code == $code)) { $item->code = null; - $item->discount = null; - $item->couponInfo = []; + $item->coupon = null; } } } diff --git a/src/Traits/CouponTrait.php b/src/Traits/CouponTrait.php index eba2a56..c6da7c4 100644 --- a/src/Traits/CouponTrait.php +++ b/src/Traits/CouponTrait.php @@ -157,18 +157,11 @@ public function checkValidTimes(Carbon $startDate, Carbon $endDate, $throwErrors * Sets a discount to an item with what code was used and the discount amount. * * @param CartItem $item - * @param $discountAmount - * - * @throws InvalidPrice */ - public function setDiscountOnItem(CartItem $item, $discountAmount) + public function setDiscountOnItem(CartItem $item) { - if (!is_numeric($discountAmount)) { - throw new InvalidPrice('You must use a discount amount.'); - } $this->appliedToCart = false; $item->code = $this->code; - $item->discount = $discountAmount; - $item->couponInfo = $this->options; + $item->coupon = $this; } } diff --git a/tests/CouponsTest.php b/tests/CouponsTest.php index 7c85620..e1ed373 100644 --- a/tests/CouponsTest.php +++ b/tests/CouponsTest.php @@ -201,7 +201,7 @@ public function testSetDiscountOnItem() $this->assertEquals(90 * 1.07, $this->laracart->total(false)); - $coupon->setDiscountOnItem($item, 10.00); + $coupon->setDiscountOnItem($item); $this->assertEquals('10OFF', $item->code); @@ -237,7 +237,7 @@ public function testDiscountTotals() $coupon = $this->laracart->findCoupon('10OFF'); - $coupon->setDiscountOnItem($item, 10.00); + $coupon->setDiscountOnItem($item); $this->assertEquals('10OFF', $item->code); @@ -304,7 +304,7 @@ public function testRemoveCoupons() $coupon = $this->laracart->findCoupon('10OFF'); - $coupon->setDiscountOnItem($item, 10.00); + $coupon->setDiscountOnItem($item); $this->assertEquals('10OFF', $item->code); diff --git a/tests/TotalsTest.php b/tests/TotalsTest.php index 06b7682..58e4537 100644 --- a/tests/TotalsTest.php +++ b/tests/TotalsTest.php @@ -258,7 +258,7 @@ public function testDoubleDiscounts() ]); $this->laracart->addCoupon($coupon); - $coupon->setDiscountOnItem($item, $item->price(false) * $coupon->value); + $coupon->setDiscountOnItem($item); $this->assertEquals(95, $this->laracart->subTotal(false)); $this->assertEquals(5, $this->laracart->totalDiscount(false));