ps8modules Posted March 18, 2021 Share Posted March 18, 2021 (edited) 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 March 18, 2021 by WebSoft SOLVED (see edit history) Link to comment Share on other sites More sharing options...
arunvishwakarama Posted March 18, 2021 Share Posted March 18, 2021 (edited) 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 March 18, 2021 by arunvishwakarama (see edit history) 1 Link to comment Share on other sites More sharing options...
ps8modules Posted March 18, 2021 Author Share Posted March 18, 2021 (edited) @arunvishwakarama I understand. I use two hooks. hookActionEmailSendBefore = to get the idShop parameter hookActionGetExtraMailTemplateVars = to get the id_lang parameter and insert a new logo. Thank you for your help Edited March 18, 2021 by WebSoft (see edit history) Link to comment Share on other sites More sharing options...
ps8modules Posted March 18, 2021 Author Share Posted March 18, 2021 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 More sharing options...
ps8modules Posted March 18, 2021 Author Share Posted March 18, 2021 I will add information that I use hooks in my own module, Mail.php will not be modified Link to comment Share on other sites More sharing options...
ps8modules Posted March 18, 2021 Author Share Posted March 18, 2021 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 More sharing options...
arunvishwakarama Posted March 18, 2021 Share Posted March 18, 2021 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; } } 1 Link to comment Share on other sites More sharing options...
ps8modules Posted March 18, 2021 Author Share Posted March 18, 2021 Thank you. result without logo. For the test I put a dump in Mail.php and the result is: Link to comment Share on other sites More sharing options...
ps8modules Posted March 18, 2021 Author Share Posted March 18, 2021 I've found that the logo is an attachment, but it's not the correct logo to upload. Link to comment Share on other sites More sharing options...
arunvishwakarama Posted March 18, 2021 Share Posted March 18, 2021 20 minutes ago, WebSoft said: I've found that the logo is an attachment, but it's not the correct logo to upload. 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)); } } 1 Link to comment Share on other sites More sharing options...
ps8modules Posted March 18, 2021 Author Share Posted March 18, 2021 (edited) Thank you. update: $message = new Swift_Message(); $params['extra_template_vars']['{shop_logo}'] = $message->embed(\Swift_Image::fromPath($get_custom_logo)); result: Edited March 18, 2021 by WebSoft (see edit history) Link to comment Share on other sites More sharing options...
ps8modules Posted March 18, 2021 Author Share Posted March 18, 2021 In Prestashop 1.7.7 removed newInstance(). 1 Link to comment Share on other sites More sharing options...
ps8modules Posted March 18, 2021 Author Share Posted March 18, 2021 Thank you. Unfortunately did not help. I'm running out of patience and I'd rather override Mail.php Link to comment Share on other sites More sharing options...
arunvishwakarama Posted March 18, 2021 Share Posted March 18, 2021 Just now, WebSoft said: Thank you. Unfortunately did not help. I'm running out of patience and I'd rather override Mail.php ok for now you can continue , i will generate the case and send you solution Thanks 1 Link to comment Share on other sites More sharing options...
ps8modules Posted March 18, 2021 Author Share Posted March 18, 2021 I will be very happy for a solution. I appreciate your help. Thank you Link to comment Share on other sites More sharing options...
ps8modules Posted March 19, 2021 Author Share Posted March 19, 2021 (edited) Hi, any news for me? I have the same problem with HTMLTemplate.php for changing the logo in invoices. Edited March 19, 2021 by WebSoft (see edit history) Link to comment Share on other sites More sharing options...
arunvishwakarama Posted March 19, 2021 Share Posted March 19, 2021 3 hours ago, WebSoft said: Hi, any news for me? I have the same problem with HTMLTemplate.php for changing the logo in invoices. I am looking to it i am able to modify but not able to send email and test on server as not having any live server. have you tried by sending it from server ? can you check below package custominvoicelogo.zip 1 Link to comment Share on other sites More sharing options...
ps8modules Posted March 19, 2021 Author Share Posted March 19, 2021 Thank you. I'll test in a few minutes. Link to comment Share on other sites More sharing options...
ps8modules Posted March 19, 2021 Author Share Posted March 19, 2021 It doesn't work. It would really be good to take a test. It constantly returns the default logo to me, without errors. I do not understand. If I enter the logo name in the configuration table, everything is fine. But the hook doesn't work. Link to comment Share on other sites More sharing options...
arunvishwakarama Posted March 19, 2021 Share Posted March 19, 2021 you checked in live environment ? by sending email ? 1 Link to comment Share on other sites More sharing options...
ps8modules Posted March 19, 2021 Author Share Posted March 19, 2021 (edited) I only test in a live environment. I use SMTP in Prestashop. When I enter a change for first or last name in the hook, the change is OK. Edited March 19, 2021 by WebSoft (see edit history) Link to comment Share on other sites More sharing options...
oliver139 Posted November 18, 2021 Share Posted November 18, 2021 (edited) 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 November 18, 2021 by oliver139 (see edit history) Link to comment Share on other sites More sharing options...
oliver139 Posted November 18, 2021 Share Posted November 18, 2021 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now