Skip to content

Commit

Permalink
Check min order total before applying coupons
Browse files Browse the repository at this point in the history
Signed-off-by: Sam Poyigi <[email protected]>
  • Loading branch information
sampoyigi committed Dec 3, 2023
1 parent ce3bfad commit fb7fff7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
32 changes: 16 additions & 16 deletions cartconditions/Coupon.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class Coupon extends CartCondition

protected static $applicableItems;

protected static $hasErrors = false;

public function getLabel()
{
return sprintf(lang($this->label), $this->getMetaData('code'));
Expand Down Expand Up @@ -64,7 +66,7 @@ public function getApplicableItems($couponModel)

public function onLoad()
{
if (!strlen($couponCode = $this->getMetaData('code')))
if (!strlen($this->getMetaData('code')) || self::$hasErrors)
return;

try {
Expand All @@ -75,8 +77,7 @@ public function onLoad()

$this->getApplicableItems($couponModel);
} catch (Exception $ex) {
if (!optional($couponModel)->auto_apply)
flash()->alert($ex->getMessage())->now();
flash()->alert($ex->getMessage())->now();

$this->removeMetaData('code');
}
Expand All @@ -85,21 +86,21 @@ public function onLoad()
public function beforeApply()
{
$couponModel = $this->getModel();
if (!$couponModel || $couponModel->apply_coupon_on == 'menu_items')
if (!$couponModel || $couponModel->appliesOnMenuItems() || self::$hasErrors)
return false;

if ($couponModel->apply_coupon_on == 'delivery_fee' && !Location::orderTypeIsDelivery())
if ($couponModel->appliesOnDelivery() && !Location::orderTypeIsDelivery())
return false;
}

public function getActions()
{
$value = optional($this->getModel())->discountWithOperand();

if (optional($this->getModel())->apply_coupon_on == 'delivery_fee') {
if (optional($this->getModel())->appliesOnDelivery()) {
$value = $this->calculateDeliveryDiscount();
} // if we are item limited and not a % we need to apportion
elseif (stripos($value, '%') === false && optional($this->getModel())->apply_coupon_on == 'menu_items') {
elseif (stripos($value, '%') === false && optional($this->getModel())->appliesOnMenuItems()) {
$value = $this->calculateApportionment($value);
}

Expand All @@ -110,13 +111,6 @@ public function getActions()
return [$actions];
}

public function getRules()
{
$minimumOrder = optional($this->getModel())->minimumOrderTotal();

return ["subtotal > {$minimumOrder}"];
}

public function whenInvalid()
{
if (!$this->getModel()->auto_apply) {
Expand Down Expand Up @@ -184,6 +178,12 @@ protected function validateCoupon($couponModel)
if ($couponModel->hasLocationRestriction($locationId))
throw new ApplicationException(lang('igniter.cart::default.alert_coupon_location_restricted'));

if (Cart::subtotal() < $couponModel->minimumOrderTotal())
throw new ApplicationException(sprintf(
lang('igniter.cart::default.alert_coupon_not_applied'),
currency_format($couponModel->minimumOrderTotal())
));

if ($couponModel->hasReachedMaxRedemption())
throw new ApplicationException(lang('igniter.cart::default.alert_coupon_maximum_reached'));

Expand All @@ -204,10 +204,10 @@ protected function validateCoupon($couponModel)

public static function isApplicableTo($cartItem)
{
if (!$couponModel = self::$couponModel)
if (!($couponModel = self::$couponModel) || self::$hasErrors)
return false;

if ($couponModel->apply_coupon_on != 'menu_items')
if (!$couponModel->appliesOnMenuItems())
return false;

if (!$applicableItems = self::$applicableItems)
Expand Down
15 changes: 15 additions & 0 deletions models/Coupons_model.php
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,21 @@ public function countCustomerRedemptions($id)
->where('customer_id', $id)->count();
}

public function appliesOnWholeCart()
{
return $this->apply_coupon_on == 'whole_cart';
}

public function appliesOnMenuItems()
{
return $this->apply_coupon_on == 'menu_items';
}

public function appliesOnDelivery()
{
return $this->apply_coupon_on == 'delivery_fee';
}

public static function getByCode($code)
{
return self::isEnabled()->whereCode($code)->first();
Expand Down

0 comments on commit fb7fff7

Please sign in to comment.