Skip to content

Commit

Permalink
fixing issues with fixed and percentage coupons
Browse files Browse the repository at this point in the history
  • Loading branch information
lukepolo committed Jan 15, 2019
1 parent 4c7e912 commit ed4d082
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/Coupons/Fixed.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,13 @@ public function __construct($code, $value, $options = [])
public function discount($throwErrors = false)
{
$subTotal = app(LaraCart::SERVICE)->subTotal(false);
$total = $subTotal - $this->value;

if (config('laracart.discountOnFees', false)) {
$total = $subTotal + app(LaraCart::SERVICE)->feeTotals(false) - $this->value;
$subTotal = $subTotal + app(LaraCart::SERVICE)->feeTotals(false);
}

$total = $subTotal - $this->value;

if ($total < 0) {
return $subTotal;
}
Expand Down
12 changes: 11 additions & 1 deletion src/Coupons/Percentage.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use LukePOLO\LaraCart\CartItem;
use LukePOLO\LaraCart\Contracts\CouponContract;
use LukePOLO\LaraCart\Exceptions\CouponException;
use LukePOLO\LaraCart\LaraCart;
use LukePOLO\LaraCart\Traits\CouponTrait;

Expand All @@ -27,10 +28,14 @@ class Percentage implements CouponContract
* @param $code
* @param $value
* @param array $options
* @throws \Exception
*/
public function __construct($code, $value, $options = [])
{
$this->code = $code;
if($value > 1) {
throw new CouponException('Invalid value for a percentage coupon. The value must be between 0 and 1.');
}
$this->value = $value;

$this->setOptions($options);
Expand Down Expand Up @@ -62,8 +67,13 @@ public function forItem(CartItem $item)
*/
public function discount($throwErrors = false)
{
$subTotal = app(LaraCart::SERVICE)->subTotal(false);
if (config('laracart.discountOnFees', false)) {
$subTotal = $subTotal + app(LaraCart::SERVICE)->feeTotals(false);
}

return LaraCart::formatMoney(
app(LaraCart::SERVICE)->subTotal(false) * $this->value,
$subTotal * $this->value,
null,
null,
false
Expand Down
17 changes: 17 additions & 0 deletions tests/CouponsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,17 @@ public function testAddPercentageCoupon()
{
$this->addItem(3, 1);

try {
$percentCoupon = new LukePOLO\LaraCart\Coupons\Percentage('10%OFF', '23');
$this->setExpectedException(\LukePOLO\LaraCart\Exceptions\CouponException::class);
} catch (\LukePOLO\LaraCart\Exceptions\CouponException $e) {
$this->assertEquals('Invalid value for a percentage coupon. The value must be between 0 and 1.', $e->getMessage());
}

$percentCoupon = new LukePOLO\LaraCart\Coupons\Percentage('10%OFF', '.1');



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

$this->assertEquals($percentCoupon, $this->laracart->findCoupon('10%OFF'));
Expand Down Expand Up @@ -426,5 +435,13 @@ public function testFixedCouponWithFeeWithTotalLessThanCoupon()
$this->app['config']->set('laracart.discountOnFees', true);

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


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

$percentCoupon = new LukePOLO\LaraCart\Coupons\Percentage('100% Off', 1);
$this->laracart->addCoupon($percentCoupon);

$this->assertEquals(0, $this->laracart->total(false));
}
}

0 comments on commit ed4d082

Please sign in to comment.