cancel
Showing results for 
Search instead for 
Did you mean: 

Overriding OrderSender doesn't work and i can't figure out why

Overriding OrderSender doesn't work and i can't figure out why

Hey guys,

 

i'm currently trying to override "\Magento\Sales\Model\Order\Email\Sender\OrderSender" (my plan is to provide different custom email-templates for every payment-method) which unfortunately doesn't seem to work.

 

I created a module for that with the following structure:

 

 

Vendor
-- /OrderMailByPaymentMethod
-- -- /etc
-- -- -- module.xml
-- -- -- /frontend
-- -- -- -- di.xml
-- -- /Model
-- -- -- /Email
-- -- -- -- /Sender
-- -- -- -- -- OrderSender.php
-- -- registration.php

 

 

and the following content:

 

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\OrderSender" type="Vendor\OrderMailByPaymentMethod\Model\Email\Sender\OrderSender"/>
</config>

 

 

module.xml:

 

 

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Vendor_OrderMailByPaymentMethod" setup_version="1.0.1">
    </module>
</config>

 

 

OrderSender.php:

 

 

<?php
namespace Vendor\OrderMailByPaymentMethod\Model\Email\Sender;

use Magento\Sales\Model\Order;

class OrderSender extends \Magento\Sales\Model\Order\Email\Sender\OrderSender
{
    protected function prepareTemplate(Order $order)
    {
        //Get Payment Method
        $paymentMethod = $order->getPayment()->getMethod();

        die("TEST".$paymentMethod."TEST");

        parent::prepareTemplate($order);


        //Define email template for each payment method
        switch ($paymentMethod) {
            case 'banktransfer' :
                $templateId = 'custom_template_cod';
                break;
            // Add cases if you have more payment methods
            default:
                $templateId = $order->getCustomerIsGuest() ? $this->identityContainer->getGuestTemplateId() : $this->identityContainer->getTemplateId();

        }

        $this->templateContainer->setTemplateId($templateId);
    }
}

 

 

and finally registration.php:

 

 

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Vendor_OrderMailByPaymentMethod',
    __DIR__
);

 

 

The module installs correctly but unfortunately overriding the class doesn't work. I also tried the following commands:

 

php bin/magento setup:upgrade
php bin/magento setup:di:compile
php bin/magento cache:clean
php bin/magento cache:flush

Any idea why this happens?

 

Best regards

Johannes

1 REPLY 1

Re: Overriding OrderSender doesn't work and i can't figure out why

Hello Farbenkollektiv


Please override this SenderBuilder class.

 

<preference for="Magento\Sales\Model\Order\Email\SenderBuilder" type="Magebull\Orderemail\Model\Mail\SenderBuilder"/>
<preference for="Magento\Sales\Model\Order\Email\Container\Template" type="Magebull\Orderemail\Model\Mail\Container\Template"/>
<preference for="Magento\Sales\Model\Order\Email\Sender\OrderSender" type="Magebull\Orderemail\Model\Mail\Sender\OrderSender"/>

 

Magebull\Orderemail\Model\Mail\Container\Template

 

 

Magebull\Orderemail\Model\Mail\Container\Template

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace vendor\moduleName\Model\Mail\Container;

class Template extends \Magento\Sales\Model\Order\Email\Container\Template
{
    /**
     * @var array
     */
    protected $templateId;

    /**
     * @var array
     */
    protected $imageAttach;

    public function setTemplateid($tempID)
    {
        $this->templateId = $tempID;
    }


    public function getTemplateid()
    {
        return $this->templateId;
    }


}

Magebull\Orderemail\Model\Mail\Sender\OrderSender

 

 

Magebull\Orderemail\Model\Mail\SenderBuilder

 

 

<?php
/**
 * @author     Kristof Ringleff
 * @package    Fooman_EmailAttachments
 * @copyright  Copyright (c) 2015 Fooman Limited (http://www.fooman.co.nz)
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
namespace Magebull\Orderemail\Model\Mail\Sender;


class OrderSender extends \Magento\Sales\Model\Order\Email\Sender\OrderSender
{
    /**
     * @var \Fooman\EmailAttachments\Model\AttachmentContainerInterface
     */
    protected $templateContainer;
     protected $_designerhelper;

    public function __construct(
        \Magento\Sales\Model\Order\Email\Container\Template $templateContainer,
        \Magento\Sales\Model\Order\Email\Container\OrderIdentity $identityContainer,
        \Magento\Sales\Model\Order\Email\SenderBuilderFactory $senderBuilderFactory,
        \Psr\Log\LoggerInterface $logger,
        \Magento\Sales\Model\Order\Address\Renderer $addressRenderer,
        \Magento\Payment\Helper\Data $paymentHelper,
        \Magento\Sales\Model\ResourceModel\Order $orderResource,
        \Magento\Framework\App\Config\ScopeConfigInterface $globalConfig,
        \Magento\Framework\Event\ManagerInterface $eventManager
    ) {
        $this->templateContainer = $templateContainer;

        parent::__construct(
            $this->templateContainer,
            $identityContainer,
            $senderBuilderFactory,
            $logger,
            $addressRenderer,
            $paymentHelper,
            $orderResource,
            $globalConfig,
            $eventManager
        );
      /*  $this->attachmentContainer = $attachmentContainer;*/
    }

    public function send(\Magento\Sales\Model\Order $order, $forceSyncMode = false)
    {
        $items = $order->getAllVisibleItems();
        $IncrementId = $order->getIncrementId();
       

        $paymentMethod = $order->getPayment()->getMethod();

    

        //Define email template for each payment method
        switch ($paymentMethod) {
            case 'banktransfer' :
                $templateId = 'custom_template_cod';
                break;
            // Add cases if you have more payment methods
            default:
                $templateId = $order->getCustomerIsGuest() ? $this->identityContainer->getGuestTemplateId() : $this->identityContainer->getTemplateId();

        }

   		 $this->templateContainer->setTemplateid($templateId);  
      


        return parent::send($order, $forceSyncMode);
    }
}

 

Magebull\Orderemail\Model\Mail\SenderBuilder

 

 

<?php
/**
 * @author     Kristof Ringleff
 * @package    Fooman_EmailAttachments
 * @copyright  Copyright (c) 2015 Fooman Limited (http://www.fooman.co.nz)
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
namespace Magebull\Orderemail\Model\Mail;

use Magento\Framework\Mail\Template\TransportBuilder;
use Magento\Sales\Model\Order\Email\Container\IdentityInterface;
use Magento\Sales\Model\Order\Email\Container\Template;

class SenderBuilder extends \Magento\Sales\Model\Order\Email\SenderBuilder
{
    /**
     * @var Template
     */
    protected $templateContainer;

    /**
     * @var IdentityInterface
     */
    protected $identityContainer;

    /**
     * @var TransportBuilder
     */
    protected $transportBuilder;

    /**
     * @param Template $templateContainer
     * @param IdentityInterface $identityContainer
     * @param TransportBuilder $transportBuilder
     */
    public function __construct(
        Template $templateContainer,
        IdentityInterface $identityContainer,
        TransportBuilder $transportBuilder
    ) {
        $this->templateContainer = $templateContainer;
        $this->identityContainer = $identityContainer;
        $this->transportBuilder = $transportBuilder;
    }

    /**
     * Prepare and send email message
     *
     * @return void
     */
   protected function configureEmailTemplate()
    {
$templateID = $this->templateContainer->getTemplateid();
        $this->transportBuilder->setTemplateIdentifier($templateId);
        $this->transportBuilder->setTemplateOptions($this->templateContainer->getTemplateOptions());
        $this->transportBuilder->setTemplateVars($this->templateContainer->getTemplateVars());
        $this->transportBuilder->setFrom($this->identityContainer->getEmailIdentity());
    } }

Hope it will help to solve the problem then please accept as solution and kudos.