diff --git a/config/app.php b/config/app.php index 890414dba..b15029bdc 100755 --- a/config/app.php +++ b/config/app.php @@ -245,7 +245,7 @@ return [ Webkul\Sales\Providers\SalesServiceProvider::class, Webkul\Tax\Providers\TaxServiceProvider::class, Webkul\API\Providers\APIServiceProvider::class, - Webkul\Discount\Providers\DiscountServiceProvider::class, + Webkul\Discount\Providers\DiscountServiceProvider::class ], /* diff --git a/packages/Webkul/Admin/src/Resources/lang/en/app.php b/packages/Webkul/Admin/src/Resources/lang/en/app.php index 2dbb9d1ab..177477423 100755 --- a/packages/Webkul/Admin/src/Resources/lang/en/app.php +++ b/packages/Webkul/Admin/src/Resources/lang/en/app.php @@ -864,7 +864,8 @@ return [ 'coupon-failed' => 'Coupon failed to apply', 'no-coupon' => '* Coupon not applicable', 'coupon-removed' => 'Coupon removed successfully', - 'coupon-remove-failed' => 'Coupon removal failed' + 'coupon-remove-failed' => 'Coupon removal failed', + 'duplicate-coupon' => 'Coupon already exists, please try again with a different coupon' ], 'catalog' => [ diff --git a/packages/Webkul/Discount/src/Helpers/Discount.php b/packages/Webkul/Discount/src/Helpers/Discount.php index 1b041ed7d..a39c05077 100644 --- a/packages/Webkul/Discount/src/Helpers/Discount.php +++ b/packages/Webkul/Discount/src/Helpers/Discount.php @@ -136,25 +136,26 @@ abstract class Discount ]); if (count($existingRule)) { - // $this->clearDiscount(); - if ($existingRule->first()->cart_rule_id != $rule->id) { $existingRule->first()->update([ 'cart_rule_id' => $rule->id ]); + $this->clearDiscount(); + $this->updateCartItemAndCart($rule); return true; } } else { - // $this->clearDiscount(); $this->cartRuleCart->create([ 'cart_id' => $cart->id, 'cart_rule_id' => $rule->id ]); + $this->clearDiscount(); + $this->updateCartItemAndCart($rule); return true; @@ -272,7 +273,7 @@ abstract class Discount /** * To find the max worth item in current cart instance * - * @return Array + * @return array */ public function maxWorthItem() { @@ -296,6 +297,27 @@ abstract class Discount return $maxWorthItem; } + /** + * Validate the currently applied cart rule + * + * @return boolean + */ + public function validateRule($rule) + { + $applicability = $this->checkApplicability($rule); + + if ($applicability) { + if ($rule->status) { + return true; + } else { + return false; + } + } else { + return false; + } + } + + /** * Checks the rule against the current cart instance whether rule conditions are applicable * or not diff --git a/packages/Webkul/Discount/src/Helpers/NonCouponAbleRule.php b/packages/Webkul/Discount/src/Helpers/NonCouponAbleRule.php index 550b9f07c..7f7cbb4de 100644 --- a/packages/Webkul/Discount/src/Helpers/NonCouponAbleRule.php +++ b/packages/Webkul/Discount/src/Helpers/NonCouponAbleRule.php @@ -32,13 +32,25 @@ class NonCouponAbleRule extends Discount ]); } - $alreadyAppliedRule = $this->cartRuleCart->findWhere([ + $alreadyAppliedCartRuleCart = $this->cartRuleCart->findWhere([ 'cart_id' => $cart->id, ]); - if (count($alreadyAppliedRule)) { - $alreadyAppliedRule = $alreadyAppliedRule->first()->cart_rule; + if (count($alreadyAppliedCartRuleCart)) { + $alreadyAppliedRule = $alreadyAppliedCartRuleCart->first()->cart_rule; + + $validated = $this->validateRule($alreadyAppliedRule); + + if (! $alreadyAppliedRule->use_coupon && ! $validated) { + // if the validation fails then the cart rule gets deleted from cart rule cart + $alreadyAppliedRule->delete(); + + // all discount is cleared fro mthe cart and cart items table + $this->clearDiscount(); + + return null; + } if ($alreadyAppliedRule->use_coupon) { return null; diff --git a/packages/Webkul/Discount/src/Http/Controllers/CartRuleController.php b/packages/Webkul/Discount/src/Http/Controllers/CartRuleController.php index 21919c081..79d790a6f 100644 --- a/packages/Webkul/Discount/src/Http/Controllers/CartRuleController.php +++ b/packages/Webkul/Discount/src/Http/Controllers/CartRuleController.php @@ -190,6 +190,16 @@ class CartRuleController extends Controller // save the coupon used in coupon section $coupons['code'] = $data['code']; + $couponExists = $this->cartRuleCoupon->findWhere([ + 'code' => $coupons['code'] + ]); + + if ($couponExists->count()) { + session()->flash('warning', trans('admin::app.promotion.status.duplicate-coupon')); + + return redirect()->back(); + } + // set coupon usage per customer same as per_customer limit which is disabled for now $coupons['usage_per_customer'] = $data['per_customer']; //0 is for unlimited usage // unset coupon code from coupon section @@ -390,6 +400,18 @@ class CartRuleController extends Controller $coupons['code'] = $data['code']; + $couponExists = $this->cartRuleCoupon->findWhere([ + 'code' => $coupons['code'] + ]); + + if ($couponExists->count()) { + if ($couponExists->first()->cart_rule_id != $id) { + session()->flash('warning', trans('admin::app.promotion.status.duplicate-coupon')); + + return redirect()->back(); + } + } + unset($data['code']); // } else { // $data['auto_generation'] = 1;