Wednesday, October 13, 2010

PrestaShop Tips - How to add new data fields in email template

At PrestaShop, emails are sent to customer or store owner some kind of events occurs, such as new order confirmation, new account registration.

Some of the email are sent by core PrestaShop, some of them are sent out via additional installed modules, such as mail alerts module.

The mail contents are generated by using email template. The data field which are represented by keywords in the template like {delivery_phone}, {invoice_firstname} are replaced by data from database when email is sent out.

Some time, you want add some more data field to customize default alert email templates. But before you can use keywords at email templates, you must define those keyword first and load data first before you can use it in the mail templates.

Here I try to give some instructions on how to add a new data fields to your email template. For example, you want add phone_mobile to your email.

The keywords are defined in following file

-- Prewstashop core
YourSiteRoot/classes/PaymentModule.php
-- ailalerts module
YourSiteRoot/modules/mailalerts/mailalerts.php

And here is sample code in above file that defines default keywords.

$templateVars = array(
'{firstname}' => $customer->firstname,
'{lastname}' => $customer->lastname,
'{email}' => $customer->email,
'{delivery_company}' => $delivery->company,
'{delivery_firstname}' => $delivery->firstname,
'{delivery_lastname}' => $delivery->lastname,
'{delivery_address1}' => $delivery->address1,
'{delivery_address2}' => $delivery->address2,
'{delivery_city}' => $delivery->city,
'{delivery_postal_code}' => $delivery->postcode,
'{delivery_country}' => $delivery->country,
'{delivery_state}' => $delivery->id_state ? $delivery_state->name : '',
'{delivery_phone}' => $delivery->phone,
'{delivery_other}' => $delivery->other,
'{invoice_company}' => $invoice->company,
'{invoice_firstname}' => $invoice->firstname,
'{invoice_lastname}' => $invoice->lastname,
'{invoice_address2}' => $invoice->address2,
'{invoice_address1}' => $invoice->address1,
'{invoice_city}' => $invoice->city,
'{invoice_postal_code}' => $invoice->postcode,
'{invoice_country}' => $invoice->country,
'{invoice_state}' => $invoice->id_state ? $invoice_state->name : '',
'{invoice_phone}' => $invoice->phone,
'{invoice_other}' => $invoice->other,
'{order_name}' => sprintf("%06d", $order->id),
'{shop_name}' => Configuration::get('PS_SHOP_NAME'),
'{date}' => $order_date_text,
'{carrier}' => (($carrier->name == '0') ? Configuration::get('PS_SHOP_NAME') : $carrier->name),
'{payment}' => $order->payment,
'{items}' => $itemsTable,
'{total_paid}' => Tools::displayPrice($order->total_paid, $currency),
'{total_products}' => Tools::displayPrice($order->getTotalProductsWithTaxes(), $currency),
'{total_discounts}' => Tools::displayPrice($order->total_discounts, $currency),
'{total_shipping}' => Tools::displayPrice($order->total_shipping, $currency),
'{total_wrapping}' => Tools::displayPrice($order->total_wrapping, $currency),
'{currency}' => $currency->sign,
'{message}' => $message
);


If you want some additional data fields, you have to check if there are existing keywords defined for your data. If not, then you have to find out if the data is defined as property of one of objects that are used in mailalerts.php, like Order, Delivery, Invoice and so on.

Then you can add keyword and property mapping, for example you want add mobile phone number to your email. Then you can implement as following.
1. Define keyword property mapping

'{invoice_phone_mobile}' => $invoice->phone_mobile,
....
'{delivery_phone_mobile}' => $delivery->phone_mobile,


2. use the new keywords in your email template.

The alert mail templates are located at following folder

--Core emails
/mails/en/

--Mail Alerts emails
/modules/mailalerts/mails/en/

Please replace /en/ with proper language code that you are using.