1

In Magento I can't override abstract class so I tried to use the around plugin. How can I do it for prepareTemplate method in Magento\Sales\Model\Order\Email\Sender abstract class? Right now I did some thing like this: di.xml

<?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\Sender">
        <plugin name="sale_order_sender_custom_email"
                type="Vendor\Module\Plugin\Magento\Sales\Model\Order\Email\SenderPlugin"
                sortOrder="1"/>
    </type>
</config>

SenderPlugin.php

<?php

namespace Vendor\Module\Plugin\Magento\Sales\Model\Order\Email;

use Closure;
use Magento\Sales\Model\Order;
use Magento\Sales\Model\Order\Email\Sender;
use Magento\Sales\Model\Order\Email\Container\IdentityInterface;
use Magento\Sales\Model\Order\Email\Container\Template;

class SenderPlugin
{

    public $templateContainer;
    public $identityContainer;

    public function __construct(
        Template $templateContainer,
        IdentityInterface $identityContainer
    )
    {
        $this->templateContainer = $templateContainer;
        $this->identityContainer = $identityContainer;
    }

    public function aroundPrepareTemplate(Sender $subject, Closure $proceed, Order $order)
    {
        $this->templateContainer->setTemplateOptions($this->getTemplateOptions());

        if ($order->getCustomerIsGuest()) {
            $templateId = $this->identityContainer->getGuestTemplateId();
            $customerName = $order->getBillingAddress()->getName();
        } else {
            $templateId = $this->identityContainer->getTemplateId();
            $customerName = $order->getCustomerName();
        }

        $this->identityContainer->setCustomerName($customerName);
        $this->identityContainer->setCustomerEmail($order->getCustomerEmail());
        $this->templateContainer->setTemplateId('custom_email_template');
    }

}
0

Were you able to fix this one? As much I recall, you cannot plugin a protected method.

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.