fix issue in coupon handling
I found a little issue on handling coupons in cart. To reproduce this issue you have to do: - create three simple products - create a cart rule with coupon - assign conditions, so that sku of product 1 and product 2 will match the coupon - add product 1 to cart - add product 2 to cart - last add product 3 to cart (which has no coupon matching) - apply the coupon You get an error message. So therefore I made some little changes.
This commit is contained in:
parent
3cee45b4a5
commit
24dd893de1
|
|
@ -8,6 +8,7 @@ use Webkul\CartRule\Repositories\CartRuleCouponRepository;
|
|||
use Webkul\CartRule\Repositories\CartRuleCouponUsageRepository;
|
||||
use Webkul\CartRule\Repositories\CartRuleCustomerRepository;
|
||||
use Webkul\Customer\Repositories\CustomerGroupRepository;
|
||||
use Webkul\Checkout\Models\CartItem;
|
||||
use Webkul\Rule\Helpers\Validator;
|
||||
use Webkul\Checkout\Facades\Cart;
|
||||
|
||||
|
|
@ -101,16 +102,22 @@ class CartRule
|
|||
public function collect()
|
||||
{
|
||||
$cart = Cart::getCart();
|
||||
$appliedCartRuleIds = [];
|
||||
|
||||
$this->calculateCartItemTotals($cart->items()->get());
|
||||
|
||||
foreach ($cart->items()->get() as $item) {
|
||||
$this->process($item);
|
||||
$itemCartRuleIds = $this->process($item);
|
||||
$appliedCartRuleIds = array_merge($appliedCartRuleIds, $itemCartRuleIds);
|
||||
|
||||
if ($item->children()->count() && $item->product->getTypeInstance()->isChildrenCalculated())
|
||||
$this->devideDiscount($item);
|
||||
}
|
||||
|
||||
$cart->applied_cart_rule_ids = implode(',', array_unique($appliedCartRuleIds, SORT_REGULAR));
|
||||
$cart->save();
|
||||
$cart->refresh();
|
||||
|
||||
$this->processShippingDiscount($cart);
|
||||
|
||||
$this->processFreeShippingDiscount($cart);
|
||||
|
|
@ -176,7 +183,7 @@ class CartRule
|
|||
if ($coupon) {
|
||||
if ($coupon->usage_limit && $coupon->times_used >= $coupon->usage_limit)
|
||||
return false;
|
||||
|
||||
|
||||
if ($cart->customer_id && $coupon->usage_per_customer) {
|
||||
$couponUsage = $this->cartRuleCouponUsageRepository->findOneWhere([
|
||||
'cart_rule_coupon_id' => $coupon->id,
|
||||
|
|
@ -210,10 +217,10 @@ class CartRule
|
|||
/**
|
||||
* Cart item discount calculation process
|
||||
*
|
||||
* @param CartItem $item
|
||||
* @return void
|
||||
* @param \Webkul\Checkout\Models\CartItem $item
|
||||
* @return array
|
||||
*/
|
||||
public function process($item)
|
||||
public function process(CartItem $item): array
|
||||
{
|
||||
$item->discount_percent = 0;
|
||||
$item->discount_amount = 0;
|
||||
|
|
@ -313,15 +320,7 @@ class CartRule
|
|||
|
||||
$item->save();
|
||||
|
||||
$cartAppliedCartRuleIds = array_merge(explode(',', $cart->applied_cart_rule_ids), $appliedRuleIds);
|
||||
|
||||
$cartAppliedCartRuleIds = array_filter($cartAppliedCartRuleIds);
|
||||
|
||||
$cartAppliedCartRuleIds = array_unique($cartAppliedCartRuleIds);
|
||||
|
||||
$cart->applied_cart_rule_ids = join(',', $cartAppliedCartRuleIds);
|
||||
|
||||
$cart->save();
|
||||
return $appliedRuleIds;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -520,4 +519,4 @@ class CartRule
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue