Skip to content

Commit

Permalink
Update SalesRuleValidator and ValidateByToken for Coupon Code Validation
Browse files Browse the repository at this point in the history
- Changed the parameter type of `SalesRuleValidator::isValid` method from `string` to `array` to handle multiple coupon codes.
- Modified the `SalesRuleValidator::isValid` method to validate against an array of coupon codes using `in_array`.
- Updated `ValidateByToken` plugin to pass the coupon code as an array instead of casting it to a string.

These changes enhance the validation process by allowing multiple coupon codes to be checked, ensuring greater flexibility and accuracy.
  • Loading branch information
eduanroute committed Jul 24, 2024
1 parent 3f79a71 commit 3767494
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
10 changes: 6 additions & 4 deletions Model/SalesRuleValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@ public function __construct(Request $request, SystemConfig $systemConfig)
}

/**
* @param Rule $rule
* @param string $couponCode
* @param Rule $rule
* @param array $couponCode
* @return bool
*/
public function isValid(Rule $rule, string $couponCode): bool
public function isValid(Rule $rule, array $couponCode): bool
{
if ($rule->getData('is_grin_only') && ($rule->getPrimaryCoupon()->getCode() === $couponCode)) {
$code = $rule->getPrimaryCoupon()->getCode();

if (!is_null($code) && $rule->getData('is_grin_only') && in_array($code, $couponCode)) {

Check warning on line 43 in Model/SalesRuleValidator.php

View workflow job for this annotation

GitHub Actions / M2 Coding Standard

The use of function is_null() is discouraged; use strict comparison "=== null"() instead

Check warning on line 43 in Model/SalesRuleValidator.php

View workflow job for this annotation

GitHub Actions / M2 Coding Standard

The use of function is_null() is discouraged; use strict comparison "=== null"() instead
return $this->request->getHeader(self::TOKEN_HEADER) === $this->systemConfig->getSalesRuleToken();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function aroundApplyRules(
$couponCode
): array {
foreach ($rules as $rule) {
if (!$this->salesRuleValidator->isValid($rule, (string) $couponCode)) {
if (!$this->salesRuleValidator->isValid($rule, $couponCode)) {
return $proceed($item, [], $skipValidation, $couponCode);
}
}
Expand Down

0 comments on commit 3767494

Please sign in to comment.