Search code examples
phpmoduleprestashopprestashop-1.6

Split Mailalerts module recipients in prestashop


I need a specific list of recipients to receive stocks alerts only and another list of recipients to receive only the new order alert.

What is the best way to do it?

Thank you

enter image description here


Solution

  • Here is how I would deal with this question:

    Create the override file /override/modules/mailalerts/mailalerts.php with this code:

    <?php
    
    class MailAlertsOverride extends MailAlerts
    {
        protected $merchant_mails_stock;
    
        protected function init()
        {
            parent::init();
            $this->merchant_mails_stock = str_replace(',', self::__MA_MAIL_DELIMITOR__, (string)Configuration::get('MA_MERCHANT_MAILS_STOCK'));
        }
    
        public function install($delete_params = true)
        {
            if (! parent::install($delete_params))
                return false;
    
            if ($delete_params)
            {
                Configuration::updateValue('MA_MERCHANT_MAILS_STOCK', Configuration::get('PS_SHOP_EMAIL'));
            }
    
            return true;
        }
    
        public function uninstall($delete_params = true)
        {
            if ($delete_params)
            {
                Configuration::deleteByName('MA_MERCHANT_MAILS_STOCK');
            }
    
            return parent::uninstall();
        }
    
        protected function postProcess()
        {
            $errors = array();
    
            if (Tools::isSubmit('submitMAMerchant'))
            {
                $emails = (string)Tools::getValue('MA_MERCHANT_MAILS_STOCK');
    
                if (!$emails || empty($emails))
                    $errors[] = $this->l('Please type one (or more) e-mail address');
                else
                {
                    $emails = str_replace(',', self::__MA_MAIL_DELIMITOR__, $emails);
                    $emails = explode(self::__MA_MAIL_DELIMITOR__, $emails);
                    foreach ($emails as $k => $email)
                    {
                        $email = trim($email);
                        if (!empty($email) && !Validate::isEmail($email))
                        {
                            $errors[] = $this->l('Invalid e-mail:').' '.Tools::safeOutput($email);
                            break;
                        }
                        elseif (!empty($email) && count($email) > 0)
                            $emails[$k] = $email;
                        else
                            unset($emails[$k]);
                    }
    
                    $emails = implode(self::__MA_MAIL_DELIMITOR__, $emails);
    
                    if (!Configuration::updateValue('MA_MERCHANT_MAILS_STOCK', (string)$emails))
                        $errors[] = $this->l('Cannot update settings');
                }
            }
    
            if (count($errors) > 0)
            {
                $this->html .= $this->displayError(implode('<br />', $errors));
                return $this->init();
            }
    
            parent::postProcess();
        }
    
        public function hookActionUpdateQuantity($params)
        {
            $this->merchant_mails = $this->merchant_mails_stock;
            parent::hookActionUpdateQuantity($params);
        }
    
        public function renderForm()
        {
            $fields_form_1 = array(
                'form' => array(
                    'legend' => array(
                        'title' => $this->l('Customer notifications'),
                        'icon' => 'icon-cogs'
                    ),
                    'input' => array(
                        array(
                            'type' => 'switch',
                            'is_bool' => true, //retro compat 1.5
                            'label' => $this->l('Product availability'),
                            'name' => 'MA_CUSTOMER_QTY',
                            'desc' => $this->l('Gives the customer the option of receiving a notification when an out-of-stock product is available again.'),
                            'values' => array(
                                array(
                                    'id' => 'active_on',
                                    'value' => 1,
                                    'label' => $this->l('Enabled')
                                ),
                                array(
                                    'id' => 'active_off',
                                    'value' => 0,
                                    'label' => $this->l('Disabled')
                                )
                            ),
                        ),
                        array(
                            'type' => 'switch',
                            'is_bool' => true, //retro compat 1.5
                            'label' => $this->l('Order edit'),
                            'name' => 'MA_ORDER_EDIT',
                            'desc' => $this->l('Send a notification to the customer when an order is edited.'),
                            'values' => array(
                                array(
                                    'id' => 'active_on',
                                    'value' => 1,
                                    'label' => $this->l('Enabled')
                                ),
                                array(
                                    'id' => 'active_off',
                                    'value' => 0,
                                    'label' => $this->l('Disabled')
                                )
                            ),
                        ),
                    ),
                    'submit' => array(
                        'title' => $this->l('Save'),
                        'class' => 'btn btn-default pull-right',
                        'name' => 'submitMailAlert',
                    )
                ),
            );
    
            $fields_form_2 = array(
                'form' => array(
                    'legend' => array(
                        'title' => $this->l('Merchant notifications'),
                        'icon' => 'icon-cogs'
                    ),
                    'input' => array(
                        array(
                            'type' => 'switch',
                            'is_bool' => true, //retro compat 1.5
                            'label' => $this->l('New order'),
                            'name' => 'MA_MERCHANT_ORDER',
                            'desc' => $this->l('Receive a notification when an order is placed.'),
                            'values' => array(
                                array(
                                    'id' => 'active_on',
                                    'value' => 1,
                                    'label' => $this->l('Enabled')
                                ),
                                array(
                                    'id' => 'active_off',
                                    'value' => 0,
                                    'label' => $this->l('Disabled')
                                )
                            ),
                        ),
                        array(
                            'type' => 'switch',
                            'is_bool' => true, //retro compat 1.5
                            'label' => $this->l('Out of stock'),
                            'name' => 'MA_MERCHANT_OOS',
                            'desc' => $this->l('Receive a notification if the available quantity of a product is below the following threshold.'),
                            'values' => array(
                                array(
                                    'id' => 'active_on',
                                    'value' => 1,
                                    'label' => $this->l('Enabled')
                                ),
                                array(
                                    'id' => 'active_off',
                                    'value' => 0,
                                    'label' => $this->l('Disabled')
                                )
                            ),
                        ),
                        array(
                            'type' => 'text',
                            'label' => $this->l('Threshold'),
                            'name' => 'MA_LAST_QTIES',
                            'class' => 'fixed-width-xs',
                            'desc' => $this->l('Quantity for which a product is considered out of stock.'),
                        ),
                        array(
                            'type' => 'switch',
                            'is_bool' => true, //retro compat 1.5
                            'label' => $this->l('Coverage warning'),
                            'name' => 'MA_MERCHANT_COVERAGE',
                            'desc' => $this->l('Receive a notification when a product has insufficient coverage.'),
                            'values' => array(
                                array(
                                    'id' => 'active_on',
                                    'value' => 1,
                                    'label' => $this->l('Enabled')
                                ),
                                array(
                                    'id' => 'active_off',
                                    'value' => 0,
                                    'label' => $this->l('Disabled')
                                )
                            ),
                        ),
                        array(
                            'type' => 'text',
                            'label' => $this->l('Coverage'),
                            'name' => 'MA_PRODUCT_COVERAGE',
                            'class' => 'fixed-width-xs',
                            'desc' => $this->l('Stock coverage, in days. Also, the stock coverage of a given product will be calculated based on this number.'),
                        ),
                        array(
                            'type' => 'switch',
                            'is_bool' => true, //retro compat 1.5
                            'label' => $this->l('Returns'),
                            'name' => 'MA_RETURN_SLIP',
                            'desc' => $this->l('Receive a notification when a customer requests a merchandise return.'),
                            'values' => array(
                                array(
                                    'id' => 'active_on',
                                    'value' => 1,
                                    'label' => $this->l('Enabled')
                                ),
                                array(
                                    'id' => 'active_off',
                                    'value' => 0,
                                    'label' => $this->l('Disabled')
                                )
                            ),
                        ),
                        array(
                            'type' => 'textarea',
                            'cols' => 36,
                            'rows' => 4,
                            'label' => $this->l('E-mail addresses'),
                            'name' => 'MA_MERCHANT_MAILS',
                            'desc' => $this->l('One e-mail address per line (e.g. [email protected]).'),
                        ),
                        array(
                            'type' => 'textarea',
                            'cols' => 36,
                            'rows' => 4,
                            'label' => $this->l('E-mail stock addresses'),
                            'name' => 'MA_MERCHANT_MAILS_STOCK',
                            'desc' => $this->l('One e-mail address per line (e.g. [email protected]).'),
                        ),
                    ),
                    'submit' => array(
                        'title' => $this->l('Save'),
                        'class' => 'btn btn-default pull-right',
                        'name' => 'submitMAMerchant',
                    )
                ),
            );
    
            $helper = new HelperForm();
            $helper->show_toolbar = false;
            $helper->table = $this->table;
            $lang = new Language((int)Configuration::get('PS_LANG_DEFAULT'));
            $helper->default_form_language = $lang->id;
            $helper->module = $this;
            $helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') ? Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') : 0;
            $helper->identifier = $this->identifier;
            $helper->submit_action = 'submitMailAlertConfiguration';
            $helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false)
                .'&configure='.$this->name
                .'&tab_module='.$this->tab
                .'&module_name='.$this->name;
            $helper->token = Tools::getAdminTokenLite('AdminModules');
            $helper->tpl_vars = array(
                'fields_value' => $this->getConfigFieldsValues(),
                'languages' => $this->context->controller->getLanguages(),
                'id_language' => $this->context->language->id
            );
    
            return $helper->generateForm(array($fields_form_1, $fields_form_2));
        }
    
        public function getConfigFieldsValues()
        {
            $config = parent::getConfigFieldsValues();
            $config['MA_MERCHANT_MAILS_STOCK'] = Tools::getValue('MA_MERCHANT_MAILS_STOCK', Configuration::get('MA_MERCHANT_MAILS_STOCK'));
            return $config;
        }
    }