I created an observer that changes the email subject when I place a new order:
This is the ChangeOrderEmailSubject.php file:
<?php
namespace SalesOrderCustObserver;
use MagentoFrameworkEventObserverInterface;
use MagentoFrameworkEventObserver;
use MagentoSalesModelOrder;
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 MagentoSalesModelOrderEmailContainer $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="SalesOrderCustObserverCustomShippingDescriptionObserver" method="execute" />
<observer name="change_order_email_subject" instance="SalesOrderCustObserverChangeOrderEmailSubject" />
</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 “MagentoSalesModelOrderEmailContainer” is not the correct method to call to change the subject of the new order email?