From 25007824ebe45ca30c4a3c68c46166d0f4247ba6 Mon Sep 17 00:00:00 2001 From: Prashant Singh Date: Thu, 27 Jun 2019 19:51:17 +0530 Subject: [PATCH] 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 *