Skip to content

Commit

Permalink
Change Coupons_history_model::createHistory method signature to objec…
Browse files Browse the repository at this point in the history
…t instead of cartcondition instance
  • Loading branch information
sampoyigi committed May 27, 2024
1 parent 657b59c commit c74ba68
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 22 deletions.
4 changes: 2 additions & 2 deletions Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public function boot()
});

Event::listen('igniter.checkout.afterSaveOrder', function ($order) {
if ($couponCondition = Cart::conditions()->get('coupon')) {
$order->logCouponHistory($couponCondition);
if ($couponTotal = $order->getOrderTotals()->firstWhere('code', 'coupon')) {
$order->logCouponHistory($couponTotal);
}
});

Expand Down
15 changes: 3 additions & 12 deletions actions/RedeemsCoupon.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,16 @@ public function redeemCoupon()
/**
* Add cart coupon to order by order_id
*
* @param \Admin\Models\Orders_model $order
* @param \Igniter\Flame\Cart\CartCondition $couponCondition
* @param \Admin\Models\Customers_model $customer
* @param object $couponTotal
*
* @return int|bool
*/
public function logCouponHistory($couponCondition)
public function logCouponHistory($couponTotal)
{
if (!$couponCondition instanceof CartCondition) {
throw new \InvalidArgumentException(sprintf(
'Invalid argument, expected %s, got %s',
CartCondition::class, get_class($couponCondition)
));
}

// Make sure order model exists
if (!$this->model->exists)
return false;

return Coupons_history_model::createHistory($couponCondition, $this->model);
return Coupons_history_model::createHistory($couponTotal, $this->model);
}
}
4 changes: 1 addition & 3 deletions cartconditions/Coupon.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,7 @@ protected function validateCoupon($couponModel)
if ($couponModel->hasReachedMaxRedemption())
throw new ApplicationException(lang('igniter.cart::default.alert_coupon_maximum_reached'));

if (($couponModel->customer_redemptions
|| optional($couponModel->customers)->isNotEmpty()
|| optional($couponModel->customer_groups)->isNotEmpty()) && !$user
if ((optional($couponModel->customers)->isNotEmpty() || optional($couponModel->customer_groups)->isNotEmpty()) && !$user
) throw new ApplicationException(lang('igniter.coupons::default.alert_coupon_login_required'));

if ($user && $couponModel->customerHasMaxRedemption($user))
Expand Down
14 changes: 9 additions & 5 deletions models/Coupons_history_model.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,24 +121,28 @@ public function touchStatus()
}

/**
* @param \Igniter\Flame\Cart\CartCondition $couponCondition
* @param object $couponTotal
* @param \Admin\Models\Orders_model $order
* @return \Admin\Models\Coupons_history_model|bool
*/
public static function createHistory($couponCondition, $order)
public static function createHistory($couponTotal, $order)
{
if (!$coupon = $couponCondition->getModel())
if ($couponTotal->code === 'coupon' && $couponTotal->title) {
$couponTotal->code = str_after(str_before($couponTotal->title, ']'), '[');
}

if (!$coupon = Coupons_model::firstWhere('code', $couponTotal->code))
return false;

$model = new static;
$model->order_id = $order->getKey();
$model->customer_id = $order->customer ? $order->customer->getKey() : null;
$model->coupon_id = $coupon->coupon_id;
$model->code = $coupon->code;
$model->amount = $couponCondition->getValue();
$model->amount = $couponTotal->value;
$model->min_total = $coupon->min_total;

if ($model->fireSystemEvent('couponHistory.beforeAddHistory', [$model, $couponCondition, $order->customer, $coupon], true) === false)
if ($model->fireSystemEvent('couponHistory.beforeAddHistory', [$model, $couponTotal, $order->customer, $coupon], true) === false)
return false;

$model->save();
Expand Down

0 comments on commit c74ba68

Please sign in to comment.