I want to override one of the function in Magento core model which is located in
vendor\magento\module-sales\Model\Order\Email\Sender\OrderSender.php
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Sales\Model\Order\Email\Sender;
use Magento\Payment\Helper\Data as PaymentHelper;
use Magento\Sales\Model\Order;
use Magento\Sales\Model\Order\Email\Container\OrderIdentity;
use Magento\Sales\Model\Order\Email\Container\Template;
use Magento\Sales\Model\Order\Email\Sender;
use Magento\Sales\Model\ResourceModel\Order as OrderResource;
use Magento\Sales\Model\Order\Address\Renderer;
use Magento\Framework\Event\ManagerInterface;
/**
* Class OrderSender
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class OrderSender extends Sender
{
protected function prepareTemplate(Order $order)
{
$transport = [
'order' => $order,
'billing' => $order->getBillingAddress(),
'payment_html' => $this->getPaymentHtml($order),
'store' => $order->getStore(),
'formattedShippingAddress' => $this->getFormattedShippingAddress($order),
'formattedBillingAddress' => $this->getFormattedBillingAddress($order),
];
$transport = new \Magento\Framework\DataObject($transport);
$this->eventManager->dispatch(
'email_order_set_template_vars_before',
['sender' => $this, 'transport' => $transport]
);
$this->templateContainer->setTemplateVars($transport->getData());
parent::prepareTemplate($order);
}
}
I just want to add additional data to $transport array variable from my custom extension, but it seems not working because when I call the additional data it didn't print anything, here's what i've done:
etc/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="Namespace\Module\Model\Order\Email\Sender\OrderSender"/>
</config>
Namespace\Module\Model\Order\Email\Sender\OrderSender.php
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Namespace\Module\Model\Order\Email\Sender;
use Magento\Payment\Helper\Data as PaymentHelper;
use Magento\Sales\Model\Order;
use Magento\Sales\Model\Order\Email\Container\OrderIdentity;
use Magento\Sales\Model\Order\Email\Container\Template;
use Magento\Sales\Model\Order\Email\Sender;
use Magento\Sales\Model\ResourceModel\Order as OrderResource;
use Magento\Sales\Model\Order\Address\Renderer;
use Magento\Framework\Event\ManagerInterface;
/**
* Class OrderSender
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class OrderSender extends \Magento\Sales\Model\Order\Email\Sender\OrderSender
{
public function prepareTemplate(Order $order)
{
$transport = [
'order' => $order,
'billing' => $order->getBillingAddress(),
'payment_html' => $this->getPaymentHtml($order),
'test' => '<p> Yoo Yeezy </p>',
'store' => $order->getStore(),
'formattedShippingAddress' => $this->getFormattedShippingAddress($order),
'formattedBillingAddress' => $this->getFormattedBillingAddress($order),
];
$transport = new \Magento\Framework\DataObject($transport);
$this->eventManager->dispatch(
'email_order_set_template_vars_before',
['sender' => $this, 'transport' => $transport]
);
$this->templateContainer->setTemplateVars($transport->getData());
parent::prepareTemplate($order);
}
}
module.xml
code<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="Namespace_Module" setup_version="1.0.0"> <sequence> <module name="Magento_Sales"/> </sequence> </module> </config>
& then runphp bin/magento setup:upgrade
,php bin/magento setup:di:compile
,php bin/magento setup:static-content:deploy
,php bin/magento indexer:reindex
,php bin/magento cache:flush
[InvalidArgumentException] There are no commands defined in the "setup" namespace.