diff --git a/src/Coupons/Fixed.php b/src/Coupons/Fixed.php index 77ec6fc..acd9480 100644 --- a/src/Coupons/Fixed.php +++ b/src/Coupons/Fixed.php @@ -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); diff --git a/src/config/laracart.php b/src/config/laracart.php index 5c6ecb1..59cfe75 100644 --- a/src/config/laracart.php +++ b/src/config/laracart.php @@ -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 diff --git a/tests/CouponsTest.php b/tests/CouponsTest.php index 903c4be..dcb17c8 100644 --- a/tests/CouponsTest.php +++ b/tests/CouponsTest.php @@ -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); @@ -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()); + } }