Mathew Nwaneri Posted April 8, 2022 Share Posted April 8, 2022 Hi Everyone, We have custom delivered.html and delivered.txt file in mails folder and we need to get product and URL related to order in the email template. Which class can help or any way to pass value to the template file? We need the product name and link for which order status is delivered. Kind regards and thanks in advanced for your help. 1 Link to comment Share on other sites More sharing options...
Ress Posted April 8, 2022 Share Posted April 8, 2022 You can use the "actionEmailSendBefore" hook. You receive as a parameter, the template, as well as the variables already sent, which you can modify. 1 Link to comment Share on other sites More sharing options...
Mathew Nwaneri Posted April 8, 2022 Author Share Posted April 8, 2022 1 minute ago, Ress said: You can use the "actionEmailSendBefore" hook. You receive as a parameter, the template, as well as the variables already sent, which you can modify. Thank you very much Ress for your quick response. 😊 Can you please be kind enough in giving details how this can be done? Regards. 1 Link to comment Share on other sites More sharing options...
Ress Posted April 8, 2022 Share Posted April 8, 2022 In a module, add the function: public function hookActionEmailSendBefore($params) { $template_name = $params['template']; // here you will have to check the template name so that you can make the changes only on the template you need $templateVars = $params['templateVars']; // from this variable, you will have to go through the products of the order, and add what you need //you will need to add the new variable to them $params['templateVars']['new_variable'] = $new_variable; } Don't forget to register the hook. 1 Link to comment Share on other sites More sharing options...
Mathew Nwaneri Posted April 8, 2022 Author Share Posted April 8, 2022 2 minutes ago, Ress said: In a module, add the function: public function hookActionEmailSendBefore($params) { $template_name = $params['template']; // here you will have to check the template name so that you can make the changes only on the template you need $templateVars = $params['templateVars']; // from this variable, you will have to go through the products of the order, and add what you need //you will need to add the new variable to them $params['templateVars']['new_variable'] = $new_variable; } Don't forget to register the hook. Thanks once again. I am not developing a module, I just need an email to be sent to customers when delivery status is changed to delivered, similar to order confirmation. I suppose your solution is more fit if I'm using a module to send these email but I am using prestashop's core mail configuration I already found the "hookActionEmailSendBefore" in the mail.php and it has reference to all templates and not one in particular $hookBeforeEmailResult = Hook::exec( 'actionEmailSendBefore', [ 'idLang' => &$idLang, 'template' => &$template, 'subject' => &$subject, 'templateVars' => &$templateVars, 'to' => &$to, 'toName' => &$toName, 'from' => &$from, 'fromName' => &$fromName, 'fileAttachment' => &$fileAttachment, 'mode_smtp' => &$mode_smtp, 'templatePath' => &$templatePath, 'die' => &$die, 'idShop' => &$idShop, 'bcc' => &$bcc, 'replyTo' => &$replyTo, ], null, Sincerely, I asked a developer whois good in what he does, but after several checks, he owned up on finding the right solution. Regards and thank you for any further help. 1 Link to comment Share on other sites More sharing options...
knacky Posted April 8, 2022 Share Posted April 8, 2022 (edited) So add your own parameters and condition there Mail.php. You can find a more accurate example in ./classes/PaymentModule.php Eg.: $id_order = Tools::getValue('id_order') ?: $this->context->order->id; if ($template == 'delivered' && $id_order){ $template = 'delivered'; $order = new Order((int) $id_order); $getProducts = $order->getProducts(true); $idLang = (int) $order->id_lang; $product_var_tpl_list = []; $products_list = ''; foreach ($getProducts as $product){ $product_var_tpl = [ 'id_product' => $product['id_product'], 'id_product_attribute' => $product['id_product_attribute'], 'reference' => $product['reference'], 'name' => $product['name'] . (isset($product['attributes']) ? ' - ' . $product['attributes'] : ''), 'quantity' => $product['quantity'], 'customization' => [], ]; if (isset($product['price']) && $product['price']) { $product_var_tpl['unit_price'] = Tools::getContextLocale($this->context)->formatPrice($product_price, $this->context->currency->iso_code); $product_var_tpl['unit_price_full'] = Tools::getContextLocale($this->context)->formatPrice($product_price,$this->context->currency->iso_code).''. $product['unity']; } else { $product_var_tpl['unit_price'] = $product_var_tpl['unit_price_full'] = ''; } $product_var_tpl_list[] = $product_var_tpl; ..... } $product_var_tpl_list[] = $product_var_tpl; $product_list_txt = ''; $product_list_html = ''; if (count($product_var_tpl_list) > 0) { $product_list_txt = $this->getEmailTemplateContent('order_conf_product_list.txt', Mail::TYPE_TEXT, $product_var_tpl_list); $product_list_html = $this->getEmailTemplateContent('order_conf_product_list.tpl', Mail::TYPE_HTML, $product_var_tpl_list); } $templateVars= [ '{products}' => $products, '{order_reference}' => $order->reference, ..... ]; $to = $this->context->customer->email; $toName = $this->context->customer->firstname . ' ' . $this->context->customer->lastname; ..... $subject = $this->context->getTranslator()->trans( 'Order delivered', [], 'Emails.Subject', $orderLanguage->locale ); } Edited April 8, 2022 by knacky (see edit history) 2 Link to comment Share on other sites More sharing options...
Mathew Nwaneri Posted April 8, 2022 Author Share Posted April 8, 2022 20 minutes ago, knacky said: So add your own parameters and condition there Mail.php. You can find a more accurate example in ./classes/PaymentModule.php Eg.: $id_order = Tools::getValue('id_order') ?: $this->context->order->id; if ($template == 'delivered' && $id_order){ $template = 'delivered'; $order = new Order((int) $id_order); $getProducts = $order->getProducts(true); $idLang = (int) $order->id_lang; $product_var_tpl_list = []; $products_list = ''; foreach ($getProducts as $product){ $product_var_tpl = [ 'id_product' => $product['id_product'], 'id_product_attribute' => $product['id_product_attribute'], 'reference' => $product['reference'], 'name' => $product['name'] . (isset($product['attributes']) ? ' - ' . $product['attributes'] : ''), 'quantity' => $product['quantity'], 'customization' => [], ]; if (isset($product['price']) && $product['price']) { $product_var_tpl['unit_price'] = Tools::getContextLocale($this->context)->formatPrice($product_price, $this->context->currency->iso_code); $product_var_tpl['unit_price_full'] = Tools::getContextLocale($this->context)->formatPrice($product_price,$this->context->currency->iso_code).''. $product['unity']; } else { $product_var_tpl['unit_price'] = $product_var_tpl['unit_price_full'] = ''; } $product_var_tpl_list[] = $product_var_tpl; ..... } $product_var_tpl_list[] = $product_var_tpl; $product_list_txt = ''; $product_list_html = ''; if (count($product_var_tpl_list) > 0) { $product_list_txt = $this->getEmailTemplateContent('order_conf_product_list.txt', Mail::TYPE_TEXT, $product_var_tpl_list); $product_list_html = $this->getEmailTemplateContent('order_conf_product_list.tpl', Mail::TYPE_HTML, $product_var_tpl_list); } $templateVars= [ '{products}' => $products, '{order_reference}' => $order->reference, ..... ]; $to = $this->context->customer->email; $toName = $this->context->customer->firstname . ' ' . $this->context->customer->lastname; ..... $subject = $this->context->getTranslator()->trans( 'Order delivered', [], 'Emails.Subject', $orderLanguage->locale ); } Hey knacky, thanks, tried adding the code but no luck. I've sent this tread to my developer, hopefully he will make a good use of it. Regards. 1 Link to comment Share on other sites More sharing options...
knacky Posted April 8, 2022 Share Posted April 8, 2022 (edited) The code is only sample, it is not fully functional. It makes sense to create a small module and attach it to the hook. Because when you update Prestashop, changes and modifications in Mail.php will be overwritten. A good programmer can have a ready-made solution within a maximum of two hours 😉 Edited April 8, 2022 by knacky (see edit history) 1 Link to comment Share on other sites More sharing options...
Mathew Nwaneri Posted April 8, 2022 Author Share Posted April 8, 2022 2 minutes ago, knacky said: It makes sense to create a small module and attach it to the hook. Because when you update Prestashop, changes and modifications in Mail.php will be overwritten. A good programmer can have a ready-made solution within a maximum of two hours 😉 Great, thanks. Will work towards this. Quite right, I was worried also of the changes being overwritten with updates. Have a great weekend man. Cheers. Link to comment Share on other sites More sharing options...
Zohaib-fk Posted April 9, 2022 Share Posted April 9, 2022 (edited) Hello, Which hook get called when order status changed to "Delivered". Currently I tried below hooks but they are getting called when order is confirmed. hookActionOrderStatusPostUpdate hookActionOrderStatusUpdate hookActionObjectOrderHistoryAddAfter PrestaShop Documentation Link => https://devdocs.prestashop.com/1.7/modules/concepts/hooks/list-of-hooks/ Edited April 11, 2022 by Zohaib-fk (see edit history) Link to comment Share on other sites More sharing options...
Zohaib-fk Posted April 11, 2022 Share Posted April 11, 2022 I got the solution by checking value of order status from tables ps_order_state_lang and ps_order_state, Orders menu option has Status value in list view of all orders. 1 Link to comment Share on other sites More sharing options...
Mathew Nwaneri Posted April 19, 2022 Author Share Posted April 19, 2022 Good morning everyone, Hope your holidays went well. Please, we have made some progress, as @knacky advised, we were able to create a module, only we still have a little bottleneck to overcome. How to get the category ID from Product ID To finish complete the project, we need to get the category ID or name from the Product Order. Currently, we are able to get the product ID from the table ps_order_detail. Below is the query. "SELECT product_id,product_name,id_order,id_shop FROM ps_order_detail ORDER BY id_order_detail DESC LIMIT 1" We want to know how to get the category ID from Product ID or from the Orders table or Orders. For example below are the product details Product ID => 17 Category ID => 83 URL => https://lordsworld.eu/es/corrector-de-ph/1-kg-de-ph-minus-reductor-corrector-de-ph-granular For the module to work as desired, we need the category ID of the ordered product. Thanks in advance to whoever can contribute in achieving this goal. Regards. Link to comment Share on other sites More sharing options...
Ress Posted April 19, 2022 Share Posted April 19, 2022 Hi, If you have a product id, you can do the following: $product = new Product($id_product, false, $id_lang); //default category id $id_category = $product->id_category_default; //all product categories id's $id_categories = $product->getCategories(); //all product categories, with details $categories = Product::getProductCategoriesFull($id_product, $id_lang); Link to comment Share on other sites More sharing options...
knacky Posted April 19, 2022 Share Posted April 19, 2022 It already has all the parameters in the '{products}' variable. $product.id_category_default, $product.id_product ..... Link to comment Share on other sites More sharing options...
Zohaib-fk Posted April 26, 2022 Share Posted April 26, 2022 (edited) Thanks. The below code helps to get the product URL with the Category name in the URL. I hope it will be helpful for the community. // SFK Modules $product = new Product($id_product , false, 1); $link = new Link(); //all product categories, with details $categories = Product::getProductCategoriesFull($id_product, $id_lang); $id_category = $product->id_category_default; $category_link_rewrite = $categories[$id_category]['link_rewrite']; $sfk_product_url = $link->getProductLink($product,null,$category_link_rewrite,null,$id_lang,$id_shop,0, false, false, false); Edited April 26, 2022 by Zohaib-fk (see edit history) 1 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