{"id":2404,"date":"2022-03-10T22:17:54","date_gmt":"2022-03-10T20:17:54","guid":{"rendered":"https:\/\/www.h-hennes.fr\/blog\/?p=2404"},"modified":"2022-03-30T22:11:53","modified_gmt":"2022-03-30T20:11:53","slug":"prestashop-filtrer-les-emails-de-produits-hors-stocks","status":"publish","type":"post","link":"https:\/\/www.h-hennes.fr\/blog\/2022\/03\/10\/prestashop-filtrer-les-emails-de-produits-hors-stocks\/","title":{"rendered":"Prestashop : Filtrer les emails de produits hors stocks"},"content":{"rendered":"\n<p>Dans Prestashop il est possible d&rsquo;\u00eatre notifi\u00e9 lorsque des commandes sont pass\u00e9es sur des produits qui ne sont bient\u00f4t plus en stock.<br>Ce param\u00e9trage est accessible depuis l&rsquo;administration sur chaque fiche produit en cochant la case \u00ab\u00a0M&rsquo;envoyer un email quand la quantit\u00e9 est en dessous ou \u00e9gale \u00e0 ce niveau.\u00a0\u00bb<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.h-hennes.fr\/blog\/wp-content\/uploads\/2022\/03\/image.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1018\" height=\"439\" src=\"https:\/\/www.h-hennes.fr\/blog\/wp-content\/uploads\/2022\/03\/image.png\" alt=\"\" class=\"wp-image-2405\" srcset=\"https:\/\/www.h-hennes.fr\/blog\/wp-content\/uploads\/2022\/03\/image.png 1018w, https:\/\/www.h-hennes.fr\/blog\/wp-content\/uploads\/2022\/03\/image-300x129.png 300w, https:\/\/www.h-hennes.fr\/blog\/wp-content\/uploads\/2022\/03\/image-768x331.png 768w\" sizes=\"auto, (max-width: 1018px) 100vw, 1018px\" \/><\/a><\/figure>\n\n\n\n<p>Cette fonctionnalit\u00e9 est tr\u00e8s pratique pour le marchant, mais en revanche ces emails n&rsquo;ont aucun int\u00e9r\u00eat pour le d\u00e9veloppeur ou l&rsquo;agence qui g\u00e8re le site.<br>C&rsquo;est la ou le fonctionnement natif de Prestashop n&rsquo;est pas tr\u00e8s pratique, car pour chaque commande d&rsquo;un produit avec cette option coch\u00e9e,<strong> l&rsquo;ensemble des utilisateurs de l&rsquo;administration<\/strong>&nbsp; avec un acc\u00e8s en lecture \u00e0 la page des stocks vont \u00eatre notifi\u00e9s.<br><br>Ces derniers temps je suis spamm\u00e9 par ce genre d&#8217;email et j&rsquo;ai donc d\u00e9cid\u00e9 de m&rsquo;attaquer au probl\u00e8me \ud83d\ude42<br><br>Une solution simple pourrait \u00eatre de retirer cet acc\u00e8s pour les profils administrateurs, mais ceci n\u00e9cessite de cr\u00e9er un nouveau r\u00f4le sp\u00e9cial et de ne pas avoir de visualisation sur cette page en back office.<br><br>Nous allons voir qu&rsquo;il est facile de contourner ce comportement et que le meilleur email est celui qui ne part pas !<br>Nous allons donc le bloquer d\u00e8s l&rsquo;envoi \ud83d\ude42<\/p>\n<p>Pour cela comme d&rsquo;habitude \u00e7a se passe via un module qui va s\u2019appeler <strong>hhoutofstockmailsfilter<\/strong> et dans lequel nous allons utiliser le hook <strong>actionEmailSendBefore<\/strong> pour bloquer l&rsquo;envoi de l&#8217;email ( cf. mon article d\u00e9taill\u00e9 sur le fonctionnement des emails : <a title=\"Prestashop : g\u00e9rer les emails dans vos modules\" href=\"https:\/\/www.h-hennes.fr\/blog\/2021\/02\/01\/prestashop-gerer-les-emails-dans-vos-modules\/\">Prestashop : g\u00e9rer les emails dans vos modules<\/a> )<\/p>\n<p>Dans ce module nous pourrons configurer les \u00e9l\u00e9ments suivants :<\/p>\n<ul>\n<li>Mode de fonctionnement ( AllowList \/ BlockList )<\/li>\n<li>Liste des emails concern\u00e9s<\/li>\n<li>Logs ou non de l&rsquo;action de filtre dans les logs Prestashop ( tr\u00e8s pratique si quelqu&rsquo;un d&rsquo;ext\u00e9rieur doit debug \ud83d\ude09 )<\/li>\n<\/ul>\n<p>Voici une visualisation du panneau de configuration :<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.h-hennes.fr\/blog\/wp-content\/uploads\/2022\/03\/image-1.png\"><img loading=\"lazy\" decoding=\"async\" width=\"997\" height=\"330\" src=\"https:\/\/www.h-hennes.fr\/blog\/wp-content\/uploads\/2022\/03\/image-1.png\" alt=\"\" class=\"wp-image-2406\" srcset=\"https:\/\/www.h-hennes.fr\/blog\/wp-content\/uploads\/2022\/03\/image-1.png 997w, https:\/\/www.h-hennes.fr\/blog\/wp-content\/uploads\/2022\/03\/image-1-300x99.png 300w, https:\/\/www.h-hennes.fr\/blog\/wp-content\/uploads\/2022\/03\/image-1-768x254.png 768w\" sizes=\"auto, (max-width: 997px) 100vw, 997px\" \/><\/a><\/figure>\n\n\n\n<p>Et voici l&rsquo;ensemble du code du module \ud83d\ude09<\/p>\n\n\n\n<pre escaped=\"true\" lang=\"php\">\nif (!defined('_PS_VERSION_')) {\n    exit;\n}\n\n\nclass Hhoutofstockmailsfilter extends Module\n{\n    \/**\n     * @var string\n     *\/\n    protected $configPrefix;\n\n    public function __construct()\n    {\n        $this-&gt;name = 'hhoutofstockmailsfilter';\n        $this-&gt;tab = 'others';\n        $this-&gt;version = '0.1.0';\n        $this-&gt;author = 'hhennes';\n        $this-&gt;bootstrap = true;\n        parent::__construct();\n\n        $this-&gt;displayName = $this-&gt;l('Hh out of stock Filter');\n        $this-&gt;description = $this-&gt;l('Control which user will receive outofstocks emails');\n        $this-&gt;configPrefix = strtoupper($this-&gt;name) . '_';\n    }\n\n    \/**\n     * Installation du module\n     * @return bool\n     *\/\n    public function install()\n    {\n        if (\n            !parent::install()\n            || !$this-&gt;registerHook('actionEmailSendBefore')\n        ) {\n            return false;\n        }\n\n        return true;\n    }\n\n    \/**\n     * Hook ex\u00e9cut\u00e9 avant d'envoyer un email\n     * @param array $params\n     * @return false|void\n     *\/\n    public function hookActionEmailSendBefore(array $params)\n    {\n        if ($params['template'] == 'productoutofstock') {\n\n            \/\/R\u00e9cup\u00e9ration de la liste des emails\n            $emails = array_map('trim',\n                explode(',', Configuration::get($this-&gt;configPrefix . 'EMAILS'))\n            );\n\n            \/\/R\u00e9cup\u00e9ration du type de filtre\n            $filterMode = Configuration::get($this-&gt;configPrefix . 'MODE');\n            $filterMode == 'ALLOWED' ?\n                $sendEMail = in_array($params['to'], $emails) :\n                $sendEMail = !in_array($params['to'], $emails);\n\n            \/\/Blocage de l'email\n            if (!$sendEMail) {\n                if (Configuration::get($this-&gt;configPrefix . 'ENABLE_LOGS') == 1) {\n                    PrestaShopLogger::addLog(\n                        'productoutofstock email was prevent to be send by module ' . $this-&gt;name\n                    );\n                }\n                return false; \/\/Si on renvoie false l'email n'est pas envoy\u00e9\n            }\n        }\n    }\n\n    \/**\n     * Configuration du module\n     * @return string\n     * @throws PrestaShopDatabaseException\n     * @throws PrestaShopException\n     *\/\n    public function getContent()\n    {\n        $html = '';\n        $html .= $this-&gt;postProcess();\n        $html .= $this-&gt;renderForm();\n\n        return $html;\n    }\n\n    \/**\n     * Gestion de l'affichage du formulaire\n     * @return string\n     * @throws PrestaShopDatabaseException\n     * @throws PrestaShopException\n     *\/\n    public function renderForm(): string\n    {\n        $fields_form = [\n            'form' =&gt; [\n                'legend' =&gt; [\n                    'title' =&gt; $this-&gt;l('Module Configuration'),\n                    'icon' =&gt; 'icon-cogs',\n                ],\n                'input' =&gt; [\n                    [\n                        'type' =&gt; 'switch',\n                        'label' =&gt; $this-&gt;l('Behavior for the filter '),\n                        'name' =&gt; $this-&gt;configPrefix . 'MODE',\n                        'required' =&gt; true,\n                        'hint' =&gt; $this-&gt;l('With mode allowed only the email in the list will receive the emails, otherwise they will not.'),\n                        'class' =&gt; 't',\n                        'values' =&gt; [\n                            [\n                                'id' =&gt; 'active_on',\n                                'value' =&gt; 'ALLOWED',\n                                'label' =&gt; $this-&gt;l('Allowed'),\n                            ],\n                            [\n                                'id' =&gt; 'active_off',\n                                'value' =&gt; 'EXCLUDED',\n                                'label' =&gt; $this-&gt;l('Excluded'),\n                            ],\n                        ],\n                    ],\n                    [\n                        'type' =&gt; 'text',\n                        'label' =&gt; $this-&gt;l('Allowed\/excluded emails comas separated'),\n                        'name' =&gt; $this-&gt;configPrefix . 'EMAILS',\n                        'required' =&gt; true,\n                        'hint' =&gt; $this-&gt;l('Depending configuration Only the emails listed here will receive\/no receive the out of stocks emails'),\n                    ],\n                    [\n                        'type' =&gt; 'switch',\n                        'label' =&gt; $this-&gt;l('Log blocked emails'),\n                        'name' =&gt; $this-&gt;configPrefix . 'ENABLE_LOGS',\n                        'required' =&gt; true,\n                        'hint' =&gt; $this-&gt;l('The blocked emails will be listed in prestashop log table'),\n                        'class' =&gt; 't',\n                        'is_bool' =&gt; true,\n                        'values' =&gt; [\n                            [\n                                'id' =&gt; 'active_on',\n                                'value' =&gt; 1,\n                                'label' =&gt; $this-&gt;l('Enabled'),\n                            ],\n                            [\n                                'id' =&gt; 'active_off',\n                                'value' =&gt; 0,\n                                'label' =&gt; $this-&gt;l('Disabled'),\n                            ],\n                        ],\n                    ],\n                ],\n                'submit' =&gt; [\n                    'title' =&gt; $this-&gt;l('Save'),\n                    'class' =&gt; 'button btn btn-default pull-right',\n                ],\n            ],\n        ];\n\n        $helper = new HelperForm();\n        $helper-&gt;show_toolbar = false;\n        $lang = new Language((int)Configuration::get('PS_LANG_DEFAULT'));\n        $helper-&gt;default_form_language = $lang-&gt;id;\n        $helper-&gt;allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') ?\n            Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') : 0;\n        $helper-&gt;id = $this-&gt;name;\n        $helper-&gt;submit_action = 'SubmitModuleConfiguration';\n        $helper-&gt;currentIndex = $this-&gt;context-&gt;link-&gt;getAdminLink('AdminModules', false)\n            . '&amp;configure=' . $this-&gt;name . '&amp;tab_module=' . $this-&gt;tab . '&amp;module_name=' . $this-&gt;name;\n        $helper-&gt;token = Tools::getAdminTokenLite('AdminModules');\n        $helper-&gt;tpl_vars = [\n            'fields_value' =&gt; $this-&gt;getConfigFieldsValues(),\n            'languages' =&gt; $this-&gt;context-&gt;controller-&gt;getLanguages(),\n            'id_language' =&gt; $this-&gt;context-&gt;language-&gt;id,\n        ];\n\n        return $helper-&gt;generateForm([$fields_form]);\n\n    }\n\n    \/**\n     * Traitement du formulaire\n     * @return string|void\n     *\/\n    public function postProcess()\n    {\n        if (Tools::isSubmit('SubmitModuleConfiguration')) {\n            Configuration::updateValue($this-&gt;configPrefix . 'MODE', Tools::getValue($this-&gt;configPrefix . 'MODE'));\n            Configuration::updateValue($this-&gt;configPrefix . 'EMAILS', Tools::getValue($this-&gt;configPrefix . 'EMAILS'));\n            Configuration::updateValue($this-&gt;configPrefix . 'ENABLE_LOGS', Tools::getValue($this-&gt;configPrefix . 'ENABLE_LOGS'));\n            return $this-&gt;displayConfirmation($this-&gt;l('Settings updated'));\n        }\n\n    }\n\n    \/**\n     * R\u00e9cup\u00e9ration des valeurs de configuration du formulaire\n     * @return array\n     *\/\n    public function getConfigFieldsValues(): array\n    {\n        return [\n            $this-&gt;configPrefix . 'MODE' =&gt; Tools::getValue($this-&gt;configPrefix . 'MODE', Configuration::get($this-&gt;configPrefix . 'MODE')),\n            $this-&gt;configPrefix . 'EMAILS' =&gt; Tools::getValue($this-&gt;configPrefix . 'EMAILS', Configuration::get($this-&gt;configPrefix . 'EMAILS')),\n            $this-&gt;configPrefix . 'ENABLE_LOGS' =&gt; Tools::getValue($this-&gt;configPrefix . 'ENABLE_LOGS', Configuration::get($this-&gt;configPrefix . 'ENABLE_LOGS')),\n        ];\n    }\n\n\n}\n<\/pre>\n\n\n\n<p>Et voila une fois le module install\u00e9 de spams avec les emails de produits hors stocks \ud83d\ude42<\/p>\n\n\n\n<p class=\"has-text-align-center has-ast-global-color-0-color has-luminous-vivid-amber-background-color has-text-color has-background\"><a href=\"https:\/\/shop.h-hennes.fr\/fr\/5-filtre-sur-les-emails-de-produits-hors-stock.html\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"T\u00e9l\u00e9charger le module sur la boutique\">T\u00e9l\u00e9charger le module complet ( et gratuit ) sur la boutique<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Dans Prestashop il est possible d&rsquo;\u00eatre notifi\u00e9 lorsque des commandes sont pass\u00e9es sur des produits qui ne sont bient\u00f4t plus en stock.Ce param\u00e9trage est accessible depuis l&rsquo;administration sur chaque fiche produit en cochant la case \u00ab\u00a0M&rsquo;envoyer un email quand la quantit\u00e9 est en dessous ou \u00e9gale \u00e0 ce niveau.\u00a0\u00bb Cette fonctionnalit\u00e9 est tr\u00e8s pratique pour [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[245],"tags":[329,585,483],"class_list":["post-2404","post","type-post","status-publish","format-standard","hentry","category-prestashop-2","tag-email","tag-out-of-stock","tag-prestashop-1-7","prestashop-1-7","prestashop-1-7-8"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/posts\/2404","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/comments?post=2404"}],"version-history":[{"count":6,"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/posts\/2404\/revisions"}],"predecessor-version":[{"id":2434,"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/posts\/2404\/revisions\/2434"}],"wp:attachment":[{"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/media?parent=2404"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/categories?post=2404"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/tags?post=2404"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}