Skip to content

Commit

Permalink
No longer have to put the discount amount on the item , it can detect…
Browse files Browse the repository at this point in the history
… that too
  • Loading branch information
lukepolo committed May 29, 2018
1 parent 1a1e727 commit 10221ac
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 16 deletions.
4 changes: 3 additions & 1 deletion src/CartItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
3 changes: 1 addition & 2 deletions src/LaraCart.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
Expand Down
11 changes: 2 additions & 9 deletions src/Traits/CouponTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
6 changes: 3 additions & 3 deletions tests/CouponsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion tests/TotalsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down

0 comments on commit 10221ac

Please sign in to comment.