0

I know how override the normal class but now i try to override the abstract class [ Magento\Sales\Model\Order\Email\Sender.php ] . i have override the Sender.php file in custom module but its not working. Please suggest what can i do to override the file in custom module. Could anyone suggest regarding this so i can proceed forward.

Core File

 /**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
 namespace Magento\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
 *
 * phpcs:disable Magento2.Classes.AbstractApi
 * @api
 * @since 100.0.2
 */
 abstract class 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;

/**
 * @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,
    \Psr\Log\LoggerInterface $logger,
    Renderer $addressRenderer
) {
    $this->templateContainer = $templateContainer;
    $this->identityContainer = $identityContainer;
    $this->senderBuilderFactory = $senderBuilderFactory;
    $this->logger = $logger;
    $this->addressRenderer = $addressRenderer;
}

/**
 * Send order email if it is enabled in configuration.
 *
 * @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();
    } catch (\Exception $e) {
        $this->logger->error($e->getMessage());
        return false;
    }
    if ($this->identityContainer->getCopyMethod() == 'copy') {
        try {
            $sender->sendCopyTo();
        } catch (\Exception $e) {
            $this->logger->error($e->getMessage());
        }
    }
    return true;
}

/**
 * Populate order email template with customer information.
 *
 * @param Order $order
 * @return void
 */
protected function prepareTemplate(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($templateId);
}

/**
 * Create Sender object using appropriate template and identity.
 *
 * @return Sender
 */
protected function getSender()
{
    return $this->senderBuilderFactory->create(
        [
            'templateContainer' => $this->templateContainer,
            'identityContainer' => $this->identityContainer,
        ]
    );
}

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

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

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

Override in custom module

<?php
declare(strict_types=1);

namespace Matrid\OrderConfirmation\Rewrite\Magento\Sales\Model\Order\Email;

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

class 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;

    /**
     * @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,
        \Psr\Log\LoggerInterface $logger,
        Renderer $addressRenderer
    ) {
        $this->templateContainer = $templateContainer;
    $this->identityContainer = $identityContainer;
    $this->senderBuilderFactory = $senderBuilderFactory;
    $this->logger = $logger;
    $this->addressRenderer = $addressRenderer;
}
protected function checkAndSendCustom(Order $order)
{
    $this->identityContainer->setStore($order->getStore());
    if (!$this->identityContainer->isEnabled()) {
        return false;
    }
    $this->prepareTemplate($order);

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

    # send the value in sendcustom function @var SenderBuilder
    try {
        $sender->sendCustom();
    } catch (\Exception $e) {
        $this->logger->error($e->getMessage());
        return false;
    }
    # copy method you to show the method you have select by the magento admin
    # if you have select the separate method then bcc change to copy
    if ($this->identityContainer->getCopyMethod() == 'bcc') {
        try {
            $sender->sendCopyToCustom();
        } catch (\Exception $e) {
            $this->logger->error($e->getMessage());
        }
    }
    return true;
}
 protected function prepareTemplate(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($templateId);
}

/**
 * Create Sender object using appropriate template and identity.
 *
 * @return Sender
 */
protected function getSender()
{
    return $this->senderBuilderFactory->create(
        [
            'templateContainer' => $this->templateContainer,
            'identityContainer' => $this->identityContainer,
        ]
    );
}

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

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

/**
 * Render billing address into html.
 *
 * @param Order $order
 * @return string|null
 */
protected function getFormattedBillingAddress($order)
{
    return $this->addressRenderer->format($order->getBillingAddress(), 'html');
}
1
  • Please correct the question and use the correct code format, it doesn't look pretty
    – Jtidd
    Commented Mar 29, 2023 at 7:08

2 Answers 2

0

To override the Magento\Sales\Model\Order\Email\Sender class in Magento, you can create a new module and follow these steps:

  • Create a new module: Create a new module under app/code directory. For example, if you want to name the module MyCompany_CustomSalesEmail, create a directory app/code/MyCompany/CustomSalesEmail.

  • Create a new class: In your module's directory, create a new class that extends the Magento\Sales\Model\Order\Email\Sender class. For example, create a new file app/code/MyCompany/CustomSalesEmail/Model/Order/Email/Sender.php

  • Update the di.xml file: Create a new di.xml file in your module's directory app/code/MyCompany/CustomSalesEmail/etc/

  • Run:

    php bin/magento module:enable MyCompany_CustomSalesEmail

1
  • but it is abstract class its not normal class any suggest please help Commented Mar 29, 2023 at 7:48
0

I suppose that you could try to define an around plugin on the Magento\Sales\Model\Order\Email\Sender method you want to change.

Or if you want to override concrete class which is inherited from the Magento\Sales\Model\Order\Email\Sender you may create a preference on it.

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.