Jump to content

Adding to MailCore class, $template_vars


Recommended Posts

Hello. I'm no programmer, and don't have extensive knowledge about how PrestaShop works at the core, so I just need to ask how this should be done properly.

 

I want to add more variables to the e-mail templates so they can be used in all of them, not just one specific email. These are defined in the class MailCore in /prestashop/classes/Mail.php, the e-mail templates are at /prestashop/mails/xx/template.html.

 

Here's the defining of the variables:

 


$template_vars['{shop_name}'] = Tools::safeOutput(Configuration::get('PS_SHOP_NAME', null, null, $id_shop));
$template_vars['{shop_url}'] = Tools::getShopDomain(true, true).__PS_BASE_URI__.'index.php';

/* ADDING NEW VALUE */
$template_vars['{custom_var}'] = 'custom_value';


 

But can I add to the $template_vars somehow through a module, or in some other fasion, or do I have to override the whole shebang? I figure it will break if using a different version of PS.

Link to comment
Share on other sites

  • 2 months later...

Hi !

Yes you are totally right if you made the change directly in the /classes/mail.php, on the next prestashop version update you will create a conflict or at least loose every changes you made.

 

The proper way to do it, is to override the code. ... But how do you do that ? Well the prestashop doc' talk a little about it (But not enough ! è_é). Don't worry i'll try to explain.

 

Simply began by copying the file /classes/Mail.php to the folder /override/classes/Mail.php.

That's it ! You just override the mail.php ! o/

No wait, it's still do the same thing as before ?

 

Ok now we won't touch /classes/Mail.php anymore (It's a core file, some psycho dev' have kill for less than that), and every following reference to mail.php will mean /override/classes/Mail.php.

 

To be a good child file, you need to extends it to his parent.

In mail.php replace the following line

class MailCore
{

by

class Mail extends MailCore
{

 

And now the child file mail.php can override his parent class, erase every function in the file that you won't override and keep only those you are overriding(be careful with the " { " and " } " ).

For example, you want to add a new templates vars and it's in the "public static function Send".

Erase every function expect this one and write your modification (dreadfully for you it's the longest one :P).

 

public static function Send($id_lang, $template, $subject, $template_vars, $to,
	$to_name = null, $from = null, $from_name = null, $file_attachment = null, $mode_smtp = null, $template_path = _PS_MAIL_DIR_, $die = false, $id_shop = null)
{
//** Skipping code **//
//** But you still have to copy it **//

$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);
/* ADDING NEW VALUE */
$template_vars['{custom_var}'] = 'custom_value';

//** Skipping code **//
//** But you still have to copy it **//

}

 

In the example I'm just skipping part of the code that not interesting, but you still need it to copy it, except you can want to override by erasing them :3.

 

Hope I'm helpful.

Edited by Juda (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...