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
Class
Custom\Override\Model\Email\Sender\OrderSender