0

I have added one field in the configuration to add email IDS. When the order is placed by some payment method for example, "Cash on Delivery" I want to send New order email to those email IDs of the custom field.

I'm not understanding how to achieve this.

Please help me with this or give me some suggestions.

1
  • Add event "sales_order_place_after" check the payment method in observer & send email. Commented May 29, 2020 at 7:05

1 Answer 1

0

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>
8
  • It's not working. I think plugin is not calling
    – m2dev
    Commented Jun 1, 2020 at 4:21
  • observer is also not calling. Can you please write complete?
    – m2dev
    Commented Jun 1, 2020 at 4:40
  • <config xmlns:xsi="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_email_order" instance="Vendor\Module\Observer\EmailOrderSetTemplateVarsBefore" /> </event> </config>
    – m2dev
    Commented Jun 1, 2020 at 4:42
  • The answer is updated. Please check it
    – Amit Bera
    Commented Jun 1, 2020 at 6:36
  • I have added same thing before updated your answer. etc/di.xml but still not working
    – m2dev
    Commented Jun 1, 2020 at 6:53

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.