Merge pull request #814 from abhishek-webkul/gli--1140

Fixed: Voucher of one customer can be applied to another customer at back office
This commit is contained in:
Rohit Jain
2024-01-25 17:27:48 +05:30
committed by GitHub
4 changed files with 19 additions and 8 deletions

View File

@@ -98,6 +98,7 @@
selectFirst: false,
scroll: false,
dataType: "json",
cacheLength: 0,
formatItem: function(data, i, max, value, term) {
return value;
},
@@ -115,7 +116,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));

View File

@@ -333,6 +333,9 @@ class AdminCartsControllerCore extends AdminController
}
$this->context->cart = new Cart((int)$id_cart);
CartRule::autoRemoveFromCart($this->context);
CartRule::autoAddToCart($this->context);
if (!$this->context->cart->id) {
$this->context->cart->recyclable = 0;
$this->context->cart->gift = 0;