0

I am trying to set runtime Email Invoice template base on custom rule in magento 2. So I am trying to override class below: Magento\Sales\Model\Order\Email\Sender But above class is abstract classs with protected method. so I am not able to override same class with method.

I have followed below steps 1. made entry in di.xml like below

    <?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Sales\Model\Order\Pdf\Invoice" type="Custom\Sales\Model\Order\Pdf\Invoice" />
    <preference for="Magento\Sales\Model\Order\Email\Sender" type="Custom\Sales\Model\Order\Email\Sender" />

</config>

2. Sender model class: Custom\Sales\Model\Order\Email\sender.php

<?php

    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */

    /**
     * Description of Sender
     *
     * @author Pramod Kharade
     */
    namespace Custom\Sales\Model\Order\Email;

    //use Magento\Sales\Model\Order;
    //use Magento\Sales\Model\Order\Email\Container\IdentityInterface;
    //use Magento\Sales\Model\Order\Email\Container\Template;
    //use Magento\Sales\Model\Order\Address\Renderer;

    class Sender extends \Magento\Sales\Model\Order\Email\Sender
    {
        /**
         * @var \Magento\Sales\Model\Order\Email\SenderBuilderFactory
         */
        protected $senderBuilderFactory;

        /**
         * @var Template
         */
        protected $templateContainer;

        /**
         * @var IdentityInterface
         */
        protected $identityContainer;

        /**
         * @var \Psr\Log\LoggerInterface
         */
        protected $logger;

        /**
         * @var Renderer
         */
        protected $addressRenderer;

        protected $invoiceruletemp;

        /**
         * @param Template $templateContainer
         * @param IdentityInterface $identityContainer
         * @param SenderBuilderFactory $senderBuilderFactory
         * @param \Psr\Log\LoggerInterface $logger
         * @param Renderer $addressRenderer
         */
        public function __construct(
            Template $templateContainer,
            IdentityInterface $identityContainer,
            \Magento\Sales\Model\Order\Email\SenderBuilderFactory $senderBuilderFactory,
    //            \Custom\InvoiceTemplateEngine\Model\ResourceModel\Grid\Collection $invoiceruletemp,
            \Psr\Log\LoggerInterface $logger,
            Renderer $addressRenderer
        ) {
    //        $this->invoiceruletemp = $invoiceruletemp;
            $this->templateContainer = $templateContainer;
            $this->identityContainer = $identityContainer;
            $this->senderBuilderFactory = $senderBuilderFactory;
            $this->logger = $logger;
            $this->addressRenderer = $addressRenderer;
        }

        /**
         * @param Order $order
         * @return bool
         */
        protected function checkAndSend(Order $order)
        {
            $this->identityContainer->setStore($order->getStore());
            if (!$this->identityContainer->isEnabled()) {
                return false;
            }
            $this->prepareTemplate($order);

            /** @var SenderBuilder $sender */
            $sender = $this->getSender();

            try {
                $sender->send();
                $sender->sendCopyTo();
            } catch (\Exception $e) {
                $this->logger->error($e->getMessage());
            }

            return true;
        }

        /**
         * @param Order $order
         * @return void
         */
        protected function prepareTemplate(Order $order)
        {
            $this->templateContainer->setTemplateOptions($this->getTemplateOptions());

    //           $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    //$invoicerule = $objectManager->create('Custom\InvoiceTemplateEngine\Model\ResourceModel\Grid\Collection')->getCollection();
    /** Apply filters here */
    //            echo "<pre>";
    //            print_r(get_class_methods($invoicerule)); die;
    //            foreach ($invoicerule as $key => $val){
    //            print_r($val);    
    //            }
    //             die;
            if ($order->getCustomerIsGuest()) {
                $templateId = $this->identityContainer->getGuestTemplateId();
                $customerName = $order->getBillingAddress()->getName();
            } else {
                $templateId = $this->identityContainer->getTemplateId();
             //echo $templateId." Template ID Custom"; die();

                $customerName = $order->getCustomerName();
            }

            $this->identityContainer->setCustomerName($customerName);
            $this->identityContainer->setCustomerEmail($order->getCustomerEmail());
            $this->templateContainer->setTemplateId($templateId);
            //$templateId= 4;
            $this->templateContainer->setTemplateId($templateId);
        }

        /**
         * @return Sender
         */
        protected function getSender()
        {
            return $this->senderBuilderFactory->create(
                [
                    'templateContainer' => $this->templateContainer,
                    'identityContainer' => $this->identityContainer,
                ]
            );
        }

        /**
         * @return array
         */
        protected function getTemplateOptions()
        {
            return [
                'area' => \Magento\Framework\App\Area::AREA_FRONTEND,
                'store' => $this->identityContainer->getStore()->getStoreId()
            ];
        }

        /**
         * @param Order $order
         * @return string|null
         */
        protected function getFormattedShippingAddress($order)
        {
            return $order->getIsVirtual()
                ? null
                : $this->addressRenderer->format($order->getShippingAddress(), 'html');
        }

        /**
         * @param Order $order
         * @return string|null
         */
        protected function getFormattedBillingAddress($order)
        {
            return $this->addressRenderer->format($order->getBillingAddress(), 'html');
        }
    }

Thanks In advance!

4
  • Did you try override using plugin ? – Kishan Patadia Jun 5 '17 at 12:20
  • 1
    Yes but not got success – Pramod Kharade Jun 5 '17 at 12:22
  • You can override a method who calls this private method if the calling method is public. – Sejal Shah Jun 10 '17 at 16:47
  • 1
    @sejal Shah Could you please post and below? I had tried by overriding class using preference. But not able getting call to override class. – Pramod Kharade Jun 12 '17 at 1:23
0

You can't override a protected function in an abstract class, either using preference or plugin. Like others have mentioned, you have to override the function in the derivative class.

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.