Skip to content

Commit

Permalink
Feature/multiple item discount (#266)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukepolo authored Apr 3, 2020
1 parent d6721f2 commit 696de54
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ language: php
php:
- 7.2
- 7.3
- 7.4

addons:
code_climate:
Expand Down
6 changes: 4 additions & 2 deletions src/CartItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ public function subTotal($format = true, $withDiscount = true, $taxedItemsOnly =
public function netTotal($format = true)
{
return LaraCart::formatMoney(
($this->price(false, false, true) * $this->qty) - $this->discount - $this->tax(false, true),
($this->price(false, false, true) * $this->qty) - $this->getDiscount(false) - $this->tax(false, true),
$this->locale,
$this->internationalFormat,
$format
Expand Down Expand Up @@ -291,8 +291,10 @@ public function getDiscount($format = true)
$amount = $this->discount;
}

$amount = $amount * $this->qty;

if ($amount < 0) {
$amount = $this->price;
$amount = $this->price * $this->qty;
}

return LaraCart::formatMoney(
Expand Down
29 changes: 29 additions & 0 deletions tests/TotalsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -606,4 +606,33 @@ public function testPreTaxationAndDiscountSubTotalOnItem()

$this->assertEquals(1, $item->subTotal(false, true, false, true));
}

public function testDiscountsOnMultiQtyItems()
{
$this->laracart->emptyCart();
$this->laracart->destroyCart();

$item = $this->laracart->add(123, 'T-Shirt', 2, 100, ['tax' => .2], true);

$coupon = new \LukePOLO\LaraCart\Coupons\Percentage('10%OFF', 0.10);
$this->laracart->addCoupon($coupon);
$coupon->setDiscountOnItem($item);

$resume = [
'subTotal' => $this->laracart->subTotal(true, false),
'totalDiscount' => $this->laracart->totalDiscount(true),
'subTotalWithDiscount' => $this->laracart->subTotal(true, true),
'taxTotal' => $this->laracart->taxTotal(true),
'netTotal' => $this->laracart->netTotal(true),
'feeTotals' => $this->laracart->feeTotals(true, true),
'total' => $this->laracart->total(true, true),
];

$this->assertEquals($item->getDiscount(false), 20);
$this->assertEquals($this->laracart->subTotal(false, false), 200);

$this->assertEquals($this->laracart->netTotal(false), 184);
$this->assertEquals($this->laracart->taxTotal(false, false), 36);
$this->assertEquals($this->laracart->total(false, false), 216);
}
}

0 comments on commit 696de54

Please sign in to comment.