0

I'm use following code to catch order information in this Observer : "sales_order_save_after"

public function execute(\Magento\Framework\Event\Observer $observer){

 $orderData = $observer->getEvent()->getOrder()->getData();
 $orderId = $orderData['increment_id'];
 $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
 $order = $objectManager->create('Magento\Sales\Model\Order')->loadByIncrementId($orderId);
 $orderItems = $order->getItems();       

 foreach ($orderItems as $item) {

     error_log(print_r($item->getData(), true));

 }


}

When I place order as guest customer it logs information of all items, But when I Login as a customer and placing order it doesn't log anything because "items" Array is empty.

How can I get item information from order of customer that is logged-in?

1 Answer 1

3

I don't think it's related to customer being logged in or not, but I think order items are not persisted yet, as you're retrieving new order object. Also you should never use objectManager directly.

Try updating your code to this:

public function execute(\Magento\Framework\Event\Observer $observer){

 $order = $observer->getEvent()->getOrder();
 $orderItems = $order->getItems();       

 foreach ($orderItems as $item) {
     error_log(print_r($item->getData(), true));
 }
}

Observer already has order object

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.