2

I wonder if there is a build in magento module which manages emails for non working days. The email sent on non working day will be different than the email sent on working day.

If no module exist, please guide me to the recommended way to do it.

  • Did you manage to solve the problem? – Bartosz Herba Dec 8 '17 at 10:18
  • I am working on it using your solution – Moe Hammadi Dec 8 '17 at 11:42
  • You can create custom template and override Magento\Sales\Order\Email\SenderBuilder classs and check condition and set email template accordingly . – Pramod Kharade Dec 12 '17 at 6:11
  • I have created a module which works fine for me. I used information from both answers so I am confused for whom to award the bounty. – Moe Hammadi Dec 13 '17 at 14:23
4
+50

There is no such a thing as you want to and therefore customization is required, however it is quite easy to achieve. I assume that you have basic knowledge on how to create your own standalone module and how to write plugins or override classes. If not then please read resources as the first step:

  1. Hello World Module
  2. DI

If you have these basics now, all you have to do is:

  1. Create a new module basics (registration.php, module.xml, composer.json)
  2. Override Magento\Sales\Order\Email\SenderBuilder.php

There you have a send method but more importantly, there is a protected configureEmailTemplate method [96] which holds configuration logic for the email. all you have to do is add new logic with regards to your data requiremnts and pass your template_id in:

$this->transportBuilder->setTemplateIdentifier($this->templateContainer->getTemplateId());

Preferably you will create a new class responsible for resolving template_id with regards to requirements, inject it into a Sender's constructor and delegate revolving template.

  1. To add your own order email template you must create etc/email_templates.xml inside of your module and add new template, eg:

    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Email:etc/email_templates.xsd"> <template id="sales_email_order_template" label="New Order" file="order_new.html" type="html" module="Magento_Sales" area="frontend"/> </config>

Element's attributes are obvious and you can always read any file in any module to get more examples.

  • This heler me a lot – Moe Hammadi Dec 14 '17 at 9:30
1

No matter which email template you want to change on non-working days. If you want to change the default magento templates, then you have to override few core files using plugin as this is the standard way. Create your own module first, in which define your custom email templates in email_templates.xml. The files you will override will contain

$this->getTemplateId()

Define you template id as:-

const XML_PATH_EMAIL_TEMPLATE_FIELD  = 'section/group/your_email_template_field_id';

const XML_PATH_EMAIL_TEMPLATE_FIELD2  = 'section/group/your_email_template_field_id_2';

Then just put $this->getTemplateId() inside if else as:-

if($day==$working_day) {
$this->temp_id = $this->getTemplateId(self::XML_PATH_EMAIL_TEMPLATE_FIELD);

}
else {
$this->temp_id = $this->getTemplateId(self::XML_PATH_EMAIL_TEMPLATE_FIELD2);

}
$this->inlineTranslation->suspend();    
            $this->generateTemplate($emailTemplateVariables,$senderInfo,$receiverInfo);    
            $transport = $this->_transportBuilder->getTransport();
            $transport->sendMessage();        
            $this->inlineTranslation->resume();

Above code may not be suitable for your condition. But as per your query to change email templates on non-working days. You can follow the same logic. Or you can send me your code for more specific answer. Hope it helps!!

  • the answer was very useful. – Moe Hammadi Dec 14 '17 at 9:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.