Jump to content

Variables in the email templates


Recommended Posts

Hello.

 

How to create a variable what will be available in the email templates?

 

For prestashop 1.5.2 following scheme worked for me:

In the Mail.php file has been created variable $template_vars['{my_var}'] = "value";

and this variable "{my_var}" I used in html files of email templates.

 

But now, in prestashop 1.5.4.1 this scheme doesn't works. Variable is not available in html files.

 

Any suggestions?

Link to comment
Share on other sites

ideally you would locate the controller/class/module responsible for sending the mail, and revise it to include the variable you wish.

 

however even in ps v1.5.4.1, your approach should still work. Are you sure you applied the override properly?

Link to comment
Share on other sites

Thank you for your reply.

 

Yes, my file Mail.php located in the "override/classes/" folder, but the value of variable is empty. I just checked it once again.

Could you please give me a little more description about "responsible for sending the mail"?

 

Thank you in advance

Link to comment
Share on other sites

Why don't you provide exactly what you are trying to accomplish, and exactly what you changed in the Mail class override.

 

In PS v1.5.4.1, there are the following lines. You should just be able to add 1 more line with your variable.

       	 $template_vars['{shop_name}'] = Tools::safeOutput(Configuration::get('PS_SHOP_NAME', null, null, $id_shop));
           $template_vars['{shop_url}'] = Context::getContext()->link->getPageLink('index', true, Context::getContext()->language->id);
           $template_vars['{my_account_url}'] = Context::getContext()->link->getPageLink('my-account', true, Context::getContext()->language->id);
           $template_vars['{guest_tracking_url}'] = Context::getContext()->link->getPageLink('guest-tracking', true, Context::getContext()->language->id);
           $template_vars['{history_url}'] = Context::getContext()->link->getPageLink('history', true, Context::getContext()->language->id);
           $template_vars['{color}'] = Tools::safeOutput(Configuration::get('PS_MAIL_COLOR', null, null, $id_shop));

Link to comment
Share on other sites

nope, its likely a cache issue. Prestashop stores information about the classes and controllers in a file /cache/class_index.php

 

Open that file and search for the Mail class. Without an override it would look like this...

 'Mail' => '',
 'MailCore' => 'classes/Mail.php',

 

With the override it should look like this.

 'Mail' => 'override/classes/Mail.php',
 'MailCore' => 'classes/Mail.php',

Link to comment
Share on other sites

Yes, you are absolutely right. I'm working with prestashop about three years and I didn't even think about that.

Thank you very much, I really appreciate your help.

 

One more question: Is there is a way to clear that cache in back office? it's necessary for customers.

Edited by Marek Mnishek (see edit history)
Link to comment
Share on other sites

  • 2 months later...
  • 1 month later...

Hi,

How would I go about getting the postcode and tracking number from the order into the email as a var ?

 

Im assuming by this mail.php in the overrides, but I dont have a clue how get the info from the order.

 

I need this as our courier uses the postcode and tracking number to track parcels.

So I need to enter this into the "In transit" email.

 

Then in the "transit.html" I would need to enter something like this:

 

www trackingcompany.com?postcode=$postcodevar&trackid=$trackingvar

 

Either that, or somehow pass the postcode variable to the tracking url: box on the carriers setup ?

ie:

www trackingcompany.com?postcode=$postcodevar&trackid=@

 

 

Thanks for your help

 

Ray

Link to comment
Share on other sites

  • 1 month later...

I would like to add {id_customer} to one of the email templates (Order Confirmation email). I guess it should be changed in this file first and the new variable should be defined: classes/order/OrderHistory.php

I have added code in bold. It didn't work though. Any suggestions?

SELECT osl.`template`, c.`lastname`, c.`firstname`, osl.`name` AS osname, c.`email`, os.`module_name`, os.`id_order_state`c.`id_customer`

...

$data = array(

'{id_customer}' => $result['id_customer'],

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

  • Like 1
Link to comment
Share on other sites

It would be helpful to understand what version of Prestashop you are using ...

 

Assuming it is PS v1.5, there is no need to change OrderHistory class.  Just edit/override the PaymentModule class and add the id_customer as

 

$data = array(
'{id_customer}' => $this->context->customer->id,

'{firstname}' => $this->context->customer->firstname,
'{lastname}' => $this->context->customer->lastname,
'{email}' => $this->context->customer->email,
...

}

  • Like 2
Link to comment
Share on other sites

  • 2 months later...
  • 1 year later...

I've been reading this thread and I think I know which files to change (PS 1.5.3), but I don't know what code to write.

 

I'd like to be able to add a variable for the customer's email address, and be able to use it in several email templates in the main "mails" folder (order_canceled, cheque, shipped, etc.). If I were to edit classes/Mails.php, what would be the code for adding a new variable for customer email address?

$template_vars['{shop_logo}'] = Tools::getShopDomain(true, true).__PS_BASE_URI__.'img/'.Configuration::get('PS_LOGO_MAIL', null, null, $id_shop);
            $template_vars['{shop_name}'] = Tools::safeOutput(Configuration::get('PS_SHOP_NAME'));
            $template_vars['{shop_url}'] = Tools::getShopDomain(true, true).__PS_BASE_URI__.'index.php';
            $template_vars['{my_account_url}'] = Context::getContext()->link->getPageLink('my-account', true, Context::getContext()->language->id);
            $template_vars['{guest_tracking_url}'] = Context::getContext()->link->getPageLink('guest-tracking', true, Context::getContext()->language->id);
            $template_vars['{history_url}'] = Context::getContext()->link->getPageLink('history', true, Context::getContext()->language->id);
            $template_vars['{color}'] = Tools::safeOutput(Configuration::get('PS_MAIL_COLOR'));

$template_vars['{email}'] = ???

 

Or do I need to edit a different file? Thank you to anyone who can help!

 

Elaine

 

Link to comment
Share on other sites

  • 2 weeks later...

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...