Jump to content

Ajout d'un message auto lors d'une commande


Recommended Posts

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();
    }
}

 

Edited by @rthur (see edit history)
Link to comment
Share on other sites

J'ai testé avec ces infos mais rien non plus

22 minutes ago, Prestashop Addict said:

Il faut ajouter des éléments

                    $msg = new Message();
                    $msg->message = $message;
                    $msg->id_cart = (int) $id_cart;
                    $msg->id_customer = (int) $id_customer;
                    $msg->id_order = (int) $orderId;
                    $msg->private = true;
                    $msg->add();

 

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...