diff --git a/classes/CartRule.php b/classes/CartRule.php index 7915ef899..0d8748d83 100644 --- a/classes/CartRule.php +++ b/classes/CartRule.php @@ -410,6 +410,8 @@ class CartRuleCore extends ObjectModel } unset($cart_rule); + Hook::exec('actionCustomerCartRulesModifier', array('cart_rules' => &$result, 'id_customer' => (int)$id_customer)); + return $result; } @@ -518,6 +520,38 @@ class CartRuleCore extends ObjectModel return false; } + /* + * 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. + * + * 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'); } diff --git a/modules/blocknewsletter/blocknewsletter.php b/modules/blocknewsletter/blocknewsletter.php index 959a54249..3a1609409 100644 --- a/modules/blocknewsletter/blocknewsletter.php +++ b/modules/blocknewsletter/blocknewsletter.php @@ -86,6 +86,8 @@ class Blocknewsletter extends Module 'actionExportGDPRData', 'actionObjectCustomerUpdateBefore', 'actionDeleteGDPRCustomer', + 'actionCustomerCartRulesModifier', + 'actionValidateCartRule', 'actionObjectCustomerDeleteAfter', ) ); @@ -133,6 +135,40 @@ class Blocknewsletter extends Module } } + public function hookActionCustomerCartRulesModifier($params) + { + $customer = new Customer($params['id_customer']); + foreach($params['cart_rules'] as $key => $cartRule) { + if ($cartRule['code'] == Configuration::get('NW_VOUCHER_CODE')) { + if ($customer->id && !$customer->is_guest) { + if (count(Order::getCustomerOrders($customer->id))) { + // remove welcome voucher + unset($params['cart_rules'][$key]); + } + } else { + // remove welcome voucher + unset($params['cart_rules'][$key]); + } + } + } + } + + public function hookActionValidateCartRule($params) + { + $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 { + $params['isValidatedByModules'] = false; + $params['isValidatedByModulesError'] = $this->l('This voucher is not available for this booking.'); + } + } + } + public function install() { if (!parent::install() || !$this->registerHooks() || !$this->callInstallTab()) { 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]