1

I want to override PrepareTemplate function of Sender.php and have tried to override Sender.php with the help of link Override abstract class but its not working for me.

I have followed the exact steps mentioned in the above link which are as below:

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">
    <preference for="Magento\Sales\Model\Order\Email\Sender" type="Module\Sales\Rewrite\Sender" />
</config>

Module\Sales\Rewrite\Sender.php

namespace Module\Sales\Rewrite;

class Sender extends \Magento\Sales\Model\Order\Email\Sender
{

 protected function prepareTemplate(Order $order)
 {
    $orderstatus = $order->getStatus();
    //here i want to send a custom email template if the order status is  complete
 }
}

The problem is that the overrided file is not called.

Can someone please help in here or can mention any other alternative.

0

I havent tried your code yet. But you try this code: (Sorry, i dont remember the source link).

Remember to replace with namespace that you want to override.

Look at how catalog product class is defined in the di.xml file of the Catalog module:

<config>
<preference for="Magento\Catalog\Api\Data\ProductInterface" type="Magento\Catalog\Model\Product" />

To override Magento\Catalog\Model\Product class all we need is to define our preference and to create a file that will extend the original class:

<config>
<preference for="Magento\Catalog\Api\Data\ProductInterface" type="Inchoo\Catalog\Model\Product" /></config>

And

<?php
namespace Inchoo\Catalog\Model;
class Product extends \Magento\Catalog\Model\Product
{// code}

In etc/module.xml(To make sure we have the right order of module dependencies):

<module name="Inchoo_Catalog" setup_version="0.0.1">
    <sequence>
        <module name="Magento_Catalog" />
    </sequence>
</module>
0

We can't rewrite the method of an abstract class, in this case, it is \Magento\Sales\Model\Order\Email\Sender

Instead of that, we need to find the actual class that is being used, likely it is Magento\Sales\Model\Order\Email\Sender\OrderSender

Can you try updating your di.xml to?

<?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\Email\Sender\OrderSender" type="Module\Sales\Rewrite\Sender" />
</config>
0

Since your requirement is to send custom email template based on order status, you might have to override couple of files based on how status changes in your case:

  1. If let's say order status is changed to Complete once shipment is created then you just have to change the shipment email template vendor/magento/module-sales/view/frontend/email/shipment_new.html and/or vendor/magento/module-sales/view/frontend/email/shipment_new_guest.html by placing it into app/design/your-theme/ folder having same folder structure.

  2. Another case is you change the order status to Complete using dropdown provided on order detail page (admin panel) then you will have to override OrderCommentSender file as follows:

di.xml

<preference for="Magento\Sales\Model\Order\Email\Sender\OrderCommentSender" type="Vendor\Sales\Model\Order\Email\Sender\OrderCommentSender" />

Vendor\Sales\Model\Order\Email\Sender\OrderCommentSender.php

<?php
namespace Vendor\Sales\Model\Order\Email\Sender;

use Magento\Sales\Model\Order;

/**
 * Class OrderCommentSender
 */
class OrderCommentSender extends \Magento\Sales\Model\Order\Email\Sender\OrderCommentSender
{
    /**
     * @param Order $order
     * @return void
     */
    protected function prepareTemplate(Order $order)
    {
        //Custom code to override the template
    }
}

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.