0

I'm creating a module that checks if the order was made by a certain group, in this case wholesale groups, and if yes send the copy to new order email to a different email address. It's okay if it still sends to the main address but I can't add another one in the admin area because only the wholesale ones matter. I already created a module with a sales_order_save_after event and Observer, but I don't know how to get the new order email of that order and send it again to the custom email.

Thanks! Best regards, Rui Silva

2
  • Hope this can help: mageplaza.com/devdocs/…
    – Amit Saini
    Mar 30, 2021 at 5:01
  • Hey @AmitSaini, I checked wouldn't this $order->setCustomerEmail($email); change the customer email set in the order? Thanks!
    – Rui Silva
    Mar 30, 2021 at 8:46

1 Answer 1

0
<?php

namespace [Vendor]\[Module]\Helper;

use Magento\Framework\App\Helper\Context;
use Magento\Framework\Mail\Template\TransportBuilder;
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\Translate\Inline\StateInterface;
use Magento\Store\Model\StoreManagerInterface;

class Data extends AbstractHelper
{
    protected $transportBuilder;
    protected $storeManager;
    protected $inlineTranslation;

    public function __construct(
        Context $context,
        TransportBuilder $transportBuilder,
        StoreManagerInterface $storeManager,
        StateInterface $state
    )
    {
        $this->transportBuilder = $transportBuilder;
        $this->storeManager = $storeManager;
        $this->inlineTranslation = $state;
        parent::__construct($context);
    }

    public function sendEmail()
    {
        // this is an example and you can change template id,fromEmail,toEmail,etc as per your need.
        $templateId = 'my_custom_email_template'; // template id
        $fromEmail = '[email protected]';  // sender Email id
        $fromName = 'Admin';             // sender Name
        $toEmail = '[email protected]'; // receiver email id

        try {
            // template variables pass here
            $templateVars = [
                'msg' => 'test',
                'msg1' => 'test1'
            ];

            $storeId = $this->storeManager->getStore()->getId();

            $from = ['email' => $fromEmail, 'name' => $fromName];
            $this->inlineTranslation->suspend();

            $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;
            $templateOptions = [
                'area' => \Magento\Framework\App\Area::AREA_FRONTEND,
                'store' => $storeId
            ];
            $transport = $this->transportBuilder->setTemplateIdentifier($templateId, $storeScope)
                ->setTemplateOptions($templateOptions)
                ->setTemplateVars($templateVars)
                ->setFrom($from)
                ->addTo($toEmail)
                ->getTransport();
            $transport->sendMessage();
            $this->inlineTranslation->resume();
        } catch (\Exception $e) {
            $this->_logger->info($e->getMessage());
        }
    }
}
1
  • Hey, with that option how do I get the New order email template, if I just insert the ID on $templateId it will work or the base vars would not work and would need to create ones just for this one? Thanks!
    – Rui Silva
    Mar 30, 2021 at 8:49

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.