Jump to content

[NOT SOLVED] hookActionEmailSendBefore


ps8modules

Recommended Posts

Prestashop 1.7.7.0

Hi to all.
I need to programmatically change the logo in email templates.

How to work with parameters in the hook?

My custom image my-custom-logo.jpg not changed in mail templates.

This params not work:

$params['templateVars']['{shop_logo}'] = $logo;

Full code:

public function hookActionEmailSendBefore($params)
{
	$id_lang = $params['idLang'];
    $templateVars = $params['templateVars'];
	$id_order = $templateVars['{id_order}'];
	$id_shop = $params['idShop'];
		
	$get_custom_logo = _PS_IMG_DIR_.'my-custom-logo.jpg';
		
	if (file_exists($get_custom_logo)){
			$logo = $get_custom_logo;
	} else {
			if (false !== Configuration::get('PS_LOGO_MAIL') && file_exists(_PS_IMG_DIR_ . Configuration::get('PS_LOGO_MAIL', null, null, $id_shop))) {
                $logo = _PS_IMG_DIR_ . Configuration::get('PS_LOGO_MAIL', null, null, $id_shop);
        } else {
            if (file_exists(_PS_IMG_DIR_ . Configuration::get('PS_LOGO', null, null, $id_shop))) {
                    $logo = _PS_IMG_DIR_ . Configuration::get('PS_LOGO', null, null, $id_shop);
            } else {
                $templateVars['{shop_logo}'] = '';
            }
        }
	}
		
		$params['templateVars']['{shop_logo}'] = $logo;

		return;
}

Thank you

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

Yes because image var is set after that hook is called registered by you so reflection of that in code

 

if (false !== Configuration::get('PS_LOGO_MAIL') &&
                file_exists(_PS_IMG_DIR_ . Configuration::get('PS_LOGO_MAIL', null, null, $idShop))
            ) {
                $logo = _PS_IMG_DIR_ . Configuration::get('PS_LOGO_MAIL', null, null, $idShop);
            } else {
                if (file_exists(_PS_IMG_DIR_ . Configuration::get('PS_LOGO', null, null, $idShop))) {
                    $logo = _PS_IMG_DIR_ . Configuration::get('PS_LOGO', null, null, $idShop);
                } else {
                    $templateVars['{shop_logo}'] = '';
                }
            }
            ShopUrl::cacheMainDomainForShop((int) $idShop);
            /* don't attach the logo as */
            if (isset($logo)) {
                $templateVars['{shop_logo}'] = $message->embed(\Swift_Image::fromPath($logo));
            }

You can try by adding actionGetExtraMailTemplateVars hook and add at same index so while merging it update that var and reflect your new image 
 

// Get extra template_vars
            $extraTemplateVars = [];
            Hook::exec(
                'actionGetExtraMailTemplateVars',
                [
                    'template' => $template,
                    'template_vars' => $templateVars,
                    'extra_template_vars' => &$extraTemplateVars,
                    'id_lang' => (int) $idLang,
                ],
                null,
                true
            );
            $templateVars = array_merge($templateVars, $extraTemplateVars);

 

Edited by arunvishwakarama (see edit history)
  • Thanks 1
Link to comment
Share on other sites

  • ps8modules changed the title to [NOT SOLVED] hookActionEmailSendBefore

Sorry @arunvishwakarama not work:

public $id_shop = '';

public function hookActionEmailSendBefore($params)
{
	$this->id_shop = $params['idShop'];
}	
	
public function hookActionGetExtraMailTemplateVars($params)
{
	$idLang = $params['id_lang'];
	$idShop = $this->id_shop;
	$templateVars = $params['template_vars'];

	$get_custom_logo = _PS_IMG_DIR_.'logo-'.$idLang.'-'.$idShop.'.jpg';
			
	if (file_exists($get_custom_logo)){
		$logo = $get_custom_logo;
		$templateVars['{shop_logo}'] = $logo;
	}
}

 

Link to comment
Share on other sites

I made a statement after the end of the code and everything seems to be fine, but there is still a different logo in the email.

public function hookActionGetExtraMailTemplateVars($params)
{
	$idLang = $params['id_lang'];
	$idShop = $this->id_shop;

	$get_custom_logo = _PS_IMG_DIR_.'logo-'.$idLang.'-'.$idShop.'.jpg';
			
	if (file_exists($get_custom_logo)){
		$logo = $get_custom_logo;
		$params['template_vars']['{shop_logo}'] = $logo;
	}
	
	foreach ($params['template_vars'] as $key=>$value){
		$xx .= $key.' - '.$value.'<br />';
	}
	
	print($xx);
}

result:

{lastname} - John
{firstname} - Doe
{id_order} - 1
{order_name} - GKXSMCCXZ
{followup} - 
{shipping_number} - 
{bankwire_owner} - 
{bankwire_details} - 
{bankwire_address} - 
{total_paid} - $1.00
{shop_logo} - /home/domains/mydomain.com/web/public/img/logo-3-1.jpg
{shop_name} - Prestashop TEST
{shop_url} - https://mydomain.com/en/
{my_account_url} - https://mydomain.com/en/my-account
{guest_tracking_url} - https://mydomain.com/en/guest-tracking
{history_url} - https://mydomain.com/en/order-history
{color} - #db3484

 

Link to comment
Share on other sites

2 hours ago, WebSoft said:

public function hookActionGetExtraMailTemplateVars($params) { $idLang = $params['id_lang']; $idShop = $this->id_shop; $templateVars = $params['template_vars']; $get_custom_logo = _PS_IMG_DIR_.'logo-'.$idLang.'-'.$idShop.'.jpg'; if (file_exists($get_custom_logo)){ $logo = $get_custom_logo; $templateVars['{shop_logo}'] = $logo; } }

It will not displayed in print  because after that hook template_vars and extra_template_vars are merged so it reflect in mail, Please update this with below and try as

public function hookActionGetExtraMailTemplateVars($params)
{
	$idLang = $params['id_lang'];
	$idShop = $this->id_shop;
	$get_custom_logo = _PS_IMG_DIR_.'logo-'.$idLang.'-'.$idShop.'.jpg';
	if (file_exists($get_custom_logo)){
		$params['extra_template_vars']['{shop_logo}'] = $get_custom_logo;
	}
}

 

  • Thanks 1
Link to comment
Share on other sites

20 minutes ago, WebSoft said:

I've found that the logo is an attachment, but it's not the correct logo to upload.

obrazek.png.418ce3e9861fbf366b6589d47715b8ad.png

can you please update it again 
 

public function hookActionGetExtraMailTemplateVars($params)
{
	$idLang = $params['id_lang'];
	$idShop = $this->id_shop;
	$get_custom_logo = _PS_IMG_DIR_.'logo-'.$idLang.'-'.$idShop.'.jpg';
	if (file_exists($get_custom_logo)){
		$message = \Swift_Message::newInstance();
		$params['extra_template_vars']['{shop_logo}'] =  $message->embed(\Swift_Image::fromPath($get_custom_logo));
	}
}

 

  • Like 1
Link to comment
Share on other sites

  • 7 months later...

Is there any solution on this?
I got the attachment solution as well, this depends on the email client.

For example, if Outlook.com is used, it shown as attachment. But if MailSpring is used, the logo directly shown.
I think this is related to some security issue.

I cannot get this done, and I think only solution is to override Mail.php or suggest PrestaShop to add a new hook before line 505 of Mail.php

if (isset($logo)) {
	$templateVars['{shop_logo}'] = $message->embed(\Swift_Image::fromPath($logo));
}

Something like:

Hook::exec('displayEmailCustomLogo',['logo' => $logo]);

So that developers can pass customized $logo before $message->embed

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

Sharing my solution here, my target is to show different logos according to customer group

I created a module which to override Mail.php before line 505
Where I am executing a custom hook

$custom_logo = Hook::exec('displayEmailCustomLogo',['logo' => $logo, 'template_vars' => $templateVars]);
if (!empty($custom_logo)) $logo = $custom_logo;
if (isset($logo)) {
	$templateVars['{shop_logo}'] = $message->embed(\Swift_Image::fromPath($logo));
}

In my module, I have the below

public function install(){
    return parent::install() &&
        $this->registerHook('displayEmailCustomLogo');
}
public function hookDisplayEmailCustomLogo($params) {
    if (isset($params['cart'])) {
        // FO
        $cgid = Customer::getGroupsStatic($params['cart']->id_customer);
    } elseif (isset($params['template_vars']['{id_order}'])) {
        // BO
        $oid = $params['template_vars']['{id_order}'];        
        $oob = new Order($oid);
        $cgid = $oob->getCustomer()->getGroups();
    } else {
        return;
    }
    if (isset($cgid) && in_array(4, $cgid)) return _PS_IMG_DIR_ . 'another_logo.png';
}

 

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