Jump to content

Change prestashop email subject (add order number)


Karolek

Recommended Posts

Hello:

You need to set the order ID on the confirmation email. Look for it at /classes/PaymentModule.php and change it like this:

if (Validate::isEmail($this->context->customer->email)) {
	Mail::Send(
        (int)$order->id_lang,
		'order_conf',
		$order->id.' '.Context::getContext()->getTranslator()->trans(
			'Order confirmation',
			array(),
			'Emails.Subject',
			$orderLanguage->locale
		),
		$data,
		$this->context->customer->email,
		$this->context->customer->firstname.' '.$this->context->customer->lastname,
		null,
		null,
		$file_attachement,
		null, _PS_MAIL_DIR_, false, (int)$order->id_shop
	);
}

Regards

  • Thanks 1
Link to comment
Share on other sites

On 5/11/2018 at 9:10 AM, Karolek said:

@Rolige thanks for reply!

 

But when I upload your code PrestaShop sent error on last oreder step, sometching is wrong, but I can't find what. Can you help me? I think sometching in code. 

Hello. Activate debug mode to know more details about error. Regards

Link to comment
Share on other sites

  • 1 year later...

In order to add ID number of order in subject of emails sent to customers to inform them that was changed their order status (eg. order modified, shipped, in transit, payment received, need pre-payment, etc) you need to alter the OrderHistory.php class; most reccomended way is to create an override; I have done this: 1. created the "OrderHistory.php"; 2. it's content I put it in "\classes\order\" and you can see it bellow:

/*
modified class in order to add ID of order to subject of emails sent when order status has changed
*/

class OrderHistory extends OrderHistoryCore
{
 
    public function sendEmail($order, $template_vars = false)
    {
        $result = Db::getInstance()->getRow('
			SELECT osl.`template`, c.`lastname`, c.`firstname`, osl.`name` AS osname, c.`email`, os.`module_name`, os.`id_order_state`, os.`pdf_invoice`, os.`pdf_delivery`
			FROM `'._DB_PREFIX_.'order_history` oh
				LEFT JOIN `'._DB_PREFIX_.'orders` o ON oh.`id_order` = o.`id_order`
				LEFT JOIN `'._DB_PREFIX_.'customer` c ON o.`id_customer` = c.`id_customer`
				LEFT JOIN `'._DB_PREFIX_.'order_state` os ON oh.`id_order_state` = os.`id_order_state`
				LEFT JOIN `'._DB_PREFIX_.'order_state_lang` osl ON (os.`id_order_state` = osl.`id_order_state` AND osl.`id_lang` = o.`id_lang`)
			WHERE oh.`id_order_history` = '.(int)$this->id.' AND os.`send_email` = 1');
        if (isset($result['template']) && Validate::isEmail($result['email'])) {
            ShopUrl::cacheMainDomainForShop($order->id_shop);

            $topic = $result['osname'];
            $data = array(
                '{lastname}' => $result['lastname'],
                '{firstname}' => $result['firstname'],
                '{id_order}' => (int)$this->id_order,
                '{order_name}' => $order->getUniqReference()
            );

            if ($result['module_name']) {
                $module = Module::getInstanceByName($result['module_name']);
                if (Validate::isLoadedObject($module) && isset($module->extra_mail_vars) && is_array($module->extra_mail_vars)) {
                    $data = array_merge($data, $module->extra_mail_vars);
                }
            }

            if ($template_vars) {
                $data = array_merge($data, $template_vars);
            }

            $data['{total_paid}'] = Tools::displayPrice((float)$order->total_paid, new Currency((int)$order->id_currency), false);

            if (Validate::isLoadedObject($order)) {
                // Attach invoice and / or delivery-slip if they exists and status is set to attach them
                if (($result['pdf_invoice'] || $result['pdf_delivery'])) {
                    $context = Context::getContext();
                    $invoice = $order->getInvoicesCollection();
                    $file_attachement = array();

                    if ($result['pdf_invoice'] && (int)Configuration::get('PS_INVOICE') && $order->invoice_number) {
                        Hook::exec('actionPDFInvoiceRender', array('order_invoice_list' => $invoice));
                        $pdf = new PDF($invoice, PDF::TEMPLATE_INVOICE, $context->smarty);
                        $file_attachement['invoice']['content'] = $pdf->render(false);
                        $file_attachement['invoice']['name'] = Configuration::get('PS_INVOICE_PREFIX', (int)$order->id_lang, null, $order->id_shop).sprintf('%06d', $order->invoice_number).'.pdf';
                        $file_attachement['invoice']['mime'] = 'application/pdf';
                    }
                    if ($result['pdf_delivery'] && $order->delivery_number) {
                        $pdf = new PDF($invoice, PDF::TEMPLATE_DELIVERY_SLIP, $context->smarty);
                        $file_attachement['delivery']['content'] = $pdf->render(false);
                        $file_attachement['delivery']['name'] = Configuration::get('PS_DELIVERY_PREFIX', Context::getContext()->language->id, null, $order->id_shop).sprintf('%06d', $order->delivery_number).'.pdf';
                        $file_attachement['delivery']['mime'] = 'application/pdf';
                    }
                } else {
                    $file_attachement = null;
                }

                if (!Mail::Send((int)$order->id_lang, $result['template'], $topic.' ID: #'.(int)$this->id_order, $data, $result['email'], $result['firstname'].' '.$result['lastname'],
                    null, null, $file_attachement, null, _PS_MAIL_DIR_, false, (int)$order->id_shop)) {
                    return false;
                }
            }

            ShopUrl::resetMainDomainCache();
        }

        return true;
    }

}

After this, do not forget to delete file "\cache\class_index.php" in order to be re-created and to be taken into account your new overwritten class.

The key is to modify the line:
 if (!Mail::Send((int)$order->id_lang, $result['template'], $topic.' ID: #'.(int)$this->id_order, $data, $result['email'], $result['firstname'].' '.$result['lastname'], ...
Original was:
 if (!Mail::Send((int)$order->id_lang, $result['template'], $topic, $data, $result['email'], $result['firstname'].' '.$result['lastname'], ...

=============
ADDING ID OF ORDER TO ORDER CONFIRMATION EMAIL (the first email which is sent to customer)
Regarding adding ID to subject sent to customer at first status (Order Received) I override the class PaymentModule.php, replacing with:

if (Validate::isEmail($this->context->customer->email)) {
                            Mail::Send(
                                (int)$order->id_lang,
                                'order_conf',
                                Mail::l('Order confirmation', (int)$order->id_lang).' ID: #'.(int)$order->id,
                                $data,
                                $this->context->customer->email,
                                $this->context->customer->firstname.' '.$this->context->customer->lastname,
                                null,
                                null,
                                $file_attachement,
                                null, _PS_MAIL_DIR_, false, (int)$order->id_shop
                            );
                        }

Pay attention at line:
Mail::l('Order confirmation', (int)$order->id_lang).' ID: #'.(int)$order->id,
The original was:
Mail::l('Order confirmation', (int)$order->id_lang),

If you want to add order reference instead of order ID then I think is a little more tricky because sql need to be altered in order to extract also the order reference...

These was useful for me in 2 cases: 1. I put prestashop to send me in BCC all copies of emails sent to customers to my gmail account and of course gmail see all emails with same subject as an conversation and group them together, is not easy to browse in order to find an specific email; 2. same thing it happens in outlook if you set it up to see the emails with same subject as an conversation. So, adding the ID of order to subject of email make any email to be different.

cheers !

  • Like 1
Link to comment
Share on other sites

After few days I have observed that emails sent to customer informing him that his order have "Package in transit" status don't have ID in subject of email. So I dig more and I have seen that this email is sent from AdminOrdersController.php file

I have copied that file in override/controllers/admin and change the class definition as: class AdminOrdersController extends AdminOrdersControllerCore

In that file, at line around 500 changed the line: 

if (@Mail::Send((int)$order->id_lang, 'in_transit', Mail::l('Package in transit', (int)$order->id_lang).' ID: #'.(int)$order->id,

And now I have ID of order in subject email.

Don't forget to delete class_index.php after put that file in override.

I am on PS 1.6.1.18

Link to comment
Share on other sites

  • 2 years later...
On 1/28/2020 at 6:33 PM, kenoffice said:

After few days I have observed that emails sent to customer informing him that his order have "Package in transit" status don't have ID in subject of email. So I dig more and I have seen that this email is sent from AdminOrdersController.php file

I have copied that file in override/controllers/admin and change the class definition as: class AdminOrdersController extends AdminOrdersControllerCore

In that file, at line around 500 changed the line: 

if (@Mail::Send((int)$order->id_lang, 'in_transit', Mail::l('Package in transit', (int)$order->id_lang).' ID: #'.(int)$order->id,

And now I have ID of order in subject email.

Don't forget to delete class_index.php after put that file in override.

I am on PS 1.6.1.18

to add the order id in the "Order Notification Received" email from PayPal ?

Link to comment
Share on other sites

  • 5 months later...

How can i do this in PS 1.7.8 and with reference instead that ID? The file /classes/PaymentModule.php is a little bit different, I have this:

Mail::Send(
    (int) $order->id_lang,
    'order_conf',
    $this->context->getTranslator()->trans(
        'Order confirmation',
        [],
        'Emails.Subject',
        $orderLanguage->locale
    ),
    $data,

 

On 1/23/2020 at 7:40 AM, kenoffice said:

In order to add ID number of order in subject of emails sent to customers to inform them that was changed their order status (eg. order modified, shipped, in transit, payment received, need pre-payment, etc) you need to alter the OrderHistory.php class; most reccomended way is to create an override; I have done this: 1. created the "OrderHistory.php"; 2. it's content I put it in "\classes\order\" and you can see it bellow:

/*
modified class in order to add ID of order to subject of emails sent when order status has changed
*/

class OrderHistory extends OrderHistoryCore
{
 
    public function sendEmail($order, $template_vars = false)
    {
        $result = Db::getInstance()->getRow('
			SELECT osl.`template`, c.`lastname`, c.`firstname`, osl.`name` AS osname, c.`email`, os.`module_name`, os.`id_order_state`, os.`pdf_invoice`, os.`pdf_delivery`
			FROM `'._DB_PREFIX_.'order_history` oh
				LEFT JOIN `'._DB_PREFIX_.'orders` o ON oh.`id_order` = o.`id_order`
				LEFT JOIN `'._DB_PREFIX_.'customer` c ON o.`id_customer` = c.`id_customer`
				LEFT JOIN `'._DB_PREFIX_.'order_state` os ON oh.`id_order_state` = os.`id_order_state`
				LEFT JOIN `'._DB_PREFIX_.'order_state_lang` osl ON (os.`id_order_state` = osl.`id_order_state` AND osl.`id_lang` = o.`id_lang`)
			WHERE oh.`id_order_history` = '.(int)$this->id.' AND os.`send_email` = 1');
        if (isset($result['template']) && Validate::isEmail($result['email'])) {
            ShopUrl::cacheMainDomainForShop($order->id_shop);

            $topic = $result['osname'];
            $data = array(
                '{lastname}' => $result['lastname'],
                '{firstname}' => $result['firstname'],
                '{id_order}' => (int)$this->id_order,
                '{order_name}' => $order->getUniqReference()
            );

            if ($result['module_name']) {
                $module = Module::getInstanceByName($result['module_name']);
                if (Validate::isLoadedObject($module) && isset($module->extra_mail_vars) && is_array($module->extra_mail_vars)) {
                    $data = array_merge($data, $module->extra_mail_vars);
                }
            }

            if ($template_vars) {
                $data = array_merge($data, $template_vars);
            }

            $data['{total_paid}'] = Tools::displayPrice((float)$order->total_paid, new Currency((int)$order->id_currency), false);

            if (Validate::isLoadedObject($order)) {
                // Attach invoice and / or delivery-slip if they exists and status is set to attach them
                if (($result['pdf_invoice'] || $result['pdf_delivery'])) {
                    $context = Context::getContext();
                    $invoice = $order->getInvoicesCollection();
                    $file_attachement = array();

                    if ($result['pdf_invoice'] && (int)Configuration::get('PS_INVOICE') && $order->invoice_number) {
                        Hook::exec('actionPDFInvoiceRender', array('order_invoice_list' => $invoice));
                        $pdf = new PDF($invoice, PDF::TEMPLATE_INVOICE, $context->smarty);
                        $file_attachement['invoice']['content'] = $pdf->render(false);
                        $file_attachement['invoice']['name'] = Configuration::get('PS_INVOICE_PREFIX', (int)$order->id_lang, null, $order->id_shop).sprintf('%06d', $order->invoice_number).'.pdf';
                        $file_attachement['invoice']['mime'] = 'application/pdf';
                    }
                    if ($result['pdf_delivery'] && $order->delivery_number) {
                        $pdf = new PDF($invoice, PDF::TEMPLATE_DELIVERY_SLIP, $context->smarty);
                        $file_attachement['delivery']['content'] = $pdf->render(false);
                        $file_attachement['delivery']['name'] = Configuration::get('PS_DELIVERY_PREFIX', Context::getContext()->language->id, null, $order->id_shop).sprintf('%06d', $order->delivery_number).'.pdf';
                        $file_attachement['delivery']['mime'] = 'application/pdf';
                    }
                } else {
                    $file_attachement = null;
                }

                if (!Mail::Send((int)$order->id_lang, $result['template'], $topic.' ID: #'.(int)$this->id_order, $data, $result['email'], $result['firstname'].' '.$result['lastname'],
                    null, null, $file_attachement, null, _PS_MAIL_DIR_, false, (int)$order->id_shop)) {
                    return false;
                }
            }

            ShopUrl::resetMainDomainCache();
        }

        return true;
    }

}

After this, do not forget to delete file "\cache\class_index.php" in order to be re-created and to be taken into account your new overwritten class.

The key is to modify the line:
 if (!Mail::Send((int)$order->id_lang, $result['template'], $topic.' ID: #'.(int)$this->id_order, $data, $result['email'], $result['firstname'].' '.$result['lastname'], ...
Original was:
 if (!Mail::Send((int)$order->id_lang, $result['template'], $topic, $data, $result['email'], $result['firstname'].' '.$result['lastname'], ...

=============
ADDING ID OF ORDER TO ORDER CONFIRMATION EMAIL (the first email which is sent to customer)
Regarding adding ID to subject sent to customer at first status (Order Received) I override the class PaymentModule.php, replacing with:

if (Validate::isEmail($this->context->customer->email)) {
                            Mail::Send(
                                (int)$order->id_lang,
                                'order_conf',
                                Mail::l('Order confirmation', (int)$order->id_lang).' ID: #'.(int)$order->id,
                                $data,
                                $this->context->customer->email,
                                $this->context->customer->firstname.' '.$this->context->customer->lastname,
                                null,
                                null,
                                $file_attachement,
                                null, _PS_MAIL_DIR_, false, (int)$order->id_shop
                            );
                        }

Pay attention at line:
Mail::l('Order confirmation', (int)$order->id_lang).' ID: #'.(int)$order->id,
The original was:
Mail::l('Order confirmation', (int)$order->id_lang),

If you want to add order reference instead of order ID then I think is a little more tricky because sql need to be altered in order to extract also the order reference...

These was useful for me in 2 cases: 1. I put prestashop to send me in BCC all copies of emails sent to customers to my gmail account and of course gmail see all emails with same subject as an conversation and group them together, is not easy to browse in order to find an specific email; 2. same thing it happens in outlook if you set it up to see the emails with same subject as an conversation. So, adding the ID of order to subject of email make any email to be different.

cheers !

 

Link to comment
Share on other sites

To change the email subject for order confirmation emails in PrestaShop, you will need to edit the email template for that specific email. Here are the steps to do this:

Log in to the back office of your PrestaShop store.

In the left menu, go to "Advanced Parameters" and then click on "Email" in the submenu.

On the next page, you will see a list of all the email templates that are used by your store. Find the email template for order confirmation emails, and click on the "Edit" button next to it.

In the email template editor, you can change the subject line for the email by modifying the text in the "Subject" field. You can add the order number to the subject line by using the {order_number} variable. For example, you can set the subject line to be "Order confirmation for order #{order_number}".

After making your changes, click on the "Save" button to save the email template. The new subject line will be used for all future order confirmation emails.

Note: The exact steps for editing email templates may vary depending on the version of PrestaShop you are using. You may need to consult the PrestaShop documentation or seek assistance from a PrestaShop expert if you are unsure about how to edit email templates.

Link to comment
Share on other sites

Thank you for your answer but i find something completely different if I follow that path: Advanced Parameters -> Email. What i find is emails sent and email configuration.

I can find the templates in international->translation but with no subject line. In the same place (international->translation) I can translate the subject but I can't use variable 

Link to comment
Share on other sites

I think I found the solution for the first part, add order reference to of emails sent to customers to inform them that was changed their order status. I created an override for /classes/order/OrderHistory.php and I put it in /override/classes/order/OrderHistory.php. This is the code:

<?php
/*
modified class in order to add Order Reference of order to subject of emails sent when order status has changed
*/

class OrderHistory extends OrderHistoryCore
{
  public function sendEmail($order, $template_vars = false)
    {
        $result = Db::getInstance()->getRow('
            SELECT osl.`template`, c.`lastname`, c.`firstname`, osl.`name` AS osname, c.`email`, os.`module_name`, os.`id_order_state`, os.`pdf_invoice`, os.`pdf_delivery`
            FROM `' . _DB_PREFIX_ . 'order_history` oh
                LEFT JOIN `' . _DB_PREFIX_ . 'orders` o ON oh.`id_order` = o.`id_order`
                LEFT JOIN `' . _DB_PREFIX_ . 'customer` c ON o.`id_customer` = c.`id_customer`
                LEFT JOIN `' . _DB_PREFIX_ . 'order_state` os ON oh.`id_order_state` = os.`id_order_state`
                LEFT JOIN `' . _DB_PREFIX_ . 'order_state_lang` osl ON (os.`id_order_state` = osl.`id_order_state` AND osl.`id_lang` = o.`id_lang`)
            WHERE oh.`id_order_history` = ' . (int) $this->id . ' AND os.`send_email` = 1');
        if (isset($result['template']) && Validate::isEmail($result['email'])) {
            ShopUrl::cacheMainDomainForShop($order->id_shop);

            $topic = $result['osname'];
            $carrierUrl = '';
            if (Validate::isLoadedObject($carrier = new Carrier((int) $order->id_carrier, $order->id_lang))) {
                $carrierUrl = $carrier->url;
            }
            $data = [
                '{lastname}' => $result['lastname'],
                '{firstname}' => $result['firstname'],
                '{id_order}' => (int) $this->id_order,
                '{order_name}' => $order->getUniqReference(),
                '{followup}' => str_replace('@', $order->getWsShippingNumber(), $carrierUrl),
                '{shipping_number}' => $order->getWsShippingNumber(),
            ];

            if ($result['module_name']) {
                $module = Module::getInstanceByName($result['module_name']);
                if (Validate::isLoadedObject($module) && isset($module->extra_mail_vars) && is_array($module->extra_mail_vars)) {
                    $data = array_merge($data, $module->extra_mail_vars);
                }
            }

            if (is_array($template_vars)) {
                $data = array_merge($data, $template_vars);
            }

            $context = Context::getContext();
            $data['{total_paid}'] = Tools::getContextLocale($context)->formatPrice((float) $order->total_paid, Currency::getIsoCodeById((int) $order->id_currency));

            if (Validate::isLoadedObject($order)) {
                // Attach invoice and / or delivery-slip if they exists and status is set to attach them
                if (($result['pdf_invoice'] || $result['pdf_delivery'])) {
                    $currentLanguage = $context->language;
                    $orderLanguage = new Language((int) $order->id_lang);
                    $context->language = $orderLanguage;
                    $context->getTranslator()->setLocale($orderLanguage->locale);
                    $invoice = $order->getInvoicesCollection();
                    $file_attachement = [];

                    if ($result['pdf_invoice'] && (int) Configuration::get('PS_INVOICE') && $order->invoice_number) {
                        Hook::exec('actionPDFInvoiceRender', ['order_invoice_list' => $invoice]);
                        $pdf = new PDF($invoice, PDF::TEMPLATE_INVOICE, $context->smarty);
                        $file_attachement['invoice']['content'] = $pdf->render(false);
                        $file_attachement['invoice']['name'] = Configuration::get('PS_INVOICE_PREFIX', (int) $order->id_lang, null, $order->id_shop) . sprintf('%06d', $order->invoice_number) . '.pdf';
                        $file_attachement['invoice']['mime'] = 'application/pdf';
                    }
                    if ($result['pdf_delivery'] && $order->delivery_number) {
                        $pdf = new PDF($invoice, PDF::TEMPLATE_DELIVERY_SLIP, $context->smarty);
                        $file_attachement['delivery']['content'] = $pdf->render(false);
                        $file_attachement['delivery']['name'] = Configuration::get('PS_DELIVERY_PREFIX', (int) $order->id_lang, null, $order->id_shop) . sprintf('%06d', $order->delivery_number) . '.pdf';
                        $file_attachement['delivery']['mime'] = 'application/pdf';
                    }

                    $context->language = $currentLanguage;
                    $context->getTranslator()->setLocale($currentLanguage->locale);
                } else {
                    $file_attachement = null;
                }

                if (!Mail::Send(
                    (int) $order->id_lang,
                    $result['template'],
                    $topic.' # '.$order->getUniqReference(),
                    $data,
                    $result['email'],
                    $result['firstname'] . ' ' . $result['lastname'],
                    null,
                    null,
                    $file_attachement,
                    null,
                    _PS_MAIL_DIR_,
                    false,
                    (int) $order->id_shop
                )) {
                    return false;
                }
            }

            ShopUrl::resetMainDomainCache();
        }

        return true;
    }
}

After this, I deleted the file "/var/cahace/prod/class_index.php" in order to be re-created and to be taken into account your new overwritten class.

I found a solution for the confirmation order too. I created a copy of  /classes/PaymentModule.php and changed class header into:  

abstract class PaymentModule extends PaymentModuleCore

I replaced:

Mail::Send(
    (int) $order->id_lang,
    'order_conf',
    $this->context->getTranslator()->trans(
        'Order confirmation',
        [],
        'Emails.Subject',
        $orderLanguage->locale
    ),
    $data,
    $this->context->customer->email,
    $this->context->customer->firstname . ' ' . $this->context->customer->lastname,
    null,
    null,
    $file_attachement,
    null,
    _PS_MAIL_DIR_,
    false,
    (int) $order->id_shop
);

with:

Mail::Send(
    (int) $order->id_lang,
    'order_conf',
    $this->context->getTranslator()->trans(
        ''Order confirmation # %s',
            [
            $order->reference,
            ],
        'Emails.Subject',
        $orderLanguage->locale
    ),
    $data,
    $this->context->customer->email,
    $this->context->customer->firstname . ' ' . $this->context->customer->lastname,
    null,
    null,
    $file_attachement,
    null,
    _PS_MAIL_DIR_,
    false,
    (int) $order->id_shop
);

Then I put it in /override/classes/PaymentModule.php  I deleted the file "/var/cahace/prod/class_index.php" in order to be re-created and to be taken into account your new overwritten class.

Edited by Stefo27 (see edit history)
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...