From 216b409531c02a4dec0215220adefb1f319d711a Mon Sep 17 00:00:00 2001 From: Prashant Singh Date: Thu, 27 Jun 2019 17:09:56 +0530 Subject: [PATCH 1/8] Apply to shipping and free shipping now added in normal flow --- .../Webkul/Discount/src/Helpers/Discount.php | 30 +++++++++++++++++++ .../src/Helpers/ValidatesDiscount.php | 20 ------------- 2 files changed, 30 insertions(+), 20 deletions(-) diff --git a/packages/Webkul/Discount/src/Helpers/Discount.php b/packages/Webkul/Discount/src/Helpers/Discount.php index 5890daabf..7602313dc 100644 --- a/packages/Webkul/Discount/src/Helpers/Discount.php +++ b/packages/Webkul/Discount/src/Helpers/Discount.php @@ -145,6 +145,8 @@ abstract class Discount $this->clearDiscount(); + $this->applyOnShipping($rule, $cart); + $this->updateCartItemAndCart($rule); return true; @@ -158,6 +160,8 @@ abstract class Discount $this->clearDiscount(); + $this->applyOnShipping($rule, $cart); + $this->updateCartItemAndCart($rule); return true; @@ -587,4 +591,30 @@ abstract class Discount return $result; } + + /** + * Apply on shipping + */ + public function applyOnShipping($appliedRule, $cart) + { + if ($appliedRule->free_shipping && $cart->selected_shipping_rate->base_price > 0) { + $cart->selected_shipping_rate->update([ + 'price' => 0, + 'base_price' => 0 + ]); + } else if ($appliedRule->free_shipping == 0 && $appliedRule->apply_to_shipping && $cart->selected_shipping_rate->base_price > 0) { + $actionType = config('discount-rules')[$appliedRule->action_type]; + + if ($appliedRule->apply_to_shipping) { + $actionInstance = new $actionType; + + $discountOnShipping = $actionInstance->calculateOnShipping($cart); + + $cart->selected_shipping_rate->update([ + 'price' => $cart->selected_shipping_rate->base_price - $discountOnShipping, + 'base_price' => $cart->selected_shipping_rate->price - core()->convertPrice($discountOnShipping, $cart->cart_currency_code) + ]); + } + } + } } \ No newline at end of file diff --git a/packages/Webkul/Discount/src/Helpers/ValidatesDiscount.php b/packages/Webkul/Discount/src/Helpers/ValidatesDiscount.php index 34c7b7703..84e472b40 100644 --- a/packages/Webkul/Discount/src/Helpers/ValidatesDiscount.php +++ b/packages/Webkul/Discount/src/Helpers/ValidatesDiscount.php @@ -48,26 +48,6 @@ class ValidatesDiscount extends Discount if (! $applicability) { return $this->remove(); - } else { - if ($appliedRule->free_shipping && $cart->selected_shipping_rate->base_price > 0) { - $cart->selected_shipping_rate->update([ - 'price' => 0, - 'base_price' => 0 - ]); - } else if ($appliedRule->free_shipping == 0 && $appliedRule->apply_to_shipping && $cart->selected_shipping_rate->base_price > 0) { - $actionType = config('discount-rules')[$appliedRule->action_type]; - - if ($appliedRule->apply_to_shipping) { - $actionInstance = new $actionType; - - $discountOnShipping = $actionInstance->calculateOnShipping($cart); - - $cart->selected_shipping_rate->update([ - 'price' => $cart->selected_shipping_rate->base_price - $discountOnShipping, - 'base_price' => $cart->selected_shipping_rate->price - core()->convertPrice($discountOnShipping, $cart->cart_currency_code) - ]); - } - } } } else { return $this->remove(); From 4db41973fd882575368fbfa28c20d0d67eb796bf Mon Sep 17 00:00:00 2001 From: Prashant Singh Date: Thu, 27 Jun 2019 18:34:37 +0530 Subject: [PATCH 2/8] Removed the option of BuyAGetB as its current functionality was same as Fixed amount --- .../Discount/src/Config/rule-conditions.php | 2 +- .../Webkul/Discount/src/Helpers/Discount.php | 32 ++++++++++--------- .../src/Helpers/NonCouponAbleRule.php | 2 +- 3 files changed, 19 insertions(+), 17 deletions(-) diff --git a/packages/Webkul/Discount/src/Config/rule-conditions.php b/packages/Webkul/Discount/src/Config/rule-conditions.php index 99d170b7a..5974af80a 100644 --- a/packages/Webkul/Discount/src/Config/rule-conditions.php +++ b/packages/Webkul/Discount/src/Config/rule-conditions.php @@ -34,7 +34,7 @@ return [ 'actions' => [ 'percent_of_product' => 'Percentage of product', 'fixed_amount' => 'Apply as fixed amount', - 'buy_a_get_b' => 'Buy A get B' + // 'buy_a_get_b' => 'Buy A get B' ], 'validation' => [ diff --git a/packages/Webkul/Discount/src/Helpers/Discount.php b/packages/Webkul/Discount/src/Helpers/Discount.php index 7602313dc..f2846951a 100644 --- a/packages/Webkul/Discount/src/Helpers/Discount.php +++ b/packages/Webkul/Discount/src/Helpers/Discount.php @@ -597,23 +597,25 @@ abstract class Discount */ public function applyOnShipping($appliedRule, $cart) { - if ($appliedRule->free_shipping && $cart->selected_shipping_rate->base_price > 0) { - $cart->selected_shipping_rate->update([ - 'price' => 0, - 'base_price' => 0 - ]); - } else if ($appliedRule->free_shipping == 0 && $appliedRule->apply_to_shipping && $cart->selected_shipping_rate->base_price > 0) { - $actionType = config('discount-rules')[$appliedRule->action_type]; - - if ($appliedRule->apply_to_shipping) { - $actionInstance = new $actionType; - - $discountOnShipping = $actionInstance->calculateOnShipping($cart); - + if (isset($cart->selected_shipping_rate->base_price)) { + if ($appliedRule->free_shipping && $cart->selected_shipping_rate->base_price > 0) { $cart->selected_shipping_rate->update([ - 'price' => $cart->selected_shipping_rate->base_price - $discountOnShipping, - 'base_price' => $cart->selected_shipping_rate->price - core()->convertPrice($discountOnShipping, $cart->cart_currency_code) + 'price' => 0, + 'base_price' => 0 ]); + } else if ($appliedRule->free_shipping == 0 && $appliedRule->apply_to_shipping && $cart->selected_shipping_rate->base_price > 0) { + $actionType = config('discount-rules')[$appliedRule->action_type]; + + if ($appliedRule->apply_to_shipping) { + $actionInstance = new $actionType; + + $discountOnShipping = $actionInstance->calculateOnShipping($cart); + + $cart->selected_shipping_rate->update([ + 'price' => $cart->selected_shipping_rate->base_price - $discountOnShipping, + 'base_price' => $cart->selected_shipping_rate->price - core()->convertPrice($discountOnShipping, $cart->cart_currency_code) + ]); + } } } } diff --git a/packages/Webkul/Discount/src/Helpers/NonCouponAbleRule.php b/packages/Webkul/Discount/src/Helpers/NonCouponAbleRule.php index 11c54d095..4fd0b591b 100644 --- a/packages/Webkul/Discount/src/Helpers/NonCouponAbleRule.php +++ b/packages/Webkul/Discount/src/Helpers/NonCouponAbleRule.php @@ -117,7 +117,7 @@ class NonCouponAbleRule extends Discount if (count($endRules) == 1) { $this->save(array_first($endRules)['rule']); - return $endRules; + return array_first($endRules)['impact']; } $maxImpact = 0; From 25007824ebe45ca30c4a3c68c46166d0f4247ba6 Mon Sep 17 00:00:00 2001 From: Prashant Singh Date: Thu, 27 Jun 2019 19:51:17 +0530 Subject: [PATCH 3/8] Shipping now getting applied according to selected shipping method --- .../promotions/cart-rule/create.blade.php | 2 +- packages/Webkul/Checkout/src/Cart.php | 9 ++- .../Webkul/Discount/src/Helpers/Discount.php | 56 +++++++++++++++++-- .../Http/Controllers/OnepageController.php | 17 ++---- 4 files changed, 63 insertions(+), 21 deletions(-) diff --git a/packages/Webkul/Admin/src/Resources/views/promotions/cart-rule/create.blade.php b/packages/Webkul/Admin/src/Resources/views/promotions/cart-rule/create.blade.php index 933d49dd7..bcf6c699e 100644 --- a/packages/Webkul/Admin/src/Resources/views/promotions/cart-rule/create.blade.php +++ b/packages/Webkul/Admin/src/Resources/views/promotions/cart-rule/create.blade.php @@ -68,7 +68,7 @@
- + @{{ errors.first('ends_till') }}
diff --git a/packages/Webkul/Checkout/src/Cart.php b/packages/Webkul/Checkout/src/Cart.php index 795d7557c..e0c0f8efb 100755 --- a/packages/Webkul/Checkout/src/Cart.php +++ b/packages/Webkul/Checkout/src/Cart.php @@ -1116,7 +1116,8 @@ class Cart { * * Move a wishlist item to cart */ - public function moveToCart($wishlistItem) { + public function moveToCart($wishlistItem) + { $product = $wishlistItem->product; if ($product->type == 'simple') { @@ -1140,7 +1141,8 @@ class Cart { * * @param instance cartItem $id */ - public function moveToWishlist($itemId) { + public function moveToWishlist($itemId) + { $cart = $this->getCart(); $items = $cart->items; $wishlist = []; @@ -1187,7 +1189,8 @@ class Cart { * * @return response mixed */ - public function proceedToBuyNow($id, $quantity) { + public function proceedToBuyNow($id, $quantity) + { $product = $this->product->findOneByField('id', $id); if ($product->type == 'configurable') { diff --git a/packages/Webkul/Discount/src/Helpers/Discount.php b/packages/Webkul/Discount/src/Helpers/Discount.php index f2846951a..726e14585 100644 --- a/packages/Webkul/Discount/src/Helpers/Discount.php +++ b/packages/Webkul/Discount/src/Helpers/Discount.php @@ -145,14 +145,13 @@ abstract class Discount $this->clearDiscount(); - $this->applyOnShipping($rule, $cart); + $this->checkOnShipping(); $this->updateCartItemAndCart($rule); return true; } } else { - $this->cartRuleCart->create([ 'cart_id' => $cart->id, 'cart_rule_id' => $rule->id @@ -160,7 +159,7 @@ abstract class Discount $this->clearDiscount(); - $this->applyOnShipping($rule, $cart); + $this->checkOnShipping(); $this->updateCartItemAndCart($rule); @@ -170,6 +169,55 @@ abstract class Discount return false; } + /** + * Checks whether rule is getting applied on shipping or not + */ + public function checkOnShipping() + { + $cart = Cart::getCart(); + + if (! isset($cart->selected_shipping_rate)) { + return false; + } + + $shippingRate = config('carriers')[$cart->selected_shipping_rate->carrier]['class']; + + $actualShippingRate = new $shippingRate; + $actualShippingRate = $actualShippingRate->calculate(); + $actualShippingPrice = $actualShippingRate->price; + $actualShippingBasePrice = $actualShippingRate->base_price; + + $alreadyAppliedCartRuleCart = $this->cartRuleCart->findWhere([ + 'cart_id' => $cart->id + ]); + + if (count($alreadyAppliedCartRuleCart)) { + $alreadyAppliedRule = $alreadyAppliedCartRuleCart->first()->cart_rule; + + $cartShippingRate = $cart->selected_shipping_rate; + + if (isset($cartShippingRate)) { + if ($cartShippingRate->price < $actualShippingPrice) { + return false; + } else { + $cartShippingRate->update([ + 'price' => $actualShippingPrice, + 'base_price' => $actualShippingBasePrice + ]); + + $this->applyOnShipping($alreadyAppliedRule, $cart); + } + } else { + $this->applyOnShipping($alreadyAppliedRule, $cart); + } + } else { + $cartShippingRate->update([ + 'price' => $actualShippingPrice, + 'base_price' => $actualShippingBasePrice + ]); + } + } + /** * Removes any cart rule from the current cart instance * @@ -597,7 +645,7 @@ abstract class Discount */ public function applyOnShipping($appliedRule, $cart) { - if (isset($cart->selected_shipping_rate->base_price)) { + if (isset($cart->selected_shipping_rate)) { if ($appliedRule->free_shipping && $cart->selected_shipping_rate->base_price > 0) { $cart->selected_shipping_rate->update([ 'price' => 0, diff --git a/packages/Webkul/Shop/src/Http/Controllers/OnepageController.php b/packages/Webkul/Shop/src/Http/Controllers/OnepageController.php index a2306085b..fd04c36a0 100755 --- a/packages/Webkul/Shop/src/Http/Controllers/OnepageController.php +++ b/packages/Webkul/Shop/src/Http/Controllers/OnepageController.php @@ -147,6 +147,8 @@ class OnepageController extends Controller $this->nonCoupon->apply(); + $this->nonCoupon->checkOnShipping(); + Cart::collectTotals(); return response()->json(Payment::getSupportedPaymentMethods()); @@ -166,6 +168,8 @@ class OnepageController extends Controller $this->nonCoupon->apply(); + $this->nonCoupon->checkOnShipping(); + Cart::collectTotals(); $cart = Cart::getCart(); @@ -285,19 +289,6 @@ class OnepageController extends Controller return $result; } - /** - * Applies non couponable rule if present - * - * @return Void - */ - public function applyNonCouponAbleRule() - { - $cart = Cart::getCart(); - $nonCouponAbleRules = Cart::applyNonCoupon(); - - return $nonCouponAbleRules; - } - /** * Initiates the removal of couponable cart rule * From 4339475e7014e30858a0ea4bc630ee8dd2b5d98b Mon Sep 17 00:00:00 2001 From: Prashant Singh Date: Thu, 27 Jun 2019 19:55:36 +0530 Subject: [PATCH 4/8] CheckOnShipping method added to check where rule applies on shipping or not --- packages/Webkul/Discount/src/Helpers/Discount.php | 2 ++ .../Webkul/Shop/src/Http/Controllers/OnepageController.php | 4 ---- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/packages/Webkul/Discount/src/Helpers/Discount.php b/packages/Webkul/Discount/src/Helpers/Discount.php index 726e14585..83adb0d58 100644 --- a/packages/Webkul/Discount/src/Helpers/Discount.php +++ b/packages/Webkul/Discount/src/Helpers/Discount.php @@ -150,6 +150,8 @@ abstract class Discount $this->updateCartItemAndCart($rule); return true; + } else { + $this->checkOnShipping(); } } else { $this->cartRuleCart->create([ diff --git a/packages/Webkul/Shop/src/Http/Controllers/OnepageController.php b/packages/Webkul/Shop/src/Http/Controllers/OnepageController.php index fd04c36a0..37784b360 100755 --- a/packages/Webkul/Shop/src/Http/Controllers/OnepageController.php +++ b/packages/Webkul/Shop/src/Http/Controllers/OnepageController.php @@ -147,8 +147,6 @@ class OnepageController extends Controller $this->nonCoupon->apply(); - $this->nonCoupon->checkOnShipping(); - Cart::collectTotals(); return response()->json(Payment::getSupportedPaymentMethods()); @@ -168,8 +166,6 @@ class OnepageController extends Controller $this->nonCoupon->apply(); - $this->nonCoupon->checkOnShipping(); - Cart::collectTotals(); $cart = Cart::getCart(); From e4612d86291f0118b35a667a58e69f6ea502c440 Mon Sep 17 00:00:00 2001 From: Prashant Singh Date: Fri, 28 Jun 2019 10:56:44 +0530 Subject: [PATCH 5/8] Fixed some discrepancies in apply to shipping in cart rules --- .../Discount/src/Actions/FixedAmount.php | 6 +- .../Discount/src/Actions/PercentOfProduct.php | 6 +- .../Discount/src/Helpers/CouponAbleRule.php | 2 + .../Webkul/Discount/src/Helpers/Discount.php | 107 ++++++++++-------- 4 files changed, 69 insertions(+), 52 deletions(-) diff --git a/packages/Webkul/Discount/src/Actions/FixedAmount.php b/packages/Webkul/Discount/src/Actions/FixedAmount.php index 2b5a200f9..ce3422524 100644 --- a/packages/Webkul/Discount/src/Actions/FixedAmount.php +++ b/packages/Webkul/Discount/src/Actions/FixedAmount.php @@ -43,10 +43,10 @@ class FixedAmount extends Action */ public function calculateOnShipping($cart) { + $cart = \Cart::getCart(); + $percentOfDiscount = ($cart->base_discount_amount * 100) / $cart->base_grand_total; - $discountOnShipping = ($percentOfDiscount / 100) * $cart->selected_shipping_rate->base_price; - - return $discountOnShipping; + return $percentOfDiscount; } } \ No newline at end of file diff --git a/packages/Webkul/Discount/src/Actions/PercentOfProduct.php b/packages/Webkul/Discount/src/Actions/PercentOfProduct.php index 79683be5e..26e1e091c 100644 --- a/packages/Webkul/Discount/src/Actions/PercentOfProduct.php +++ b/packages/Webkul/Discount/src/Actions/PercentOfProduct.php @@ -40,10 +40,10 @@ class PercentOfProduct extends Action */ public function calculateOnShipping($cart) { + $cart = \Cart::getCart(); + $percentOfDiscount = ($cart->base_discount_amount * 100) / $cart->base_grand_total; - $discountOnShipping = ($percentOfDiscount / 100) * $cart->selected_shipping_rate->base_price; - - return $discountOnShipping; + return $percentOfDiscount; } } \ No newline at end of file diff --git a/packages/Webkul/Discount/src/Helpers/CouponAbleRule.php b/packages/Webkul/Discount/src/Helpers/CouponAbleRule.php index f46c4c5f9..11ab89d40 100644 --- a/packages/Webkul/Discount/src/Helpers/CouponAbleRule.php +++ b/packages/Webkul/Discount/src/Helpers/CouponAbleRule.php @@ -136,6 +136,8 @@ class CouponAbleRule extends Discount if ($existingRule->count()) { $existingRule->first()->delete(); + $this->resetShipping($cart); + foreach ($cart->items as $item) { if ($item->discount_amount > 0) { $item->update([ diff --git a/packages/Webkul/Discount/src/Helpers/Discount.php b/packages/Webkul/Discount/src/Helpers/Discount.php index 83adb0d58..4479a254d 100644 --- a/packages/Webkul/Discount/src/Helpers/Discount.php +++ b/packages/Webkul/Discount/src/Helpers/Discount.php @@ -145,13 +145,13 @@ abstract class Discount $this->clearDiscount(); - $this->checkOnShipping(); - $this->updateCartItemAndCart($rule); + $this->checkOnShipping($cart); + return true; } else { - $this->checkOnShipping(); + $this->checkOnShipping($cart); } } else { $this->cartRuleCart->create([ @@ -161,10 +161,10 @@ abstract class Discount $this->clearDiscount(); - $this->checkOnShipping(); - $this->updateCartItemAndCart($rule); + $this->checkOnShipping($cart); + return true; } @@ -174,10 +174,8 @@ abstract class Discount /** * Checks whether rule is getting applied on shipping or not */ - public function checkOnShipping() + public function checkOnShipping($cart) { - $cart = Cart::getCart(); - if (! isset($cart->selected_shipping_rate)) { return false; } @@ -194,32 +192,77 @@ abstract class Discount ]); if (count($alreadyAppliedCartRuleCart)) { + $this->resetShipping($cart); + $alreadyAppliedRule = $alreadyAppliedCartRuleCart->first()->cart_rule; $cartShippingRate = $cart->selected_shipping_rate; if (isset($cartShippingRate)) { - if ($cartShippingRate->price < $actualShippingPrice) { + if ($cartShippingRate->base_price < $actualShippingBasePrice) { return false; } else { - $cartShippingRate->update([ - 'price' => $actualShippingPrice, - 'base_price' => $actualShippingBasePrice - ]); - $this->applyOnShipping($alreadyAppliedRule, $cart); } } else { $this->applyOnShipping($alreadyAppliedRule, $cart); } } else { - $cartShippingRate->update([ - 'price' => $actualShippingPrice, - 'base_price' => $actualShippingBasePrice - ]); + $this->resetShipping($cart); } } + /** + * Apply on shipping + * + * @return void + */ + public function applyOnShipping($appliedRule, $cart) + { + if (isset($cart->selected_shipping_rate)) { + if ($appliedRule->free_shipping && $cart->selected_shipping_rate->base_price > 0) { + $cart->selected_shipping_rate->update([ + 'price' => 0, + 'base_price' => 0 + ]); + } else if ($appliedRule->free_shipping == 0 && $appliedRule->apply_to_shipping && $cart->selected_shipping_rate->base_price > 0) { + $actionType = config('discount-rules')[$appliedRule->action_type]; + + if ($appliedRule->apply_to_shipping) { + $actionInstance = new $actionType; + + $discountOnShipping = $actionInstance->calculateOnShipping($cart); + + $discountOnShipping = ($discountOnShipping / 100) * $cart->selected_shipping_rate->base_price; + + $cart->selected_shipping_rate->update([ + 'price' => $cart->selected_shipping_rate->price - core()->convertPrice($discountOnShipping, $cart->cart_currency_code), + 'base_price' => $cart->selected_shipping_rate->base_price - $discountOnShipping + ]); + } + } + } + } + + /** + * Resets the shipping for the current items in the cart + */ + public function resetShipping($cart) + { + $shippingRate = config('carriers')[$cart->selected_shipping_rate->carrier]['class']; + + $actualShippingRate = new $shippingRate; + $actualShippingRate = $actualShippingRate->calculate(); + $actualShippingPrice = $actualShippingRate->price; + $actualShippingBasePrice = $actualShippingRate->base_price; + $cartShippingRate = $cart->selected_shipping_rate; + + $cartShippingRate->update([ + 'price' => $actualShippingPrice, + 'base_price' => $actualShippingBasePrice + ]); + } + /** * Removes any cart rule from the current cart instance * @@ -641,32 +684,4 @@ abstract class Discount return $result; } - - /** - * Apply on shipping - */ - public function applyOnShipping($appliedRule, $cart) - { - if (isset($cart->selected_shipping_rate)) { - if ($appliedRule->free_shipping && $cart->selected_shipping_rate->base_price > 0) { - $cart->selected_shipping_rate->update([ - 'price' => 0, - 'base_price' => 0 - ]); - } else if ($appliedRule->free_shipping == 0 && $appliedRule->apply_to_shipping && $cart->selected_shipping_rate->base_price > 0) { - $actionType = config('discount-rules')[$appliedRule->action_type]; - - if ($appliedRule->apply_to_shipping) { - $actionInstance = new $actionType; - - $discountOnShipping = $actionInstance->calculateOnShipping($cart); - - $cart->selected_shipping_rate->update([ - 'price' => $cart->selected_shipping_rate->base_price - $discountOnShipping, - 'base_price' => $cart->selected_shipping_rate->price - core()->convertPrice($discountOnShipping, $cart->cart_currency_code) - ]); - } - } - } - } } \ No newline at end of file From 02faaf7415463b6f6efe72c29d8e024835aed743 Mon Sep 17 00:00:00 2001 From: Prashant Singh Date: Fri, 28 Jun 2019 11:30:01 +0530 Subject: [PATCH 6/8] Added reset shipping when applying coupon cart rule --- packages/Webkul/Discount/src/Helpers/Discount.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/packages/Webkul/Discount/src/Helpers/Discount.php b/packages/Webkul/Discount/src/Helpers/Discount.php index 4479a254d..e8f471d22 100644 --- a/packages/Webkul/Discount/src/Helpers/Discount.php +++ b/packages/Webkul/Discount/src/Helpers/Discount.php @@ -137,6 +137,10 @@ abstract class Discount 'cart_id' => $cart->id ]); + if ($rule->use_coupon) { + $this->resetShipping($cart); + } + if (count($existingRule)) { if ($existingRule->first()->cart_rule_id != $rule->id) { $existingRule->first()->update([ @@ -246,9 +250,13 @@ abstract class Discount /** * Resets the shipping for the current items in the cart + * + * @return void */ public function resetShipping($cart) { + $cart = \Cart::getCart(); + $shippingRate = config('carriers')[$cart->selected_shipping_rate->carrier]['class']; $actualShippingRate = new $shippingRate; From e8dd4219a723b7dadec830d6635b4411b0c7f85e Mon Sep 17 00:00:00 2001 From: Prashant Singh Date: Fri, 28 Jun 2019 12:57:19 +0530 Subject: [PATCH 7/8] Apply on shipping working on cart rule --- packages/Webkul/Discount/src/Actions/BuyAGetB.php | 2 +- .../Webkul/Discount/src/Actions/FixedAmount.php | 2 +- .../Discount/src/Actions/PercentOfProduct.php | 2 +- packages/Webkul/Discount/src/Helpers/Discount.php | 14 ++++++++++---- .../src/Http/Controllers/OnepageController.php | 2 ++ 5 files changed, 15 insertions(+), 7 deletions(-) diff --git a/packages/Webkul/Discount/src/Actions/BuyAGetB.php b/packages/Webkul/Discount/src/Actions/BuyAGetB.php index f1e47aa90..99add92fc 100644 --- a/packages/Webkul/Discount/src/Actions/BuyAGetB.php +++ b/packages/Webkul/Discount/src/Actions/BuyAGetB.php @@ -27,7 +27,7 @@ class BuyAGetB extends Action $amountDiscounted = $amountDiscounted * $realQty; } - if ($amountDiscounted > $item['base_price']) { + if ($amountDiscounted > $item['base_price'] && $realQty == 1) { $amountDiscounted = $item['base_price']; } } diff --git a/packages/Webkul/Discount/src/Actions/FixedAmount.php b/packages/Webkul/Discount/src/Actions/FixedAmount.php index ce3422524..5f6209c27 100644 --- a/packages/Webkul/Discount/src/Actions/FixedAmount.php +++ b/packages/Webkul/Discount/src/Actions/FixedAmount.php @@ -27,7 +27,7 @@ class FixedAmount extends Action $amountDiscounted = $amountDiscounted * $realQty; } - if ($amountDiscounted > $item['base_price']) { + if ($amountDiscounted > $item['base_price'] && $realQty == 1) { $amountDiscounted = $item['base_price']; } } diff --git a/packages/Webkul/Discount/src/Actions/PercentOfProduct.php b/packages/Webkul/Discount/src/Actions/PercentOfProduct.php index 26e1e091c..6d3ee3772 100644 --- a/packages/Webkul/Discount/src/Actions/PercentOfProduct.php +++ b/packages/Webkul/Discount/src/Actions/PercentOfProduct.php @@ -24,7 +24,7 @@ class PercentOfProduct extends Action $amountDiscounted = $amountDiscounted * $realQty; } - if ($amountDiscounted > $item['base_price']) { + if ($amountDiscounted > $item['base_price'] && $realQty == 1) { $amountDiscounted = $item['base_price']; } } diff --git a/packages/Webkul/Discount/src/Helpers/Discount.php b/packages/Webkul/Discount/src/Helpers/Discount.php index e8f471d22..180b602b2 100644 --- a/packages/Webkul/Discount/src/Helpers/Discount.php +++ b/packages/Webkul/Discount/src/Helpers/Discount.php @@ -132,7 +132,7 @@ abstract class Discount { $cart = \Cart::getCart(); - // create or update + // Create or update $existingRule = $this->cartRuleCart->findWhere([ 'cart_id' => $cart->id ]); @@ -151,11 +151,13 @@ abstract class Discount $this->updateCartItemAndCart($rule); - $this->checkOnShipping($cart); + if ($rule->use_coupon) { + $this->checkOnShipping($cart); + } return true; } else { - $this->checkOnShipping($cart); + // $this->checkOnShipping($cart); } } else { $this->cartRuleCart->create([ @@ -167,7 +169,9 @@ abstract class Discount $this->updateCartItemAndCart($rule); - $this->checkOnShipping($cart); + if ($rule->use_coupon) { + $this->checkOnShipping($cart); + } return true; } @@ -223,6 +227,8 @@ abstract class Discount */ public function applyOnShipping($appliedRule, $cart) { + $cart = \Cart::getCart(); + if (isset($cart->selected_shipping_rate)) { if ($appliedRule->free_shipping && $cart->selected_shipping_rate->base_price > 0) { $cart->selected_shipping_rate->update([ diff --git a/packages/Webkul/Shop/src/Http/Controllers/OnepageController.php b/packages/Webkul/Shop/src/Http/Controllers/OnepageController.php index 37784b360..bfe3abe71 100755 --- a/packages/Webkul/Shop/src/Http/Controllers/OnepageController.php +++ b/packages/Webkul/Shop/src/Http/Controllers/OnepageController.php @@ -166,6 +166,8 @@ class OnepageController extends Controller $this->nonCoupon->apply(); + $this->nonCoupon->checkOnShipping(Cart::getCart()); + Cart::collectTotals(); $cart = Cart::getCart(); From a304795c36a771f670128a8d2ac974d23b1aee4b Mon Sep 17 00:00:00 2001 From: Prashant Singh Date: Fri, 28 Jun 2019 12:59:40 +0530 Subject: [PATCH 8/8] Fixed issues with buy atleast --- .../Webkul/Discount/src/Helpers/Discount.php | 22 ++++++++++--------- .../src/Helpers/NonCouponAbleRule.php | 2 ++ 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/packages/Webkul/Discount/src/Helpers/Discount.php b/packages/Webkul/Discount/src/Helpers/Discount.php index 180b602b2..d344df504 100644 --- a/packages/Webkul/Discount/src/Helpers/Discount.php +++ b/packages/Webkul/Discount/src/Helpers/Discount.php @@ -263,18 +263,20 @@ abstract class Discount { $cart = \Cart::getCart(); - $shippingRate = config('carriers')[$cart->selected_shipping_rate->carrier]['class']; + if (isset($cart->selected_shipping_rate->carrier)) { + $shippingRate = config('carriers')[$cart->selected_shipping_rate->carrier]['class']; - $actualShippingRate = new $shippingRate; - $actualShippingRate = $actualShippingRate->calculate(); - $actualShippingPrice = $actualShippingRate->price; - $actualShippingBasePrice = $actualShippingRate->base_price; - $cartShippingRate = $cart->selected_shipping_rate; + $actualShippingRate = new $shippingRate; + $actualShippingRate = $actualShippingRate->calculate(); + $actualShippingPrice = $actualShippingRate->price; + $actualShippingBasePrice = $actualShippingRate->base_price; + $cartShippingRate = $cart->selected_shipping_rate; - $cartShippingRate->update([ - 'price' => $actualShippingPrice, - 'base_price' => $actualShippingBasePrice - ]); + $cartShippingRate->update([ + 'price' => $actualShippingPrice, + 'base_price' => $actualShippingBasePrice + ]); + } } /** diff --git a/packages/Webkul/Discount/src/Helpers/NonCouponAbleRule.php b/packages/Webkul/Discount/src/Helpers/NonCouponAbleRule.php index 4fd0b591b..8144f9046 100644 --- a/packages/Webkul/Discount/src/Helpers/NonCouponAbleRule.php +++ b/packages/Webkul/Discount/src/Helpers/NonCouponAbleRule.php @@ -37,6 +37,8 @@ class NonCouponAbleRule extends Discount // if the validation fails then the cart rule gets deleted from cart rule cart $alreadyAppliedCartRuleCart->first()->delete(); + $this->resetShipping(); + // all discount is cleared fro mthe cart and cart items table $this->clearDiscount();