0

I have one module where i can set email address in back end. When order placed i want to send that order email copy to my custom email address which is set in backend.

I want to add an observer when order placed and send email copy to my custom email address.

how can i achieve this functionality with custom module?

Thanks.

14
  • Hey, so you want to send a new order email to the client and your custom email? Magento has that option, it is a requirement being a custom module? – Rui Silva Dec 22 '20 at 11:23
  • I know that Magento has that config in admin. But for example I buy a product and I want to tell a friend that I just bought the product. So I want to write this module – 98 boycodobv Dec 22 '20 at 11:51
  • So you need that to work on the frontend, so the customer insert another email to where the new order email will be sent to along with the one to himself? or just on the backend and only admins can select the email it will be sent to? – Rui Silva Dec 22 '20 at 12:12
  • 1
    magento.stackexchange.com/a/277626/86398 This might help, I look a bit fast but I think it might be the way, later I'll check with more time – Rui Silva Dec 22 '20 at 13:11
  • 1
    Hey, I'm checking it, can you try to getSender and then use $sender->sendCopyTo($email) , I don't know if it will work using the sendCopyTo this way but if you can try it. Check vendor/magento/module-sales/Model/Order/Email/Sender.php might help a bit as example of what magento core use – Rui Silva Dec 22 '20 at 15:17
1

observer and event.xml

    /**
     * @var \Magento\Checkout\Model\Session
     */
    protected $_checkoutSession;
    public function __construct(
        \Magento\Checkout\Model\Session $checkoutSession
    )
    {
        $this->_checkoutSession = $checkoutSession;
    }
    /**
     * @param Observer $observer
     *
     * @return void
     */
    public function execute(Observer $observer)
    {
        $order = $observer->getEvent()->getOrder();
        $quote = $observer->getEvent()->getQuote();
        $this->_checkoutSession->setEmailCc($quote->getData(CustomFieldsInterface::CHECKOUT_EMAIL_CC));

    }

override plugin Magento\Sales\Model\Order\Email\Container\OrderIdentity

public function getEmailCopyTo()
    {

        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $currentEmailCc = $objectManager->get('Magento\Checkout\Model\Session');
        $emailCC = $currentEmailCc->getEmailCc();

        $data = $this->getConfigValue(self::XML_PATH_EMAIL_COPY_TO, $this->getStore()->getStoreId());

        $data2 = $data.','.$emailCC;

        if (!empty($data) && !empty($emailCC)) {
            return array_map('trim', explode(',', $data2));
        }
        if (empty($data) && !empty($emailCC)){
            return array_map('trim', explode(',', $emailCC));
        }
        if (!empty($data) && empty($emailCC) ){
            return array_map('trim', explode(',', $data));
        }
        if (empty($data) && empty($emailCC)) {
            return false;
        }

    }

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.