0

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?

1 Answer 1

0

You can solve this by as i tested it on my environment works fine

magento/module-sales/Model/Order/Email/Sender/OrderSender.php

in prepareTemplate

protected function prepareTemplate(Order $order)
    {
        $orderNumber = $order->getIncrementId();
        $customValue = 'ssss';
        
        $emailSubject = "Order N. " . $orderNumber;
        
        if ($customValue != "") {
            $emailSubject .= " Riordino N. " . $customValue;
        }

        
        $transport = [
            'order' => $order,
            'order_id' => $order->getId(),
            'billing' => $order->getBillingAddress(),
            'payment_html' => $this->getPaymentHtml($order),
            'store' => $order->getStore(),
            'formattedShippingAddress' => $this->getFormattedShippingAddress($order),
            'formattedBillingAddress' => $this->getFormattedBillingAddress($order),
            'created_at_formatted' => $order->getCreatedAtFormatted(2),
            'order_data' => [
                'customer_name' => $order->getCustomerName(),
                'is_not_virtual' => $order->getIsNotVirtual(),
                'email_customer_note' => $order->getEmailCustomerNote(),
                'frontend_status_label' => $order->getFrontendStatusLabel()
            ],
            'subjects' => $emailSubject  // Add the email subject to the transport array
        ];
        if (empty($customValue)) {
            unset($transport['subjects']);
        }
        $transportObject = new DataObject($transport);

        /**
         * Event argument `transport` is @deprecated. Use `transportObject` instead.
         */
        $this->eventManager->dispatch(
            'email_order_set_template_vars_before',
            ['sender' => $this, 'transport' => $transportObject, 'transportObject' => $transportObject]
        );

        $this->templateContainer->setTemplateVars($transportObject->getData());

        parent::prepareTemplate($order);
    }

and in order email from backend the template you are using use

enter image description here

1
  • @Jackom you tried that ?
    – Kareem k
    Commented May 24 at 4:10

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.