diff --git a/admin/themes/default/template/controllers/orders/form.tpl b/admin/themes/default/template/controllers/orders/form.tpl index 99b5ab612..9334e9358 100644 --- a/admin/themes/default/template/controllers/orders/form.tpl +++ b/admin/themes/default/template/controllers/orders/form.tpl @@ -115,7 +115,8 @@ ajax: "1", token: "{getAdminToken tab='AdminCartRules'}", tab: "AdminCartRules", - action: "searchCartRuleVouchers" + action: "searchCartRuleVouchers", + id_customer: function () { return window.id_customer }, } } ) diff --git a/classes/CartRule.php b/classes/CartRule.php index 9086135ad..35a882ece 100644 --- a/classes/CartRule.php +++ b/classes/CartRule.php @@ -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).'%\''); } } } diff --git a/controllers/admin/AdminCartRulesController.php b/controllers/admin/AdminCartRulesController.php index 87509df72..f8917621e 100644 --- a/controllers/admin/AdminCartRulesController.php +++ b/controllers/admin/AdminCartRulesController.php @@ -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));