From ee9de469d4b0258ae492ff3fa8fb0c5ed6a8a0b2 Mon Sep 17 00:00:00 2001 From: Shreesh Arora Date: Fri, 7 Jun 2024 12:27:59 +0530 Subject: [PATCH 1/4] Fixed: Welcome voucher is available for every customer --- classes/CartRule.php | 6 ++++ modules/blocknewsletter/blocknewsletter.php | 34 ++++++++++++++++++++- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/classes/CartRule.php b/classes/CartRule.php index 634434e53..24d49acd6 100644 --- a/classes/CartRule.php +++ b/classes/CartRule.php @@ -407,6 +407,8 @@ class CartRuleCore extends ObjectModel } unset($cart_rule); + Hook::exec('actionCartRulesAvailable', array('result' => &$result, 'id_customer' => (int)$id_customer)); + return $result; } @@ -685,6 +687,10 @@ class CartRuleCore extends ObjectModel } } + if ($errors = Hook::exec('actionCheckCartRuleValid', array('cartRule' => $this))) { + return (!$display_error) ? false : Tools::displayError($errors); + } + if (!$nb_products) { return (!$display_error) ? false : Tools::displayError('Cart is empty'); } diff --git a/modules/blocknewsletter/blocknewsletter.php b/modules/blocknewsletter/blocknewsletter.php index a7a0612d6..3ece0d252 100644 --- a/modules/blocknewsletter/blocknewsletter.php +++ b/modules/blocknewsletter/blocknewsletter.php @@ -84,7 +84,9 @@ class Blocknewsletter extends Module 'actionCustomerAccountAdd', 'registerGDPRConsent', 'actionExportGDPRData', - 'actionDeleteGDPRCustomer' + 'actionDeleteGDPRCustomer', + 'actionCartRulesAvailable', + 'actionCheckCartRuleValid', ) ); } @@ -111,6 +113,36 @@ class Blocknewsletter extends Module } } + public function hookActionCartRulesAvailable($params) + { + foreach($params['result'] as $key => $cartRule) { + if ($cartRule['code'] == Configuration::get('NW_VOUCHER_CODE')) { + if ($params['id_customer']) { + if (count(OrderCore::getCustomerOrders($params['id_customer']))) { + // remove welcome voucher + unset($params['result'][$key]); + } + } else { + // remove welcome voucher + unset($params['result'][$key]); + } + } + } + } + + public function hookActionCheckCartRuleValid($params) + { + if ($params['cartRule']->code == Configuration::get('NW_VOUCHER_CODE')) { + if ($params['id_customer']) { + if (count(OrderCore::getCustomerOrders($params['id_customer']))) { + return $this->l('This voucher is not available for this booking.'); + } + } else { + return $this->l('This voucher is not available for this booking.'); + } + } + } + public function install() { if (!parent::install() || !$this->registerHooks() || !$this->callInstallTab()) { From fe31a29245b68317f08a0c064f2c1f1d5838e728 Mon Sep 17 00:00:00 2001 From: Shreesh Arora Date: Thu, 11 Jul 2024 15:14:30 +0530 Subject: [PATCH 2/4] prevent guest users form using welcome voucher --- classes/CartRule.php | 40 ++++++++++++++++++--- modules/blocknewsletter/blocknewsletter.php | 32 +++++++++-------- 2 files changed, 53 insertions(+), 19 deletions(-) diff --git a/classes/CartRule.php b/classes/CartRule.php index 24d49acd6..133bce84f 100644 --- a/classes/CartRule.php +++ b/classes/CartRule.php @@ -407,7 +407,7 @@ class CartRuleCore extends ObjectModel } unset($cart_rule); - Hook::exec('actionCartRulesAvailable', array('result' => &$result, 'id_customer' => (int)$id_customer)); + Hook::exec('actionCustomerCartRulesModifier', array('cart_rules' => &$result, 'id_customer' => (int)$id_customer)); return $result; } @@ -517,6 +517,40 @@ class CartRuleCore extends ObjectModel return false; } + /* + * Custom cart rule validation from modules. Allows to create infinite possibilities of rules. + * + * If null is provided, nothing happens and built-in validation is ran. Useful if you want your own conditions, + * but also want to retain functionality of the core. + * + * If true is provided, the validation ends here and the rule is VALID, ignoring the rest of core validation. + * + * If false is provided, the validation ends here and the rule is not VALID, ignoring the rest of core validation. + * In this case, it's recommended to properly alter the isValidatedByModulesError error message so the user knows why. + */ + $isValidatedByModules = null; + $isValidatedByModulesError = Tools::displayError('This voucher is not valid.'); + Hook::exec( + 'actionValidateCartRule', + array( + 'cart_rule' => $this, + 'context' => $context, + 'alreadyInCart' => $alreadyInCart, + 'display_error' => $display_error, + 'check_carrier' => $check_carrier, + 'isValidatedByModules' => &$isValidatedByModules, + 'isValidatedByModulesError' => &$isValidatedByModulesError, + ) + ); + + if ($isValidatedByModules === false) { + return (!$display_error) ? false : $isValidatedByModulesError; + } + + if ($isValidatedByModules === true) { + return (!$display_error) ? true : null; + } + if (!$this->active) { return (!$display_error) ? false : Tools::displayError('This voucher is disabled'); } @@ -687,10 +721,6 @@ class CartRuleCore extends ObjectModel } } - if ($errors = Hook::exec('actionCheckCartRuleValid', array('cartRule' => $this))) { - return (!$display_error) ? false : Tools::displayError($errors); - } - if (!$nb_products) { return (!$display_error) ? false : Tools::displayError('Cart is empty'); } diff --git a/modules/blocknewsletter/blocknewsletter.php b/modules/blocknewsletter/blocknewsletter.php index e8a8c2133..dbfe6698a 100644 --- a/modules/blocknewsletter/blocknewsletter.php +++ b/modules/blocknewsletter/blocknewsletter.php @@ -85,8 +85,8 @@ class Blocknewsletter extends Module 'registerGDPRConsent', 'actionExportGDPRData', 'actionDeleteGDPRCustomer', - 'actionCartRulesAvailable', - 'actionCheckCartRuleValid', + 'actionCustomerCartRulesModifier', + 'actionValidateCartRule', 'actionObjectCustomerDeleteAfter', ) ); @@ -124,32 +124,36 @@ class Blocknewsletter extends Module } } - public function hookActionCartRulesAvailable($params) + public function hookActionCustomerCartRulesModifier($params) { - foreach($params['result'] as $key => $cartRule) { + $customer = new Customer($params['id_customer']); + foreach($params['cart_rules'] as $key => $cartRule) { if ($cartRule['code'] == Configuration::get('NW_VOUCHER_CODE')) { - if ($params['id_customer']) { - if (count(OrderCore::getCustomerOrders($params['id_customer']))) { + if ($customer->id && !$customer->is_guest) { + if (count(Order::getCustomerOrders($customer->id))) { // remove welcome voucher - unset($params['result'][$key]); + unset($params['cart_rules'][$key]); } } else { // remove welcome voucher - unset($params['result'][$key]); + unset($params['cart_rules'][$key]); } } } } - public function hookActionCheckCartRuleValid($params) + public function hookActionValidateCartRule($params) { - if ($params['cartRule']->code == Configuration::get('NW_VOUCHER_CODE')) { - if ($params['id_customer']) { - if (count(OrderCore::getCustomerOrders($params['id_customer']))) { - return $this->l('This voucher is not available for this booking.'); + $customer = new Customer($params['context']->cart->id_customer); + if ($params['cart_rule']->code == Configuration::get('NW_VOUCHER_CODE')) { + if ($customer->id && !$customer->is_guest) { + if (count(Order::getCustomerOrders($customer->id))) { + $params['isValidatedByModules'] = false; + $params['isValidatedByModulesError'] = $this->l('This voucher is not available for this booking.'); } } else { - return $this->l('This voucher is not available for this booking.'); + $params['isValidatedByModules'] = false; + $params['isValidatedByModulesError'] = $this->l('This voucher is not available for this booking.'); } } } From 6606d975c8e24743338aca9141870fcc4450eee4 Mon Sep 17 00:00:00 2001 From: Shreesh Arora Date: Fri, 16 Aug 2024 12:30:09 +0530 Subject: [PATCH 3/4] update newsletter welcome voucher --- modules/blocknewsletter/mails/en/newsletter_voucher.html | 7 ++++++- modules/blocknewsletter/mails/en/newsletter_voucher.txt | 4 +++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/modules/blocknewsletter/mails/en/newsletter_voucher.html b/modules/blocknewsletter/mails/en/newsletter_voucher.html index cb8e3db1e..1156ed1e4 100644 --- a/modules/blocknewsletter/mails/en/newsletter_voucher.html +++ b/modules/blocknewsletter/mails/en/newsletter_voucher.html @@ -87,7 +87,12 @@

Newsletter subscription

- Regarding your newsletter subscription, we are pleased to offer you the following voucher: {discount} + Regarding your newsletter subscription, we are pleased to offer you the welcome voucher: {discount} + +
+
+ + Please note that the voucher will be valid on your first order. diff --git a/modules/blocknewsletter/mails/en/newsletter_voucher.txt b/modules/blocknewsletter/mails/en/newsletter_voucher.txt index 7e1039388..5d95feb94 100644 --- a/modules/blocknewsletter/mails/en/newsletter_voucher.txt +++ b/modules/blocknewsletter/mails/en/newsletter_voucher.txt @@ -4,7 +4,9 @@ Hi, Regarding your newsletter subscription, we are pleased to offer -you the following voucher: {discount} +you the welcome voucher: {discount} + +Please note that the voucher will be valid on your first order. {shop_name} [/{shop_url}] powered by Webkul(tm) [https://webkul.com] From 791d906a8feb887bccae571e00c7ba6a13846740 Mon Sep 17 00:00:00 2001 From: Shreesh Arora Date: Wed, 21 Aug 2024 10:53:18 +0530 Subject: [PATCH 4/4] update new hook comment --- classes/CartRule.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/classes/CartRule.php b/classes/CartRule.php index 133bce84f..835ad2ab2 100644 --- a/classes/CartRule.php +++ b/classes/CartRule.php @@ -518,9 +518,7 @@ class CartRuleCore extends ObjectModel } /* - * Custom cart rule validation from modules. Allows to create infinite possibilities of rules. - * - * If null is provided, nothing happens and built-in validation is ran. Useful if you want your own conditions, + * If null is provided, default validation will be processed. Useful if you want your own conditions, * but also want to retain functionality of the core. * * If true is provided, the validation ends here and the rule is VALID, ignoring the rest of core validation.