From 3767494ce1c0ddc624f18027d5d6add108e7330b Mon Sep 17 00:00:00 2001 From: Eduan Date: Wed, 24 Jul 2024 13:19:13 -0300 Subject: [PATCH] Update SalesRuleValidator and ValidateByToken for Coupon Code Validation - 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. --- Model/SalesRuleValidator.php | 10 ++++++---- .../SalesRule/Model/RulesApplier/ValidateByToken.php | 2 +- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/Model/SalesRuleValidator.php b/Model/SalesRuleValidator.php index 2af9132..a5f3278 100644 --- a/Model/SalesRuleValidator.php +++ b/Model/SalesRuleValidator.php @@ -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)) { return $this->request->getHeader(self::TOKEN_HEADER) === $this->systemConfig->getSalesRuleToken(); } diff --git a/Plugin/Magento/SalesRule/Model/RulesApplier/ValidateByToken.php b/Plugin/Magento/SalesRule/Model/RulesApplier/ValidateByToken.php index 8e826cd..381f589 100644 --- a/Plugin/Magento/SalesRule/Model/RulesApplier/ValidateByToken.php +++ b/Plugin/Magento/SalesRule/Model/RulesApplier/ValidateByToken.php @@ -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); } }