From c9cd73b67e9cb903d6515b995aaa33aa7756d17a Mon Sep 17 00:00:00 2001 From: Prashant Singh Date: Fri, 6 Sep 2019 13:38:05 +0530 Subject: [PATCH 1/2] Fixed edge case conditions and a bug in application of couponable rule --- .../Webkul/Discount/src/Actions/Cart/FixedAmount.php | 6 ++++-- .../Discount/src/Actions/Cart/PercentOfProduct.php | 12 ++++++++++-- .../Webkul/Discount/src/Helpers/Cart/Discount.php | 8 ++++---- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/packages/Webkul/Discount/src/Actions/Cart/FixedAmount.php b/packages/Webkul/Discount/src/Actions/Cart/FixedAmount.php index 4daeb90be..668862df4 100644 --- a/packages/Webkul/Discount/src/Actions/Cart/FixedAmount.php +++ b/packages/Webkul/Discount/src/Actions/Cart/FixedAmount.php @@ -56,9 +56,11 @@ class FixedAmount extends Action $report['item_id'] = $item->id; $report['product_id'] = $item->child ? $item->child->product_id : $item->product_id; - $discount = round($itemPrice * $discQuantity, 4); + $discount = round($itemPrice * $discQuantity, 4) * $discQuantity; - $discount = $discount <= $itemPrice ? $discount : $itemPrice; + if ($rule->action_type == 'fixed_amount') { + $discount = $discount <= $itemPrice ? $discount : $itemPrice; + } $report['discount'] = $discount; diff --git a/packages/Webkul/Discount/src/Actions/Cart/PercentOfProduct.php b/packages/Webkul/Discount/src/Actions/Cart/PercentOfProduct.php index 46343b847..b9f0e4574 100644 --- a/packages/Webkul/Discount/src/Actions/Cart/PercentOfProduct.php +++ b/packages/Webkul/Discount/src/Actions/Cart/PercentOfProduct.php @@ -63,9 +63,17 @@ class PercentOfProduct extends Action $report['item_id'] = $item->id; $report['product_id'] = $item->child ? $item->child->product_id : $item->product_id; - $discount = round(($itemPrice * $rule->disc_amount) / 100, 4); + if ($rule->disc_amount > 100) { + $discount_amount = 100; + } else { + $discount_amount = 100; + } - $discount = $discount <= $itemPrice ? $discount : $itemPrice; + $discount = round(($itemPrice * $discount_amount) / 100, 4) * $discQuantity; + + if ($rule->action_type == 'percent_of_product') { + $discount = $discount <= $itemPrice ? $discount : $itemPrice; + } $report['discount'] = $discount; diff --git a/packages/Webkul/Discount/src/Helpers/Cart/Discount.php b/packages/Webkul/Discount/src/Helpers/Cart/Discount.php index d2b7b3cec..5488de48e 100644 --- a/packages/Webkul/Discount/src/Helpers/Cart/Discount.php +++ b/packages/Webkul/Discount/src/Helpers/Cart/Discount.php @@ -58,16 +58,16 @@ abstract class Discount */ public function getApplicableRules($code = null) { + $rules = collect(); + if ($code != null) { - $rules = $this->cartRule->findWhere([ + $eligibleRules = $this->cartRule->findWhere([ 'use_coupon' => 1, 'status' => 1 ]); - foreach($rules as $rule) { + foreach($eligibleRules as $rule) { if ($rule->coupons->code == $code) { - $rules = collect(); - $rules->push($rule); break; From 6ca065544cae21350ca701e9568a0ad6a368564a Mon Sep 17 00:00:00 2001 From: Prashant Singh Date: Fri, 6 Sep 2019 13:46:20 +0530 Subject: [PATCH 2/2] Edge case fix for Cart Rule actions --- packages/Webkul/Discount/src/Actions/Cart/FixedAmount.php | 4 +--- .../Webkul/Discount/src/Actions/Cart/PercentOfProduct.php | 4 +--- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/packages/Webkul/Discount/src/Actions/Cart/FixedAmount.php b/packages/Webkul/Discount/src/Actions/Cart/FixedAmount.php index 668862df4..4b9fc7f4a 100644 --- a/packages/Webkul/Discount/src/Actions/Cart/FixedAmount.php +++ b/packages/Webkul/Discount/src/Actions/Cart/FixedAmount.php @@ -58,9 +58,7 @@ class FixedAmount extends Action $discount = round($itemPrice * $discQuantity, 4) * $discQuantity; - if ($rule->action_type == 'fixed_amount') { - $discount = $discount <= $itemPrice ? $discount : $itemPrice; - } + $discount = $discount <= $itemPrice * $discQuantity ? $discount : $itemPrice * $discQuantity; $report['discount'] = $discount; diff --git a/packages/Webkul/Discount/src/Actions/Cart/PercentOfProduct.php b/packages/Webkul/Discount/src/Actions/Cart/PercentOfProduct.php index b9f0e4574..ce776ef7c 100644 --- a/packages/Webkul/Discount/src/Actions/Cart/PercentOfProduct.php +++ b/packages/Webkul/Discount/src/Actions/Cart/PercentOfProduct.php @@ -71,9 +71,7 @@ class PercentOfProduct extends Action $discount = round(($itemPrice * $discount_amount) / 100, 4) * $discQuantity; - if ($rule->action_type == 'percent_of_product') { - $discount = $discount <= $itemPrice ? $discount : $itemPrice; - } + $discount = $discount <= $itemPrice * $discQuantity ? $discount : $itemPrice * $discQuantity; $report['discount'] = $discount;