From c52238e45eee0e22206f3a4b4ad9679e20a55773 Mon Sep 17 00:00:00 2001 From: Prashant Singh Date: Fri, 14 Jun 2019 13:21:17 +0530 Subject: [PATCH] Cart rule frontend normalization --- packages/Webkul/Admin/src/Config/system.php | 18 ----- packages/Webkul/Checkout/src/Cart.php | 16 ++--- .../Webkul/Discount/src/Helpers/Discount.php | 67 +++++++++++++++++-- .../src/Http/Controllers/CartController.php | 16 +---- .../Http/Controllers/OnepageController.php | 14 +--- .../views/checkout/onepage.blade.php | 2 + .../views/checkout/total/summary.blade.php | 9 ++- 7 files changed, 82 insertions(+), 60 deletions(-) diff --git a/packages/Webkul/Admin/src/Config/system.php b/packages/Webkul/Admin/src/Config/system.php index cf4f86667..0e9c88925 100644 --- a/packages/Webkul/Admin/src/Config/system.php +++ b/packages/Webkul/Admin/src/Config/system.php @@ -79,24 +79,6 @@ return [ 'type' => 'boolean' ] ], - ], [ - 'key' => 'customer.settings.credit-max', - 'name' => 'admin::app.admin.system.credit-max', - 'sort' => 4, - 'fields' => [ - [ - 'name' => 'Use Credit Max', - 'title' => 'admin::app.admin.system.use-credit-max', - 'type' => 'boolean', - 'channel_based' => true - ], [ - 'name' => 'Credit Max Value', - 'title' => 'admin::app.admin.system.credit-max-value', - 'type' => 'text', - 'validation' => 'decimal', - 'channel_based' => true - ] - ], ], [ 'key' => 'general', 'name' => 'admin::app.admin.system.general', diff --git a/packages/Webkul/Checkout/src/Cart.php b/packages/Webkul/Checkout/src/Cart.php index ee7a23625..4f3a30ab0 100755 --- a/packages/Webkul/Checkout/src/Cart.php +++ b/packages/Webkul/Checkout/src/Cart.php @@ -1305,13 +1305,13 @@ class Cart { * * @return void */ - public function removeDiscount() + public function clearDiscount() { $cartItems = $this->getCart()->items; foreach($cartItems as $item) { $item->update([ - 'coupon_code' => null, + 'coupon_code' => NULL, 'discount_percent' => 0, 'discount_amount' => 0, 'base_discount_amount' => 0 @@ -1319,7 +1319,7 @@ class Cart { } $this->getCart()->update([ - 'coupon_code' => null, + 'coupon_code' => NULL, 'discount_amount' => 0, 'base_discount_amount' => 0 ]); @@ -1327,12 +1327,12 @@ class Cart { return true; } - public function removeCoupon() - { - $result = $this->discount->removeCoupon(); + // public function removeCoupon() + // { + // $result = $this->discount->removeCoupon(); - return $result; - } + // return $result; + // } public function leastWorthItem() { diff --git a/packages/Webkul/Discount/src/Helpers/Discount.php b/packages/Webkul/Discount/src/Helpers/Discount.php index 3fb200e42..e033433af 100644 --- a/packages/Webkul/Discount/src/Helpers/Discount.php +++ b/packages/Webkul/Discount/src/Helpers/Discount.php @@ -38,6 +38,23 @@ class Discount public function applyNonCouponAbleRule() { + $cart = \Cart::getCart(); + + $previousRule = $this->cartRuleCart->findWhere([ + 'cart_id' => $cart->id + ]); + + if ($previousRule->count()) { + $previousRule = $previousRule->first()->cart_rule; + + if ($previousRule->use_coupon) { + + return 'false'; + } + } else { + \Cart::clearDiscount(); + } + if (auth()->guard('customer')->check()) { $nonCouponAbleRules = $this->cartRule->findWhere([ 'use_coupon' => 0, @@ -104,6 +121,17 @@ class Discount if ($rule['rule']->id == $leastId) { $rule['used_coupon'] = false; + foreach (\Cart::getCart()->items as $item) { + if ($item->id == $itemId['item_id']) { + $item->update([ + 'discount_amount' => array_first($canBeApplied)['discount'], + 'base_discount_amount' => array_first($canBeApplied)['discount'] + ]); + + break; + } + } + $this->save($rule['rule']); return $rule; @@ -113,8 +141,6 @@ class Discount } if ($canBeApplied->count()) { - $this->save(array_first($canBeApplied)['rule']); - $itemId = array_first($canBeApplied); foreach (\Cart::getCart()->items as $item) { @@ -128,14 +154,18 @@ class Discount } } + $this->save(array_first($canBeApplied)['rule']); + return array_first($canBeApplied); } else { - return false; + return 'false'; } } public function applyCouponAbleRule($code) { + $cart = \Cart::getCart(); + if (auth()->guard('customer')->check()) { $couponAbleRules = $this->cartRule->findWhere([ 'use_coupon' => 1, @@ -256,13 +286,20 @@ class Discount $itemId = array_first($canBeApplied); - foreach (\Cart::getCart()->items as $item) { + foreach ($cart->items as $item) { if ($item->id == $itemId['item_id']) { $item->update([ 'discount_amount' => array_first($canBeApplied)['discount'], - 'base_discount_amount' => array_first($canBeApplied)['discount'] + 'base_discount_amount' => array_first($canBeApplied)['discount'], + 'coupon_code' => $rule->coupons->code ]); + $cart->update([ + 'coupon_code' => $rule->coupons->code + ]); + + \Cart::collectTotals(); + break; } } @@ -385,6 +422,26 @@ class Discount return false; } + /** + * Removes the cart rule from the cart + */ + public function removeRule() + { + $cart = Cart::getCart(); + + $appliedRule = $this->cartRuleCart->findWhere([ + 'cart_id' => $cart->id + ]); + + if ($appliedRule->count() == 0) { + Cart::clearDiscount(); + + Cart::collectTotals(); + } + + return true; + } + public function removeCoupon() { $cart = \Cart::getCart(); diff --git a/packages/Webkul/Shop/src/Http/Controllers/CartController.php b/packages/Webkul/Shop/src/Http/Controllers/CartController.php index 1d5da6825..77ad39612 100755 --- a/packages/Webkul/Shop/src/Http/Controllers/CartController.php +++ b/packages/Webkul/Shop/src/Http/Controllers/CartController.php @@ -82,21 +82,7 @@ class CartController extends Controller */ public function index() { - $cart = Cart::getCart(); - - $appliedRule = $this->cartRuleCart->findWhere([ - 'cart_id' => $cart->id - ]); - - if ($appliedRule->count() == 0) { - Cart::removeDiscount(); - - Cart::collectTotals(); - } - - Cart::applyNonCoupon(); - - return view($this->_config['view'])->with('cart', $cart); + return view($this->_config['view'])->with('cart', Cart::getCart()); } /** diff --git a/packages/Webkul/Shop/src/Http/Controllers/OnepageController.php b/packages/Webkul/Shop/src/Http/Controllers/OnepageController.php index 9cbedeb01..eaf2454d6 100755 --- a/packages/Webkul/Shop/src/Http/Controllers/OnepageController.php +++ b/packages/Webkul/Shop/src/Http/Controllers/OnepageController.php @@ -67,18 +67,6 @@ class OnepageController extends Controller if (Cart::hasError()) return redirect()->route('shop.checkout.cart.index'); - $cart = Cart::getCart(); - - $appliedRule = $this->cartRuleCart->findWhere([ - 'cart_id' => $cart->id - ]); - - if ($appliedRule->count() == 0) { - Cart::removeDiscount(); - - Cart::collectTotals(); - } - Cart::applyNonCoupon(); return view($this->_config['view'])->with('cart', Cart::getCart()); @@ -133,7 +121,7 @@ class OnepageController extends Controller if (Cart::hasError() || !$shippingMethod || !Cart::saveShippingMethod($shippingMethod)) return response()->json(['redirect_url' => route('shop.checkout.cart.index')], 403); - Cart::applyNonCoupon(); + Cart::applyNonCoupon(); Cart::collectTotals(); diff --git a/packages/Webkul/Shop/src/Resources/views/checkout/onepage.blade.php b/packages/Webkul/Shop/src/Resources/views/checkout/onepage.blade.php index a16e3b747..99ab1ac54 100755 --- a/packages/Webkul/Shop/src/Resources/views/checkout/onepage.blade.php +++ b/packages/Webkul/Shop/src/Resources/views/checkout/onepage.blade.php @@ -378,7 +378,9 @@ axios.post('{{ route('shop.checkout.check.coupons') }}', { code: this_this.code }).then(function(response) { + console.log(response.data); }).catch(function(error) { + console.log(error.data); }); }, } diff --git a/packages/Webkul/Shop/src/Resources/views/checkout/total/summary.blade.php b/packages/Webkul/Shop/src/Resources/views/checkout/total/summary.blade.php index ee471e47f..b0cdc4f81 100755 --- a/packages/Webkul/Shop/src/Resources/views/checkout/total/summary.blade.php +++ b/packages/Webkul/Shop/src/Resources/views/checkout/total/summary.blade.php @@ -36,7 +36,7 @@ - @if (! request()->is('checkout/cart')) + @if (! request()->is('checkout/cart') && ! $cart->coupon_code)
@@ -48,5 +48,12 @@
+ @else +
+
+ + +
+
@endif \ No newline at end of file