TransWikia.com

Get data from orders with observers

Magento Asked on December 14, 2021

I am with magento 2.3.4,
and I want to capture the information:

product

order

client

I would also like to know how I can print the data on the console.
I am using the observer when ordering
checkout_onepage_controller_success_action

I am trying to capture the information from the observer but nothing shows me

<?php
namespace NasserIntegracionObserver;
use MagentoFrameworkEventObserverInterface;
class Test implements ObserverInterface{

protected $_logger;
protected $requester;
protected $messageManager;
public function __construct(
    PsrLogLoggerInterface $logger,
     NasserIntegracionHelperRequester $requester,
     MagentoFrameworkMessageManagerInterface $messageManager
)
{
    $this->_logger=$logger;
    $this->requester=$requester;
    $this->messageManager = $messageManager;
}
public function execute(MagentoFrameworkEventObserver $observer){
 $data = $observer->getEvent()->getData();
 $this->_logger->info('Data', $data);
// $this->messageManager->addNotice(__('Hola este'));
 $erpId = $this->requester->createPedido([]);
}}

I hope you can help me

One Answer

If you look at the function MagentoCheckoutControllerOnepageSuccess::execute, you will see the code below:

$this->_eventManager->dispatch(
            'checkout_onepage_controller_success_action',
            [
                'order_ids' => [$session->getLastOrderId()],
                'order' => $session->getLastRealOrder()
            ]
        );

and therefore, you should be able to log 2 things:

$orderIds = $observer->getEvent()->getData('order_ids');
 $this->_logger->info('order_id', print_r($orderIds, true));

$order = $observer->getEvent()->getData('order');
 $this->_logger->info('order', print_r($order->getData(), true));

Now, the second line is a big object, so that may be why you see nothing at the moment.. So, I'd start with order increment id for instance.. or just the data you need

Answered by Herve Tribouilloy on December 14, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP