Do not allow voucher code duplications for same customer
This commit is contained in:
@@ -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 : ''));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user