Yes, you can achieve this requirement by few customizations
Step1: Create a class which has setter and get method and using class you can pass an email to Magento\Sales\Model\Order\Email\Container\OrderIdentity:getEmailCopyTo
.
Code
<?php
namespace DevAmitBera\OrderEmail\Model;
class OrderEmail
{
private $extraOrderEmailId;
public function set($email)
{
$this->extraOrderEmailId = $email;
}
public function get()
{
return $this->extraOrderEmailId;
}
}
Step2: Create an observer on email_order_set_template_vars_before
event and your custom email $extraOrderEmailId
of DevAmitBera\OrderEmail\Model\Order\OrderEmail
.
Code
<?php
namespace DevAmitBera\OrderEmail\Observer;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
class EmailOrderSetTemplateVarsBefore implements ObserverInterface
{
/**
* @var \Ronin\ErpOrder\Model\OrderEmail
*/
private $orderEmail;
public function __construct(
\Ronin\ErpOrder\Model\OrderEmail $orderEmail
){
$this->orderEmail = $orderEmail;
}
/**
* @inheritDoc
*/
public function execute(Observer $observer)
{
$transportObject = $observer->getEvent()->getData('transportObject');
if($transportObject->getData('order')){
$order = $transportObject->getData('order');
$payment = $order->getPayment();
if($payment->getMethod() == 'cashondelivery'){
$this->orderEmail->set(['[email protected]','[email protected]']);
}
}
}
}
Step3: Create a plugin on Magento\Sales\Model\Order\Email\Container\OrderIdentity::getEmailCopyTo
and then using after Plugin on the method getEmailCopyTo
and your Custom email to order list
Code
<?php
namespace DevAmitBera\OrderEmail\Plugin\Order\Email\Container;
class OrderIdentity
{
/**
* @var \Ronin\ErpOrder\Model\OrderEmail
*/
private $orderEmail;
public function __construct(
\Ronin\ErpOrder\Model\OrderEmail $orderEmail
){
$this->orderEmail = $orderEmail;
}
public function afterGetEmailCopyTo(
\Magento\Sales\Model\Order\Email\Container\OrderIdentity $subject,
$result
){
$additionalEmailId = $this->orderEmail->get();
if(($additionalEmailId === null) || (!$additionalEmailId) || empty($additionalEmailId)){
return $result;
}
if (!empty($result) && is_array($result)){
foreach($additionalEmailId as $eachEmail){
$result[] = (string) $eachEmail;
}
return $result;
}else{
return [$additionalEmailId];
}
}
}
events.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="email_order_set_template_vars_before">
<observer name="set_extra_params_to_order"
instance="DevAmitBera\OrderEmail\Observer\EmailOrderSetTemplateVarsBefore" />
</event>
</config>
di.xml for plugin definition
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Sales\Model\Order\Email\Container\OrderIdentity">
<plugin name="add_addition_class_to"
type="DevAmitBera\OrderEmail\Plugin\Order\Email\Container\OrderIdentity"
sortOrder="1" disabled="false" />
</type>
</config>