I created an observer that changes the email subject when I place a new order: This is the ChangeOrderEmailSubject.php file:
<?php
namespace Sales\OrderCust\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer;
use Magento\Sales\Model\Order;
class ChangeOrderEmailSubject implements ObserverInterface
{
public function execute(Observer $observer)
{
/** @var Order $order */
$order = $observer->getEvent()->getOrder();
$orderNumber = $order->getIncrementId();
$customValue = $order->getIdreorder();
$emailSubject = __("Order N.") . $orderNumber;
if ($customValue != "") {
$emailSubject .= " Riordino N.".$customValue;
}
/** @var \Magento\Sales\Model\Order\Email\Container $emailContainer */
$emailContainer = $observer->getEvent()->getContainer();
$emailContainer->setSubject($emailSubject);
}
}
Instead, this is the events.xml file:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="sales_order_place_after">
<observer name="custom_shipping_description" instance="Sales\OrderCust\Observer\CustomShippingDescriptionObserver" method="execute" />
<observer name="change_order_email_subject" instance="Sales\OrderCust\Observer\ChangeOrderEmailSubject" />
</event>
</config>
Unfortunately when I try to execute the new order this is the error code I get “main.CRITICAL: Error: Call to a member function setSubject() on null in ”. How can I solve this problem? Maybe “\Magento\Sales\Model\Order\Email\Container” is not the correct method to call to change the subject of the new order email?