How to override below file in module ?
Magento\Sales\Model\Order\Email\Sender\CreditmemoSender.php
How to override below file in module ?
Magento\Sales\Model\Order\Email\Sender\CreditmemoSender.php
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.