Do not allow voucher of one customer to be applicable to another customer

This commit is contained in:
Abhishek Kumar
2023-08-28 17:20:43 +05:30
parent ccace94c38
commit 368c95b08a
3 changed files with 15 additions and 8 deletions

View File

@@ -115,7 +115,8 @@
ajax: "1",
token: "{getAdminToken tab='AdminCartRules'}",
tab: "AdminCartRules",
action: "searchCartRuleVouchers"
action: "searchCartRuleVouchers",
id_customer: function () { return window.id_customer },
}
}
)

View File

@@ -601,7 +601,7 @@ class CartRuleCore extends ObjectModel
// Check if the cart rule is only usable by a specific customer, and if the current customer is the right one
if ($this->id_customer && $context->cart->id_customer != $this->id_customer) {
if (!Context::getContext()->customer->isLogged()) {
if (!defined('_PS_ADMIN_DIR_') && !Context::getContext()->customer->isLogged()) {
return (!$display_error) ? false : (Tools::displayError('You cannot use this voucher').' - '.Tools::displayError('Please log in first'));
}
return (!$display_error) ? false : Tools::displayError('You cannot use this voucher');
@@ -1466,15 +1466,16 @@ class CartRuleCore extends ObjectModel
* @param $id_lang
* @return array
*/
public static function getCartsRuleByCode($name, $id_lang, $extended = false)
public static function getCartsRuleByCode($name, $id_lang, $extended = false, $id_customer = 0)
{
$sql_base = 'SELECT cr.*, crl.*
FROM '._DB_PREFIX_.'cart_rule cr
LEFT JOIN '._DB_PREFIX_.'cart_rule_lang crl ON (cr.id_cart_rule = crl.id_cart_rule AND crl.id_lang = '.(int)$id_lang.')';
FROM '._DB_PREFIX_.'cart_rule cr
LEFT JOIN '._DB_PREFIX_.'cart_rule_lang crl ON (cr.id_cart_rule = crl.id_cart_rule AND crl.id_lang = '.(int)$id_lang.')
WHERE 1'.($id_customer ? ' AND (cr.`id_customer` = 0 OR cr.`id_customer` = '.(int) $id_customer.')' : '');
if ($extended) {
return Db::getInstance()->executeS('('.$sql_base.' WHERE code LIKE \'%'.pSQL($name).'%\') UNION ('.$sql_base.' WHERE name LIKE \'%'.pSQL($name).'%\')');
return Db::getInstance()->executeS('('.$sql_base.' AND code LIKE \'%'.pSQL($name).'%\') UNION ('.$sql_base.' AND name LIKE \'%'.pSQL($name).'%\')');
} else {
return Db::getInstance()->executeS($sql_base.' WHERE code LIKE \'%'.pSQL($name).'%\'');
return Db::getInstance()->executeS($sql_base.' AND code LIKE \'%'.pSQL($name).'%\'');
}
}
}

View File

@@ -768,7 +768,12 @@ class AdminCartRulesControllerCore extends AdminController
public function displayAjaxSearchCartRuleVouchers()
{
$found = false;
if ($vouchers = CartRule::getCartsRuleByCode(Tools::getValue('q'), (int)$this->context->language->id, true)) {
if ($vouchers = CartRule::getCartsRuleByCode(
Tools::getValue('q'),
(int)$this->context->language->id,
true,
(int) Tools::getValue('id_customer')
)) {
$found = true;
}
echo json_encode(array('found' => $found, 'vouchers' => $vouchers));