5

What is the proper way to assign custom email template to specific payment method upon creating an order? For example:

When choosing a Bank transfer payment method on the checkout it should send custom email template upon submiting the order, not the default order email.

I've looked around but found no proper solution for this issue, only this: Magento 2: how to declare a custom email template for your module

0

A module can be made with below code to override "Magento\Sales\Model\Order\Email\Sender\OrderSender"

File: 'app/code/Vendor/Module/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="Vendor\Module\Model\Email\Sender\OrderSender"/>
</config>

File: 'app/code/Vendor/Module/Email/Sender/OrderSender.php'

Class Vendor\Module\Model\Email\Sender\OrderSender    
namespace Vendor\Module\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);
  }

 }

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.