Skip to content

Commit

Permalink
Merge pull request #178 from aditya-cherukuri/discounts
Browse files Browse the repository at this point in the history
Allow discounts on fees
  • Loading branch information
lukepolo authored Feb 22, 2017
2 parents 9e63be8 + 88fa04c commit f09ba37
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/Coupons/Fixed.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ public function __construct($code, $value, $options = [])
*/
public function discount($throwErrors = false)
{
$total = app(LaraCart::SERVICE)->subTotal(false) - $this->value;
if (config('laracart.discountOnFees', false)) {
$total = app(LaraCart::SERVICE)->subTotal(false) + app(LaraCart::SERVICE)->feeTotals(false) - $this->value;
} else {
$total = app(LaraCart::SERVICE)->subTotal(false) - $this->value;
}

if ($total < 0) {
return app(LaraCart::SERVICE)->subTotal(false);
Expand Down
8 changes: 8 additions & 0 deletions src/config/laracart.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@
*/
'discountTaxable' => true,

/*
|--------------------------------------------------------------------------
| Allows you to choose if the discounts applied to fees
|--------------------------------------------------------------------------
|
*/
'discountOnFees' => false,

/*
|--------------------------------------------------------------------------
| Allows you to configure if a user can apply multiple coupons
Expand Down
23 changes: 23 additions & 0 deletions tests/CouponsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,9 @@ public function testCouponMessage()
$this->assertEquals('Sorry, you must have at least 100 dollars!', $fixedCoupon->getFailedMessage());
}

/**
* Testing discount when total is greater than applied coupon value.
*/
public function testFixedCouponWithTotalLessThanCoupon()
{
$fixedCoupon = new LukePOLO\LaraCart\Coupons\Fixed('500 OFF', 500);
Expand All @@ -345,4 +348,24 @@ public function testFixedCouponWithTotalLessThanCoupon()

$this->assertEquals('400', $fixedCoupon->discount());
}

/**
* Testing discount when total with fees is greater than applied coupon value.
*/
public function testFixedCouponWithFeeWithTotalLessThanCoupon()
{
$fixedCoupon = new LukePOLO\LaraCart\Coupons\Fixed('500 OFF', 500);

$this->laracart->addCoupon($fixedCoupon);

$this->assertEquals('0', $fixedCoupon->discount());

$this->addItem(1, 400);

$this->laracart->addFee('testFee', 150);

$this->app['config']->set('laracart.discountOnFees', true);

$this->assertEquals('500', $fixedCoupon->discount());
}
}

0 comments on commit f09ba37

Please sign in to comment.