1

I would get information on all items of product purchased.

I proceeded by this way but I can not have any information on all items of product purchased :

 <?php namespace MyTestSpace\productsPurchased\Observer; use Magento\Framework\Event\Observer; use Magento\Framework\Event\ObserverInterface; use MyTestSpace\productsPurchased\Logger\Logger; class OrderTracker implements ObserverInterface { private $_logger; public function __construct( Logger $logger) { $this->_logger = $logger; } public function execute(Observer $observer) { try { $order = $observer->getEvent()->getOrder(); $orderId = $order->getIncrementId(); $allItems = $order->getAllItems(); $this->_logger->info('OrderId -> '. $orderId); $itemArry = []; foreach($allItems as $item){ $itemArry[]= $item; } $this->_logger->info('OrderAllItem -> ', $itemArry); } catch (\Throwable $th) { $this->_logger->info($th->getMessage()); } } } 

In my log file (OrderTracker.log) I obtient "OrderId" but not 'OrderAllItem'!

Here is an example

[2020-06-19 15:15:40] OrderTrackerLogger.INFO: OrderId -> 000000181 [] [] [2020-06-19 15:15:40] OrderTrackerLogger.INFO: OrderAllItem -> ["[object] (Magento\Sales\Model\Order\Item\Interceptor: {})"] []

Can anyone show me how can we get the OrderAllItem ? Thanks

3
  • you want to Retrieve items on Order ??? If Yes follow this code $order = $this->_objectManager->create('Magento\Sales\Model\Order')->load($orderId); $orderItems = $order->getAllItems(); >> magento.stackexchange.com/questions/96237/… Commented Jun 19, 2020 at 18:33
  • Hi @MohitPatel : I tried it but I have this error : 'undefined property: MyTestSpace\productsPurchased\Observer\Tracker::$_objectManager' Commented Jun 22, 2020 at 21:39
  • this error say to you are not load this class Commented Jun 23, 2020 at 5:21

2 Answers 2

0
__construct(\Magento\Sales\Model\OrderFactory $orderFactory) { $this->orderFactory = $orderFactory } public function execute(Observer $observer) { // Get Order Order id.... non increment.. $order = $this->orderFactory->create(); $orderDetail = $order->load($orderId); $orderDetail->getAllItems() //// code... } 
2
  • Hi, I don't have orderID, I don't know orderId. Why, I do not have this commandId, I explain by more information : When a client buy some article, I must find the information of this order, by the event sales_order_place_after, and I want to sent at the API of our provider. So I tried your code like this : Commented Jun 22, 2020 at 21:23
  • Hi @Pankaj : I tried it butI can not find : here is response : **OrderTrackerLogger.INFO: No such entity with orderId = [] ** Commented Jun 22, 2020 at 21:43
0

So I tried your code like this :

namespace MyTestSpace\productsPurchased\Observer; use Magento\Framework\Event\Observer; use Magento\Framework\Event\ObserverInterface; use MyTestSpace\productsPurchased\Logger\Logger; class OrderTracker implements ObserverInterface { private $_logger; public function __construct( Logger $logger, \Magento\Sales\Model\OrderFactory $orderFactory) { $this->_logger = $logger; $this->orderFactory = $orderFactory; } public function execute(Observer $observer) { try { $order = $observer->getEvent()->getOrder(); $orderId = $order->getIncrementId(); //Argument 2 passed to Monolog\Logger::info() must be of the type array, string given, //$this->_logger->info('OrderID -> ', $orderId); $orderIdArry = [ 'OrderID' => $orderId, ]; $this->_logger->info('OrderID -> ', $orderIdArry); // old one //$allItems = $order->getAllItems(); $orderFactory = $this->orderFactory->create(); $orderDetail = $orderFactory->load($orderId); $allItems = $orderDetail->getAllItems(); $itemArry = []; foreach($allItems as $item){ $itemArry[]= $item; } $this->_logger->info('OrderAllItem -> ', $itemArry); } catch (\Throwable $th) { $this->_logger->info($th->getMessage()); } } } 

I find "orderId' but I have not any information in my following variable : itemArray. here is result :

[2020-06-22 20:58:59] OrderTrackerLogger.INFO: OrderID -> {"OrderID":"000000185"} [] [2020-06-22 20:58:59] OrderTrackerLogger.INFO: No such entity with orderId = [] [] Could you help me ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.