Jump to content

Edit History

@rthur

@rthur

Salut,

Je suis sous Presta 8.1.3 et j'essaie de faire un petit module pour afficher un message en backoffice + envoi d'un email spécifique au client lors de la commande de certains produits spécifiques. Mon email est bien envoyé mais le message ne d'affiche pas en BO de la commande alors qu'il est bien stocké en bdd. Une idée du souci ?

Merci

<?php

if (!defined('_PS_VERSION_')) {
    exit;
}

class SendCustomEmail extends Module
{
    public function __construct()
    {
        $this->name = 'sendcustomemail';
        $this->tab = 'administration';
        $this->version = '1.0.0';
        $this->author = 'XXX';
        $this->need_instance = 0;

        parent::__construct();

        $this->displayName = $this->l('Send Custom Email');
        $this->description = $this->l('Envoi d\'emails d\'information suite à la commande de certains produits');
    }

    public function install()
    {
        return parent::install() && $this->registerHook('actionValidateOrder');
    }

    public function hookActionValidateOrder($params)
    {
        try {
            $order = $params['order'];
            $products = $order->getProducts();
                                    
            // Mapping id produits et categories
            $categoryEmailMap = [
                'template_1' => ['categories' => [63, 77], 'subject' => 'Sujet email'],
            ];
            
            
            $productEmailMap = [
                'template_2' => ['products' => [160], 'subject' => 'Sujet email'],
                'template_3' => ['products' => [103, 104], 'subject' => 'Sujet email'],
            ];
            

            foreach ($products as $product) {
                $productCategories = Product::getProductCategories($product['product_id']);
                
                // Check match categories
                foreach ($categoryEmailMap as $template => $details) {
                    if (!empty(array_intersect($productCategories, $details['categories']))) {
                        $emailDetails = ['template' => $template, 'subject' => $details['subject']];
                        $this->sendCustomEmail($order, $emailDetails);
                        break 2;
                    }
                }
                
                // Check match produits
                foreach ($productEmailMap as $template => $details) {
                    if (in_array($product['product_id'], $details['products'])) {
                        $emailDetails = ['template' => $template, 'subject' => $details['subject']];
                        $this->sendCustomEmail($order, $emailDetails);
                        break 2; 
                    }
                }
            }
        } catch (Exception $e) {
            PrestaShopLogger::addLog('Error in hookActionValidateOrder: ' . $e->getMessage(), 3, null, 'SendCustomEmail', null, true);
        }
    }

    protected function sendCustomEmail($order, $emailDetails)
    {
        try {
            $customer = new Customer($order->id_customer);
            $email = $customer->email;

            $subject = $emailDetails['subject'];
            $template = $emailDetails['template']; 
            $templateVars = [
                '{firstname}' => $customer->firstname,
                '{lastname}' => $customer->lastname,
                '{order_id}' => $order->id,
                '{order_name}' => $order->reference,
            ];

            $langIso = Language::getIsoById((int)$order->id_lang);

            $templatePathHtml = _PS_MAIL_DIR_ . $langIso . '/' . $template . '.html';
            $templatePathTxt = _PS_MAIL_DIR_ . $langIso . '/' . $template . '.txt';


            if (file_exists($templatePathHtml) && file_exists($templatePathTxt)) {

                $mailSent = Mail::Send(
                    (int)$order->id_lang,
                    $template, 
                    $subject,
                    $templateVars,
                    $email,
                    $customer->firstname . ' ' . $customer->lastname,
                    null,
                    null,
                    null,
                    null,
                    _PS_MAIL_DIR_,
                    false,
                    (int)$order->id_shop
                );

                if ($mailSent) {
                    PrestaShopLogger::addLog('Email sent to ' . $email, 1, null, 'SendCustomEmail', null, true);
                    $this->addOrderMessage($order->id, utf8_encode('Email envoyé'));
                } else {
                    PrestaShopLogger::addLog('Failed to send email to ' . $email, 3, null, 'SendCustomEmail', null, true);
                    $this->addOrderMessage($order->id, utf8_encode('Échec de l\'envoi de l\'email'));
                }
            } else {
                PrestaShopLogger::addLog('Template files do not exist: ' . $templatePathHtml . ' or ' . $templatePathTxt, 3, null, 'SendCustomEmail', null, true);
                $this->addOrderMessage($order->id, utf8_encode('Les fichiers de modèle d\'email n\'existent pas'));
            }
        } catch (Exception $e) {
            PrestaShopLogger::addLog('Error in sendCustomEmail: ' . $e->getMessage(), 3, null, 'SendCustomEmail', null, true);
            $this->addOrderMessage($order->id, utf8_encode('Erreur lors de l\'envoi de l\'email : ' . $e->getMessage()));
        }
    }

    protected function addOrderMessage($orderId, $message)
    {
        $msg = new Message();
        $msg->message = $message;
        $msg->id_order = (int) $orderId;
        $msg->private = 1;
        $msg->add();
    }
}

 

@rthur

@rthur

Salut,

Je suis sous Presta 8.1.3 et j'essaie de faire un petit module pour afficher un message en backoffice + envoi d'un email spécifique au client lors de la commande de certains produits spécifiques. Mon email est bien envoyé mais le message ne d'affiche pas en BO de la commande alors qu'il est bien stocké en bdd. Une idée du souci ?

Merci

<?php

if (!defined('_PS_VERSION_')) {
    exit;
}

class SendCustomEmail extends Module
{
    public function __construct()
    {
        $this->name = 'sendcustomemail';
        $this->tab = 'administration';
        $this->version = '1.0.0';
        $this->author = 'XXX';
        $this->need_instance = 0;

        parent::__construct();

        $this->displayName = $this->l('Send Custom Email');
        $this->description = $this->l('Envoi d\'emails d\'information suite à la commande de certains produits');
    }

    public function install()
    {
        return parent::install() && $this->registerHook('actionValidateOrder');
    }

    public function hookActionValidateOrder($params)
    {
        try {
            $order = $params['order'];
            $products = $order->getProducts();
                                    
            // Mapping id produits et categories
            $categoryEmailMap = [
                'custom_email_num_serie' => ['categories' => [63, 77], 'subject' => 'Sujet email'],
            ];
            
            
            $productEmailMap = [
                'template_1' => ['products' => [160], 'subject' => 'Sujet 1'],
                'template_2' => ['products' => [103, 104], 'subject' => 'Sujet 2'],
            ];
            

            foreach ($products as $product) {
                $productCategories = Product::getProductCategories($product['product_id']);
                
                // Check match categories
                foreach ($categoryEmailMap as $template => $details) {
                    if (!empty(array_intersect($productCategories, $details['categories']))) {
                        $emailDetails = ['template' => $template, 'subject' => $details['subject']];
                        $this->sendCustomEmail($order, $emailDetails);
                        break 2; 
                    }
                }
                
                // Check match produits
                foreach ($productEmailMap as $template => $details) {
                    if (in_array($product['product_id'], $details['products'])) {
                        $emailDetails = ['template' => $template, 'subject' => $details['subject']];
                        $this->sendCustomEmail($order, $emailDetails);
                        break 2; 
                    }
                }
            }
        } catch (Exception $e) {
            PrestaShopLogger::addLog('Error in hookActionValidateOrder: ' . $e->getMessage(), 3, null, 'SendCustomEmail', null, true);
        }
    }

    protected function sendCustomEmail($order, $emailDetails)
    {
        try {
            $customer = new Customer($order->id_customer);
            $email = $customer->email;

            $subject = $emailDetails['subject'];
            $template = $emailDetails['template']; 
            $templateVars = [
                '{firstname}' => $customer->firstname,
                '{lastname}' => $customer->lastname,
                '{order_id}' => $order->id,
                '{order_name}' => $order->reference,
            ];

            $langIso = Language::getIsoById((int)$order->id_lang);

            $templatePathHtml = _PS_MAIL_DIR_ . $langIso . '/' . $template . '.html';
            $templatePathTxt = _PS_MAIL_DIR_ . $langIso . '/' . $template . '.txt';


            if (file_exists($templatePathHtml) && file_exists($templatePathTxt)) {

                $mailSent = Mail::Send(
                    (int)$order->id_lang,
                    $template, 
                    $subject,
                    $templateVars,
                    $email,
                    $customer->firstname . ' ' . $customer->lastname,
                    null,
                    null,
                    null,
                    null,
                    _PS_MAIL_DIR_,
                    false,
                    (int)$order->id_shop
                );

                if ($mailSent) {
                    PrestaShopLogger::addLog('Email sent to ' . $email, 1, null, 'SendCustomEmail', null, true);
                    $this->addOrderMessage($order->id, utf8_encode('Email envoyé'));
                } else {
                    PrestaShopLogger::addLog('Failed to send email to ' . $email, 3, null, 'SendCustomEmail', null, true);
                    $this->addOrderMessage($order->id, utf8_encode('Échec de l\'envoi de l\'email'));
                }
            } else {
                PrestaShopLogger::addLog('Template files do not exist: ' . $templatePathHtml . ' or ' . $templatePathTxt, 3, null, 'SendCustomEmail', null, true);
                $this->addOrderMessage($order->id, utf8_encode('Les fichiers de modèle d\'email n\'existent pas'));
            }
        } catch (Exception $e) {
            PrestaShopLogger::addLog('Error in sendCustomEmail: ' . $e->getMessage(), 3, null, 'SendCustomEmail', null, true);
            $this->addOrderMessage($order->id, utf8_encode('Erreur lors de l\'envoi de l\'email : ' . $e->getMessage()));
        }
    }

    protected function addOrderMessage($orderId, $message)
    {
        $msg = new Message();
        $msg->message = $message;
        $msg->id_order = (int) $orderId;
        $msg->private = 1;
        $msg->add();
    }
}

 

@rthur

@rthur

Salut,

Je suis sous Presta 8.1.3 et j'essaie de faire un petit module pour afficher un message en backoffice + envoi d'un email spécifique au client lors de la commande de certains produits spécifiques. Mon email est bien envoyé mais le message ne d'affiche pas en BO de la commande alors qu'il est bien stocké en bdd. Une idée du souci ?

Merci

<?php

if (!defined('_PS_VERSION_')) {
    exit;
}

class SendCustomEmail extends Module
{
    public function __construct()
    {
        $this->name = 'sendcustomemail';
        $this->tab = 'administration';
        $this->version = '1.0.0';
        $this->author = 'XXX';
        $this->need_instance = 0;

        parent::__construct();

        $this->displayName = $this->l('Send Custom Email');
        $this->description = $this->l('Envoi d\'emails d\'information suite à la commande de certains produits');
    }

    public function install()
    {
        return parent::install() && $this->registerHook('actionValidateOrder');
    }

    public function hookActionValidateOrder($params)
    {
        try {
            $order = $params['order'];
            $products = $order->getProducts();
                                    
            // Mapping id produits et categories
            $categoryEmailMap = [
                'custom_email' => ['categories' => [63, 77], 'subject' => 'Sujet email'],
            ];
            
            
            $productEmailMap = [
                'template_1' => ['products' => [160], 'subject' => 'Sujet 1'],
                'template_2' => ['products' => [103, 104], 'subject' => 'Sujet 2'],
            ];
            

            foreach ($products as $product) {
                $productCategories = Product::getProductCategories($product['product_id']);
                
                // Check match categories
                foreach ($categoryEmailMap as $template => $details) {
                    if (!empty(array_intersect($productCategories, $details['categories']))) {
                        $emailDetails = ['template' => $template, 'subject' => $details['subject']];
                        $this->sendCustomEmail($order, $emailDetails);
                        break 2; // Break out of both foreach loops
                    }
                }
                
                // Check match produits
                foreach ($productEmailMap as $template => $details) {
                    if (in_array($product['product_id'], $details['products'])) {
                        $emailDetails = ['template' => $template, 'subject' => $details['subject']];
                        $this->sendCustomEmail($order, $emailDetails);
                        break 2; // Break out of both foreach loops
                    }
                }
            }
        } catch (Exception $e) {
            PrestaShopLogger::addLog('Error in hookActionValidateOrder: ' . $e->getMessage(), 3, null, 'SendCustomEmail', null, true);
        }
    }

    protected function sendCustomEmail($order, $emailDetails)
    {
        try {
            $customer = new Customer($order->id_customer);
            $email = $customer->email;

            $subject = $emailDetails['subject'];
            $template = $emailDetails['template']; 
            $templateVars = [
                '{firstname}' => $customer->firstname,
                '{lastname}' => $customer->lastname,
                '{order_id}' => $order->id,
                '{order_name}' => $order->reference,
            ];

            $langIso = Language::getIsoById((int)$order->id_lang);

            $templatePathHtml = _PS_MAIL_DIR_ . $langIso . '/' . $template . '.html';
            $templatePathTxt = _PS_MAIL_DIR_ . $langIso . '/' . $template . '.txt';


            if (file_exists($templatePathHtml) && file_exists($templatePathTxt)) {

                $mailSent = Mail::Send(
                    (int)$order->id_lang,
                    $template, 
                    $subject,
                    $templateVars,
                    $email,
                    $customer->firstname . ' ' . $customer->lastname,
                    null,
                    null,
                    null,
                    null,
                    _PS_MAIL_DIR_,
                    false,
                    (int)$order->id_shop
                );

                if ($mailSent) {
                    PrestaShopLogger::addLog('Email sent to ' . $email, 1, null, 'SendCustomEmail', null, true);
                    $this->addOrderMessage($order->id, utf8_encode('Email envoyé'));
                } else {
                    PrestaShopLogger::addLog('Failed to send email to ' . $email, 3, null, 'SendCustomEmail', null, true);
                    $this->addOrderMessage($order->id, utf8_encode('Échec de l\'envoi de l\'email'));
                }
            } else {
                PrestaShopLogger::addLog('Template files do not exist: ' . $templatePathHtml . ' or ' . $templatePathTxt, 3, null, 'SendCustomEmail', null, true);
                $this->addOrderMessage($order->id, utf8_encode('Les fichiers de modèle d\'email n\'existent pas'));
            }
        } catch (Exception $e) {
            PrestaShopLogger::addLog('Error in sendCustomEmail: ' . $e->getMessage(), 3, null, 'SendCustomEmail', null, true);
            $this->addOrderMessage($order->id, utf8_encode('Erreur lors de l\'envoi de l\'email : ' . $e->getMessage()));
        }
    }

    protected function addOrderMessage($orderId, $message)
    {
        $msg = new Message();
        $msg->message = $message;
        $msg->id_order = (int) $orderId;
        $msg->private = 1;
        $msg->add();
    }
}

 

@rthur

@rthur

Salut,

Je suis sous Presta 8.1.3 et j'essaie de faire un petit module pour afficher un message en backoffice + envoi d'un email spécifique au client lors de la commande de certains produits spécifiques. Mon email est bien envoyé mais le message ne d'affiche pas en BO de la commande alors qu'il est bien stocké en bdd. Une idée du souci ?

Merci

<?php

if (!defined('_PS_VERSION_')) {
    exit;
}

class SendCustomEmail extends Module
{
    public function __construct()
    {
        $this->name = 'sendcustomemail';
        $this->tab = 'administration';
        $this->version = '1.0.0';
        $this->author = 'XXX';
        $this->need_instance = 0;

        parent::__construct();

        $this->displayName = $this->l('Send Custom Email');
        $this->description = $this->l('Envoi d\'emails d\'information suite à la commande de certains produits');
    }

    public function install()
    {
        return parent::install() && $this->registerHook('actionValidateOrder');
    }

    public function hookActionValidateOrder($params)
    {
        try {
            $order = $params['order'];
            $products = $order->getProducts();
                                    
            // Mapping id produits et categories
            $categoryEmailMap = [
                'custom_email_num_serie' => ['categories' => [63, 77], 'subject' => 'Sujet email'],
            ];
            
            
            $productEmailMap = [
                'template_1' => ['products' => [160], 'subject' => 'Sujet 1'],
                'template_2' => ['products' => [103, 104], 'subject' => 'Sujet 2'],
            ];
            

            foreach ($products as $product) {
                $productCategories = Product::getProductCategories($product['product_id']);
                
                // Check match categories
                foreach ($categoryEmailMap as $template => $details) {
                    if (!empty(array_intersect($productCategories, $details['categories']))) {
                        $emailDetails = ['template' => $template, 'subject' => $details['subject']];
                        $this->sendCustomEmail($order, $emailDetails);
                        break 2; // Break out of both foreach loops
                    }
                }
                
                // Check match produits
                foreach ($productEmailMap as $template => $details) {
                    if (in_array($product['product_id'], $details['products'])) {
                        $emailDetails = ['template' => $template, 'subject' => $details['subject']];
                        $this->sendCustomEmail($order, $emailDetails);
                        break 2; // Break out of both foreach loops
                    }
                }
            }
        } catch (Exception $e) {
            PrestaShopLogger::addLog('Error in hookActionValidateOrder: ' . $e->getMessage(), 3, null, 'SendCustomEmail', null, true);
        }
    }

    protected function sendCustomEmail($order, $emailDetails)
    {
        try {
            $customer = new Customer($order->id_customer);
            $email = $customer->email;

            $subject = $emailDetails['subject'];
            $template = $emailDetails['template']; 
            $templateVars = [
                '{firstname}' => $customer->firstname,
                '{lastname}' => $customer->lastname,
                '{order_id}' => $order->id,
                '{order_name}' => $order->reference,
            ];

            $langIso = Language::getIsoById((int)$order->id_lang);

            $templatePathHtml = _PS_MAIL_DIR_ . $langIso . '/' . $template . '.html';
            $templatePathTxt = _PS_MAIL_DIR_ . $langIso . '/' . $template . '.txt';


            if (file_exists($templatePathHtml) && file_exists($templatePathTxt)) {

                $mailSent = Mail::Send(
                    (int)$order->id_lang,
                    $template, 
                    $subject,
                    $templateVars,
                    $email,
                    $customer->firstname . ' ' . $customer->lastname,
                    null,
                    null,
                    null,
                    null,
                    _PS_MAIL_DIR_,
                    false,
                    (int)$order->id_shop
                );

                if ($mailSent) {
                    PrestaShopLogger::addLog('Email sent to ' . $email, 1, null, 'SendCustomEmail', null, true);
                    $this->addOrderMessage($order->id, utf8_encode('Email envoyé'));
                } else {
                    PrestaShopLogger::addLog('Failed to send email to ' . $email, 3, null, 'SendCustomEmail', null, true);
                    $this->addOrderMessage($order->id, utf8_encode('Échec de l\'envoi de l\'email'));
                }
            } else {
                PrestaShopLogger::addLog('Template files do not exist: ' . $templatePathHtml . ' or ' . $templatePathTxt, 3, null, 'SendCustomEmail', null, true);
                $this->addOrderMessage($order->id, utf8_encode('Les fichiers de modèle d\'email n\'existent pas'));
            }
        } catch (Exception $e) {
            PrestaShopLogger::addLog('Error in sendCustomEmail: ' . $e->getMessage(), 3, null, 'SendCustomEmail', null, true);
            $this->addOrderMessage($order->id, utf8_encode('Erreur lors de l\'envoi de l\'email : ' . $e->getMessage()));
        }
    }

    protected function addOrderMessage($orderId, $message)
    {
        $msg = new Message();
        $msg->message = $message;
        $msg->id_order = (int) $orderId;
        $msg->private = 1;
        $msg->add();
    }
}

 

×
×
  • Create New...