2

I want to use separate email templates for orders depending on the payment method selected.

I have looked around but not found anything to achieve this so I started trying to build a module to handle this.

I have started with overwriting the function configureEmailTemplate() within Magento/Sales/Model/Order/Email/SenderBuilder

protected function configureEmailTemplate()
{
     /**
     * @var \Magento\Checkout\Model\Session $_checkoutSession
     */

    if ($this->_checkoutSession->getQuote()->getPayment()->getMethod() == "LEASING") {
        $this->transportBuilder->setTemplateIdentifier("leasingid");
    } else {
        $this->transportBuilder->setTemplateIdentifier($this->templateContainer->getTemplateId());
    }
    
    $this->transportBuilder->setTemplateIdentifier($this->templateContainer->getTemplateId());
    $this->transportBuilder->setTemplateOptions($this->templateContainer->getTemplateOptions());
    $this->transportBuilder->setTemplateVars($this->templateContainer->getTemplateVars());
    $this->transportBuilder->setFrom($this->identityContainer->getEmailIdentity());
}

This is an example of what I want to achieve. I am unsure how I can call this checkoutSession data so have added

     use Magento\Checkout\Model\Session;

and

    protected $_checkoutSession;

So I can determine my payment method. Then set the template based on that payment method using something like below.

if ($this->_checkoutSession->getQuote()->getPayment()->getMethod() == "LEASING") {
    $this->transportBuilder->setTemplateIdentifier("leasingid");
} 

I have been trying to get a message to log, to begin with, but nothing is showing. I would like to know if this is the correct way to go about achieving this before i invest lots of time in figuring this out?

1 Answer 1

4

You can try this:

  • Create an module for override core code. In this case Custom_Override

  • File app/code/Custom/Override/etc/di.xml

    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
        <preference for="Magento\Sales\Model\Order\Email\Sender\OrderSender" type="Custom\Override\Model\Email\Sender\OrderSender"/>
    </config>
    
  • Class Custom\Override\Model\Email\Sender\OrderSender

    <?php
    namespace Custom\Override\Model\Email\Sender;
    
    use Magento\Sales\Model\Order;
    
    class OrderSender extends \Magento\Sales\Model\Order\Email\Sender\OrderSender
    {
       protected function prepareTemplate(Order $order)
       {
           parent::prepareTemplate($order);
    
           //Get Payment Method
           $paymentMethod = $order->getPayment()->getMethod();
    
           //Define email template for each payment method
           switch ($paymentMethod) {
               case 'cashondelivery' : $templateId = 'custom_template_cod'; break;
               case 'checkmo' : $templateId = 'custom_template_checkmo'; break;
               // Add cases if you have more payment methods
               default:
                   $templateId = $order->getCustomerIsGuest() ?
                       $this->identityContainer->getGuestTemplateId()
                       : $this->identityContainer->getTemplateId();
    
           }
    
           $this->templateContainer->setTemplateId($templateId);
       }
    
     }
    
4
  • Thankyou! Yeah this seems to be good. I am having an issue with the default case however not sending emails.
    – harri
    May 22, 2017 at 14:02
  • Nope that seems all good email got lost somewhere and arrived a little late i think all working now. Thanks so much this is perfect.
    – harri
    May 22, 2017 at 14:11
  • You're welcome :). I think Magento 2 still set order emails into queue and run cron to send them
    – Quan Le
    May 23, 2017 at 3:01
  • Override is a bad practice. You should avoid that. Use plugin for a \Magento\Sales\Model\Order\Email\Container\Template::getTemplateId (afterGetTemplateId) and modify templateId as you need
    – Styopchik
    Nov 6, 2020 at 10:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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