Magento 2 – Email Template Based on Payment Method

checkoutmagento2

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?

Best Answer

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);
       }
    
     }
    
Related Topic