1

I need to create an observer of the vendor/magento/module-sales/Model/Order/Email/Sender/OrderCommentSender.php class, at the send() method (83th line), so that I can append a customized comment in the comments section.

Line 83 starts at $this->eventManager->dispatch

public function send(Order $order, $notify = true, $comment = '') {

$this->identityContainer->setStore($order->getStore());

$transport = [
    'order' => $order,
    'comment' => $comment,
    'billing' => $order->getBillingAddress(),
    'store' => $order->getStore(),
    'formattedShippingAddress' => $this->getFormattedShippingAddress($order),
    'formattedBillingAddress' => $this->getFormattedBillingAddress($order),
    'order_data' => [
        'customer_name' => $order->getCustomerName(),
        'frontend_status_label' => $order->getFrontendStatusLabel()
    ]
];

$transportObject = new DataObject($transport);

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

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

return $this->checkAndSend($order, $notify); }
0

How about you add comment to the $order and in the email, you get it from $order object?

No observer neeed

1
  • Actually I'm not aiming to simply add a comment to the email, but I need to get the comment's content from another table and then automatically add it to the comment section – myname2021 Mar 4 at 14:09
0

I suggest you try these steps:

Step 1: Choose the event that you would create an observer (access class OrderCommentSender search eventManager->dispatch, coppy event name)

Step 2: create events.xml file in folder (ex: //etc/events.xml)

Step 3: insert add observer code in XML file " instance="\\Observer\ClassNameObserver" />

Step 4: create new ClassNameObserver file following this path: //Observer/ClassNameObserver.php

Step 5: create function execute(Observer $observer) class ClassNameObserver implements \Magento\Framework\Event\ObserverInterface

{
    /**
     * @param Observer $observer
     */
    public function execute(Observer $observer)
    {
        //code implement at here
    }
}
1
  • In the doubt that I had, I ended up managing to do it, I just forgot to give the feedback here. Thank you anyway! – myname2021 Mar 5 at 16:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.