Added cart rule validation function which validates the cart rule and added unique coupon check while creating or updating couponable cart rule

This commit is contained in:
Prashant Singh 2019-06-20 13:03:01 +05:30
parent acad17af67
commit 264d2e3c9e
5 changed files with 66 additions and 9 deletions

View File

@ -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
],
/*

View File

@ -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' => [

View File

@ -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

View File

@ -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;

View File

@ -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;