3

How to override below file in module ?

Magento\Sales\Model\Order\Email\Sender\CreditmemoSender.php
6
  • 2
    You can use preference in di.xml or intercept the method using plugin. Commented Feb 19, 2021 at 13:55
  • Did you get your answer? Or you need any help?
    – ZealousWeb
    Commented Feb 22, 2021 at 9:31
  • I have added answer. Please check and let me know if you have any query.
    – ZealousWeb
    Commented Feb 22, 2021 at 9:50
  • Have you checked answer?
    – ZealousWeb
    Commented Feb 22, 2021 at 12:07
  • @devhs Are you able to do it?
    – ZealousWeb
    Commented Feb 23, 2021 at 5:51

1 Answer 1

2

With below code you can override CreditmemoSender.php file

Step 1: Create di.xml file PackageName/Module/etc/

<?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\CreditmemoSender" type="PackageName\Module\Model\Rewrite\Order\Email\Sender\CreditmemoSender" />
</config>

Step 2: Create file CreditmemoSender.php under path PackageName/Module/Model/Rewrite/Order/Email/Sender

<?php
namespace PackageName\Module\Model\Rewrite\Order\Email\Sender;

use Magento\Payment\Helper\Data as PaymentHelper;
use Magento\Sales\Model\Order;
use Magento\Sales\Model\Order\Creditmemo;
use Magento\Sales\Model\Order\Email\Container\CreditmemoIdentity;
use Magento\Sales\Model\Order\Email\Container\Template;
use Magento\Sales\Model\Order\Email\Sender;
use Magento\Sales\Model\ResourceModel\Order\Creditmemo as CreditmemoResource;
use Magento\Sales\Model\Order\Address\Renderer;
use Magento\Framework\Event\ManagerInterface;
use Magento\Framework\DataObject;

class CreditmemoSender extends \Magento\Sales\Model\Order\Email\Sender\CreditmemoSender
{
    public function send(Creditmemo $creditmemo, $forceSyncMode = false)
    {
        $creditmemo->setSendEmail($this->identityContainer->isEnabled());

        if (!$this->globalConfig->getValue('sales_email/general/async_sending') || $forceSyncMode) {
            $order = $creditmemo->getOrder();
            $this->identityContainer->setStore($order->getStore());

            $transport = [
                'order' => $order,
                'order_id' => $order->getId(),
                'creditmemo' => $creditmemo,
                'creditmemo_id' => $creditmemo->getId(),
                'comment' => $creditmemo->getCustomerNoteNotify() ? $creditmemo->getCustomerNote() : '',
                'billing' => $order->getBillingAddress(),
                'payment_html' => $this->getPaymentHtml($order),
                'store' => $order->getStore(),
                'formattedShippingAddress' => $this->getFormattedShippingAddress($order),
                'formattedBillingAddress' => $this->getFormattedBillingAddress($order),
                'order_data' => [
                    'customer_name' => $order->getCustomerName(),
                    'is_not_virtual' => $order->getIsNotVirtual(),
                    'email_customer_note' => $order->getEmailCustomerNote(),
                    'frontend_status_label' => $order->getFrontendStatusLabel()
                ]
            ];
            $transportObject = new DataObject($transport);

            /**
             * Event argument `transport` is @deprecated. Use `transportObject` instead.
             */
            $this->eventManager->dispatch(
                'email_creditmemo_set_template_vars_before',
                ['sender' => $this, 'transport' => $transportObject->getData(), 'transportObject' => $transportObject]
            );

            $this->templateContainer->setTemplateVars($transportObject->getData());

            if ($this->checkAndSend($order)) {
                $creditmemo->setEmailSent(true);
                $this->creditmemoResource->saveAttribute($creditmemo, ['send_email', 'email_sent']);
                return true;
            }
        } else {
            $creditmemo->setEmailSent(null);
            $this->creditmemoResource->saveAttribute($creditmemo, 'email_sent');
        }

        $this->creditmemoResource->saveAttribute($creditmemo, 'send_email');

        return false;
    }
}

Please write die("testing); in the beginning of send() function to check this overridden file is working or not for you.

2
  • Can you please approve and upvote this?
    – ZealousWeb
    Commented Mar 10, 2021 at 9:13
  • @zealouseWeb thanks this helped me
    – Mujahidh
    Commented Aug 29, 2022 at 7:39

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.