Jump to content

Resolved - Can I add code to email templates using a hook?


gusman126

Recommended Posts

Can I add code to email templates using a hook?
Hello I want to add a follow-up of opening emails.
https://developers.google.com/analytics/devguides/collection/protocol/v1/email
I am looking for a hook that will add a small code to the existing email templates in the Prestashop.
I don't want it to be manual and for the seller to add this code by hand,
I need to make the change with a hook, or detect when an email is generated or sent to add this code to the template

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

actionEmailAddAfterContent

Add extra content after mail content This hook is called just after fetching mail template

Located in: /classes/Mail.php

actionEmailAddBeforeContent

Add extra content before mail content This hook is called just before fetching mail template

Located in: /classes/Mail.php

actionEmailSendBefore

Before sending an email This hook is used to filter the content or the metadata of an email before sending it or even prevent its sending

Located in: /classes/Mail.php

Link to comment
Share on other sites

Sample:

public function hookActionEmailAddAfterContent($param)
{
    if (!isset($param['template']) || !isset($param['template_html']) || !isset($param['template_txt'])) {
        return;
    }

    $tpl_name = (string) $param['template'];
    $id_lang = (int) $param['id_lang'];
    
    $param['template_html'] .= '<a href="https://my-url.com">'.$this->l('my url text').'</a>';
    $param['template_txt'] .= '<a href="https://my-url.com">'.$this->l('my url text').'</a>';
    
    $param['template_vars']['{shop_url}'] .= ' - '.'<a href="https://my-url.com">'.$this->l('my url text').'</a>';
}

or

public function hookActionEmailSendBefore($param)
{
    if (!isset($param['template']) || !isset($param['template_html']) || !isset($param['template_txt'])) {
        return;
    }

    $tpl_name = (string) $param['template'];
    $id_lang = (int) $param['id_lang'];
    
    $param['template_html'] .= '<a href="https://my-url.com">'.$this->l('my url text').'</a>';
    $param['template_txt'] .= '<a href="https://my-url.com">'.$this->l('my url text').'</a>';
    
    $param['template_vars']['{shop_url}'] .= ' - '.'<a href="https://my-url.com">'.$this->l('my url text').'</a>';
}

 

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

You can also add conditions to the code for which templates you want to use the hook.
E.g.

public function hookActionEmailSendBefore($param)
{
    ...
    /* list of email templates for which I want a hook */
    $tpl_names = array('order_conf', 'bankwire', 'in_transit');

    $tpl_name = (string) $param['template'];
    $id_lang = (int) $param['id_lang'];
    
    /* when the email template is in the list */
    if (in_array($tpl_name, $tpl_names, true)) {
        ....
    }
}

 

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

Many thanks

it has worked perfectly, I have added the code that google recommends "email tracking" in my analytics / datalayer module and everything works perfectly
Thank you

public function hookActionEmailAddAfterContent($param)
	{
		if (!isset($param['template']) || !isset($param['template_html']) || !isset($param['template_txt'])) {
			return;
		}

		$tpl_name = (string) $param['template'];
		$id_lang = (int) $param['id_lang'];
		
		$gacode = Configuration::get('DATALAYERTAGMANAGER_IDGANALYTICS');
		$add = '<img src="https://www.google-analytics.com/collect?v=1&tid='.$gacode.'&t=event&ec=email&ea=open"/>';
		
		$param['template_html'] .= $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...