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

  • 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/… – Mohit Patel Jun 19 at 18:33
  • Hi @MohitPatel : I tried it but I have this error : 'undefined property: MyTestSpace\productsPurchased\Observer\Tracker::$_objectManager' – learner Jun 22 at 21:39
  • this error say to you are not load this class – Mohit Patel Jun 23 at 5:21
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...
}
| improve this answer | |
  • 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 : – learner Jun 22 at 21:23
  • Hi @Pankaj : I tried it butI can not find : here is response : **OrderTrackerLogger.INFO: No such entity with orderId = [] ** – learner Jun 22 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 ?

| improve this answer | |

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.