diff --git a/classes/CartRule.php b/classes/CartRule.php index 35a882ece..f48ffb3b1 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 fa682541b..9d2c23fb4 100644 --- a/classes/PaymentModule.php +++ b/classes/PaymentModule.php @@ -664,9 +664,13 @@ 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); + 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