0

I override the \Magento\Sales\Model\Order\Email\SenderBuilder class in my Magento 2. I want to load some order data but I am not getting anything.

Code :

<?php
namespace Customisation\OrderEmail\Model\Order\Email;

use Magento\Framework\App\ObjectManager;
use Magento\Framework\Mail\Template\TransportBuilder;
use Magento\Framework\Mail\Template\TransportBuilderByStore;
use Magento\Sales\Model\Order\Email\Container\IdentityInterface;
use Magento\Sales\Model\Order\Email\Container\Template;

class SenderBuilder extends \Magento\Sales\Model\Order\Email\SenderBuilder
{
    protected $templateContainer;
    protected $identityContainer;
    protected $transportBuilder;
    private $transportBuilderByStore;

    public function __construct(
        Template $templateContainer,
        IdentityInterface $identityContainer,
        TransportBuilder $transportBuilder,
        TransportBuilderByStore $transportBuilderByStore = null,

    ) {
        $this->templateContainer = $templateContainer;
        $this->identityContainer = $identityContainer;
        $this->transportBuilder = $transportBuilder;
        $this->transportBuilderByStore = $transportBuilderByStore ?: ObjectManager::getInstance()->get(
            TransportBuilderByStore::class
        );
        parent::__construct($templateContainer, $identityContainer, $transportBuilder, $this->transportBuilderByStore);

    }

    public function send()
    {
        //$custom_email = '[email protected]';
        $_objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $this->configureEmailTemplate();

        $this->transportBuilder->addTo(
            $this->identityContainer->getCustomerEmail(),
            $this->identityContainer->getCustomerName()
        );

        $copyTo = $this->identityContainer->getEmailCopyTo();

        if (!empty($copyTo) && $this->identityContainer->getCopyMethod() == 'bcc') {
            foreach ($copyTo as $email) {
                $this->transportBuilder->addBcc($email);
            }
        }

        if($email){
            $this->transportBuilder->addBcc($custom_email); // Added for add BCC for custom dynamic email address
            $logger->info($email);
        }

        $transport = $this->transportBuilder->getTransport();
        $transport->sendMessage();
    }
}

I want to get order data in this send(). How can I get order id or Order in this function ?

1 Answer 1

6

There should be order instance in the template variables.

$vars = $this->templateContainer->getTemplateVars();
$order = $vars['order'];

Might be slightly different depending on the version you are running. Check the prepareTemplate method in Magento\Sales\Model\Order\Email\Sender class.

7
  • Thank you. But I am getting following error Uncaught Error: Call to undefined method getTemplateVars(). My magento version is 2.3.2 Apr 20, 2020 at 12:02
  • I've updated the answer. @DavidCoder Apr 20, 2020 at 12:32
  • Now getting error: Call to a member function getOrder() on array Apr 20, 2020 at 12:38
  • getting above error in $order = $vars->getOrder(); line Apr 20, 2020 at 12:42
  • 1
    Hmm, I thought it was DataObject for some reason. Apr 20, 2020 at 12:48

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.