With upgrade to Magento 2.4.4, I’m using checkout_onepage_controller_success_action
to get order information that I insert into a custom table that I created in Magento. We’re allowing customers to enter gift message only in the cart page which means it is allowed at order level but not at the item level.
What is the method to retrieve the Gift message entered in the cart page from the order object that we get in the Observer class subscribed to checkout_onepage_controller_success_action
.
Below is my old code:
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
namespace PerfectmakeupmirrorsOrderObserver;
use PerfectmakeupmirrorsOrderHelperData;
use MagentoGiftMessageApiOrderRepositoryInterface;
use MagentoFrameworkEventObserverInterface;
use MagentoFrameworkEventObserver;
use PsrLogLoggerInterface;
class OrderSaveAfter implements ObserverInterface
{
protected $logger;
protected $helper;
protected $order_gift_repository;
/**
* @desc Construct method which is used to inject dependency of helper class
* @param PsrLogLoggerInterface $logger
* @param PerfectmakeupmirrorsOrderHelperData $helper
* @param MagentoGiftMessageApiOrderRepositoryInterface $order_gift_repository
*/
public function __construct(LoggerInterface $logger, Data $helper, OrderRepositoryInterface $order_gift_repository){
$this->logger = $logger;
$this->helper = $helper;
$this->order_gift_repository = $order_gift_repository;
}
/**
* @desc Observer execute method which gets control from dispatcher of 'sales_order_save_after' event
* @param MagentoFrameworkEventObserver object
*/
public function execute(Observer $observer) {
// 1. Retrieve the Gift Message using dependency injection from the order object.
// 2. Fetch the record from poms_jobs_queue along with the record ID where the status = "-1".
// This order object is obtained only after the event "sales_order_save_after" is dispatched.
$order = $observer->getEvent()->getOrder();
// Retrieving the Gift Message using dependency injection OrderRepositoryInterface
// Ensure to use getId() to get the real order ID.
// Call getMessage() to get the gift message.
// Exception handling deployed here to address the issue with the empty gift message.
// Order with empty gift message raises NoSuchEntityException exception.
// Exception handling is how this situation is dealt with in Magento 2.
$gift_message = ''; // Initialize to blank.
try {
// MagentoGiftMessageApiOrderRepositoryInterface $order_gift_repository
$gift_object = $this->order_gift_repository->get($order->getId());
// This line is what throws an exception in case of an empty gift message.
$gift_message = $gift_object->getMessage();
} catch (NoSuchEntityException $e) {
// Valid scenario of No gift message specified. Nothing to be done.
} catch (Exception $e) {
// Error scenario. Since empty gift message throws this error, logging commented
// $this->logger->debug("Exception: " . $e->getMessage());
}
// Call the helper class method to update the order_info array.
// Pass order object and the gift message as arguments.
try {
$this->helper->add_gift_message_to_order_info($order, $gift_message);
} catch (Exception $e) {
$this->logger->debug("***ERROR*** Exception: " . $e->getMessage());
}
}
}