From a49650efbea20ca35eaebbb45e2213ad84614414 Mon Sep 17 00:00:00 2001 From: Abhishek Kumar Date: Mon, 7 Aug 2023 16:15:10 +0530 Subject: [PATCH 1/3] Do not allow voucher code duplications for same customer --- classes/CartRule.php | 5 +++-- classes/PaymentModule.php | 9 ++++++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/classes/CartRule.php b/classes/CartRule.php index 9086135ad..c965e3f36 100644 --- a/classes/CartRule.php +++ b/classes/CartRule.php @@ -421,7 +421,7 @@ class CartRuleCore extends ObjectModel * @param $name * @return bool */ - public static function cartRuleExists($name) + public static function cartRuleExists($code, $id_customer = 0) { if (!CartRule::isFeatureActive()) { return false; @@ -430,7 +430,8 @@ class CartRuleCore extends ObjectModel return (bool)Db::getInstance()->getValue(' SELECT `id_cart_rule` FROM `'._DB_PREFIX_.'cart_rule` - WHERE `code` = \''.pSQL($name).'\''); + WHERE `code` = \''.pSQL($code).'\''. + ($id_customer ? 'AND `id_customer` = '.(int) $id_customer : '')); } /** diff --git a/classes/PaymentModule.php b/classes/PaymentModule.php index 3e89319f2..0e3ccbfdd 100644 --- a/classes/PaymentModule.php +++ b/classes/PaymentModule.php @@ -669,6 +669,13 @@ abstract class PaymentModuleCore extends Module $voucher->code = preg_replace('/'.$matches[0].'$/', '-'.(intval($matches[1]) + 1), $voucher->code); } + // If ABCDEFGH-2 already exists for customer use ABCDEFGH-3 + while (CartRule::cartRuleExists($voucher->code, $order->id_customer)) { + if (preg_match('/\-([0-9]{1,2})$/', $voucher->code, $matches)) { + $voucher->code = preg_replace('/'.$matches[1].'$/', (intval($matches[1]) + 1), $voucher->code); + } + } + // Set the new voucher value $voucher->reduction_amount = $remaining_amount; if ($voucher->reduction_tax) { @@ -1782,4 +1789,4 @@ abstract class PaymentModuleCore extends Module } return $result; } -} +} \ No newline at end of file From 83ec4b4306271f9545684a3ae0ea25b8a07618ae Mon Sep 17 00:00:00 2001 From: Abhishek Kumar Date: Mon, 15 Jan 2024 12:52:37 +0530 Subject: [PATCH 2/3] Fix voucher code issue --- classes/PaymentModule.php | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/classes/PaymentModule.php b/classes/PaymentModule.php index 8c1841308..ce2801e26 100644 --- a/classes/PaymentModule.php +++ b/classes/PaymentModule.php @@ -664,12 +664,9 @@ abstract class PaymentModuleCore extends Module unset($voucher->id); // Set a new voucher code + // For a customer, applying voucher ABCD will create partial vouchers ABCD-2, ABCD-3 and so on, and + // applying voucher ABCD-2 will create vouchers ABCD-2-2, ABCD-2-3 and so on $voucher->code = empty($voucher->code) ? substr(md5($order->id.'-'.$order->id_customer.'-'.$cart_rule['obj']->id), 0, 16) : $voucher->code.'-2'; - if (preg_match('/\-([0-9]{1,2})\-([0-9]{1,2})$/', $voucher->code, $matches) && $matches[1] == $matches[2]) { - $voucher->code = preg_replace('/'.$matches[0].'$/', '-'.(intval($matches[1]) + 1), $voucher->code); - } - - // If ABCDEFGH-2 already exists for customer use ABCDEFGH-3 while (CartRule::cartRuleExists($voucher->code, $order->id_customer)) { if (preg_match('/\-([0-9]{1,2})$/', $voucher->code, $matches)) { $voucher->code = preg_replace('/'.$matches[1].'$/', (intval($matches[1]) + 1), $voucher->code); From e3afb4c6ec05abcd0f1d665f385625a04a3cdb99 Mon Sep 17 00:00:00 2001 From: Abhishek Kumar Date: Mon, 15 Jan 2024 12:53:46 +0530 Subject: [PATCH 3/3] Undo newline removal --- classes/PaymentModule.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/classes/PaymentModule.php b/classes/PaymentModule.php index ce2801e26..8cdbec966 100644 --- a/classes/PaymentModule.php +++ b/classes/PaymentModule.php @@ -1786,4 +1786,4 @@ abstract class PaymentModuleCore extends Module } return $result; } -} \ No newline at end of file +}