Magento 2 Checkout Observer Debugging – Step-by-Step Guide

checkoutevent-observermagento2

I'm trying to create a Module that gets the order data when an order is placed and sends it to a third party using FTP.

I've created an Observer that gets called by the sales_order_place_after event.

My Observer code:

<?php

use Magento\Framework\Event\ObserverInterface;

class Orderobserver implements ObserverInterface
{
    public function __construct(
        \Magento\Quote\Model\QuoteFactory $quoteFactory,
        \Magento\Sales\Model\Order $order,
        \Digitaq\CbFashion\Helper\Data $helper
    )
    {
        $this->_order = $order;
        $this->_helper = $helper;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $order = $observer->getEvent()->getOrder();
        $this->_helper->writeShp($order);
    }
}

The observer gets called succesfully and the writeShp method also does, but it's not possible for me to dump the $order variable, as that leads to a redirect to the cart without the order being fulfilled. How can I see this variable so I know what data I can pull?

Best Answer

Try to log your data by using this way:

protected $logger;
public function __construct(\Psr\Log\LoggerInterface $logger)
{
    $this->logger = $logger;
}

You use debug, exception, system for PSR Logger for example:

$this->logger->info($yourCustomData);
$this->logger->debug($yourCustomData);

Hopefully, this will help you.

Related Topic