From 62a7954c330e7390d7c19b15920ca447ee9bac70 Mon Sep 17 00:00:00 2001 From: Prashant Singh Date: Mon, 27 May 2019 18:49:09 +0530 Subject: [PATCH] cart rules working --- packages/Webkul/Checkout/src/Cart.php | 5 -- .../Webkul/Discount/src/Helpers/Discount.php | 7 -- .../src/Http/Controllers/CartController.php | 69 +++++++++++++++++-- 3 files changed, 64 insertions(+), 17 deletions(-) diff --git a/packages/Webkul/Checkout/src/Cart.php b/packages/Webkul/Checkout/src/Cart.php index e6a81b875..04db42ed0 100755 --- a/packages/Webkul/Checkout/src/Cart.php +++ b/packages/Webkul/Checkout/src/Cart.php @@ -1240,9 +1240,4 @@ class Cart { public function setCouponAble() { return $this->discount->checkCouponConditions($this->getCart()); } - - public function setCoupon() { - $couponRules = $this->setCouponAble(); - return $couponRules; - } } \ No newline at end of file diff --git a/packages/Webkul/Discount/src/Helpers/Discount.php b/packages/Webkul/Discount/src/Helpers/Discount.php index 268a82e8b..b7c4e63e0 100644 --- a/packages/Webkul/Discount/src/Helpers/Discount.php +++ b/packages/Webkul/Discount/src/Helpers/Discount.php @@ -16,13 +16,6 @@ class Discount $this->cartRule = $cartRule; } - public function checkCoupon() - { - foreach($this->cartRule->all() as $rule) { - return $rule->name; - } - } - public function getGuestEndRules() { $rules = $this->cartRule->findWhere(['status' => 1, 'end_other_rules' => 1, 'is_guest' => 1]); diff --git a/packages/Webkul/Shop/src/Http/Controllers/CartController.php b/packages/Webkul/Shop/src/Http/Controllers/CartController.php index f436267f8..1fb168379 100755 --- a/packages/Webkul/Shop/src/Http/Controllers/CartController.php +++ b/packages/Webkul/Shop/src/Http/Controllers/CartController.php @@ -261,20 +261,79 @@ class CartController extends Controller $code = request()->input('code'); $rules = Cart::setCoupon(); + $impacts = array(); + $appliedRule; foreach($rules['id'] as $rule) { if ($rule->use_coupon && $rule->auto_generation == 0) { - // dump($rule->coupons->code, $rule->id); - if ($rule->coupons->code == $code) { - return $rule->id; + $appliedRule = $rule; + + break; } } else { - dump($rule->coupons->prefix, $rule->coupons->suffix, $rule->id); + dd('auto_generation in next version'); } } - die; + $cart = Cart::getCart(); + // check all the conditions associated with the rule + if ($appliedRule->starts_from == null) { + $action_type = $appliedRule->action_type; + $disc_threshold = $appliedRule->disc_threshold; + $disc_amount = $appliedRule->disc_amount; + $disc_quantity = $appliedRule->disc_quantity; + + $newBaseSubTotal = 0; + $newQuantity = 0; + if ($cart->items_qty >= $disc_threshold) { + if ($action_type == config('pricerules.cart.validation.0')) { + //CART + $newBaseSubTotal = ($cart->base_sub_total * $disc_amount) / 100; + } else if ($action_type == config('pricerules.cart.validation.1')) { + $newBaseSubTotal = $cart->base_sub_total - $disc_amount; + } else if ($action_type == config('pricerules.cart.validation.2')) { + //CART + $newQuantity = $cart->items()->first()->quantity + $disc_amount; + } else if ($action_type == config('pricerules.cart.validation.3')) { + $newBaseSubTotal = $disc_amount ; + } + + if ($action_type == config('pricerules.cart.validation.2')) { + dd($newQuantity); + } else { + dd($newBaseSubTotal); + } + } + } else { + //time based rules + $action_type = $appliedRule->action_type; + $disc_threshold = $appliedRule->disc_threshold; + $disc_amount = $appliedRule->disc_amount; + $disc_quantity = $appliedRule->disc_quantity; + + $newBaseSubTotal = 0; + $newQuantity = 0; + if ($cart->items_qty >= $disc_threshold) { + if ($action_type == config('pricerules.cart.validation.0')) { + //CART + $newBaseSubTotal = ($cart->base_sub_total * $disc_amount) / 100; + } else if ($action_type == config('pricerules.cart.validation.1')) { + $newBaseSubTotal = $cart->base_sub_total - $disc_amount; + } else if ($action_type == config('pricerules.cart.validation.2')) { + //CART + $newQuantity = $cart->items()->first()->quantity + $disc_amount; + } else if ($action_type == config('pricerules.cart.validation.3')) { + $newBaseSubTotal = $disc_amount ; + } + + if ($action_type == config('pricerules.cart.validation.2')) { + dd($newQuantity); + } else { + dd($newBaseSubTotal); + } + } + } } } \ No newline at end of file