From 24dd893de1ad1878ef917ccf686bd002e4eac8f3 Mon Sep 17 00:00:00 2001 From: SteffenMahler <57138594+SteffenMahler@users.noreply.github.com> Date: Tue, 25 Feb 2020 09:33:47 +0100 Subject: [PATCH 1/3] 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. --- .../Webkul/CartRule/src/Helpers/CartRule.php | 29 +++++++++---------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/packages/Webkul/CartRule/src/Helpers/CartRule.php b/packages/Webkul/CartRule/src/Helpers/CartRule.php index 8ad0c4dac..8545397da 100644 --- a/packages/Webkul/CartRule/src/Helpers/CartRule.php +++ b/packages/Webkul/CartRule/src/Helpers/CartRule.php @@ -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 } } } -} \ No newline at end of file +} From c7cd5f5f5123ef26d80e9fcb2965209915bf3a3c Mon Sep 17 00:00:00 2001 From: Steffen Mahler Date: Tue, 25 Feb 2020 10:09:00 +0100 Subject: [PATCH 2/3] remove line --- packages/Webkul/CartRule/src/Helpers/CartRule.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/Webkul/CartRule/src/Helpers/CartRule.php b/packages/Webkul/CartRule/src/Helpers/CartRule.php index 8545397da..75dbe7ce0 100644 --- a/packages/Webkul/CartRule/src/Helpers/CartRule.php +++ b/packages/Webkul/CartRule/src/Helpers/CartRule.php @@ -228,8 +228,6 @@ class CartRule $cart = $item->cart; - $cart->applied_cart_rule_ids = null; - $appliedRuleIds = []; foreach ($this->getCartRules() as $rule) { From d3c768297403c3a96e3ffef866d7db9d7939e110 Mon Sep 17 00:00:00 2001 From: Steffen Mahler Date: Tue, 25 Feb 2020 10:33:13 +0100 Subject: [PATCH 3/3] remove unnecessary line --- packages/Webkul/CartRule/src/Helpers/CartRule.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/Webkul/CartRule/src/Helpers/CartRule.php b/packages/Webkul/CartRule/src/Helpers/CartRule.php index 75dbe7ce0..a67c69a44 100644 --- a/packages/Webkul/CartRule/src/Helpers/CartRule.php +++ b/packages/Webkul/CartRule/src/Helpers/CartRule.php @@ -226,8 +226,6 @@ class CartRule $item->discount_amount = 0; $item->base_discount_amount = 0; - $cart = $item->cart; - $appliedRuleIds = []; foreach ($this->getCartRules() as $rule) {