Question

In Magento 1 Simply I can get the Order information by using Mage::getModel() as well Product also same thing right?

$order_information = Mage::getModel('sales/order')->load($order_id);

as well

$product_information = Mage::getModel('catalog/product')->load($product_id)

In magento 2 I'm fallowing below code but it's not working the website is hanging up.

my code is:

Order event Learning/Custom/etc/events.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Event/etc/events.xsd">
    <event name="sales_order_save_after">
        <observer name="custom_event" instance="Learning\Custom\Model\Observer" method="getOrder" />
    </event>
</config>

Product event Learning/Custom/etc/adminhtml/events.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/Event/etc/events.xsd">
    <event name="catalog_product_save_commit_after">
        <observer name="custom_event" instance="Learning\Custom\Model\Observer" method="getProduct" />
    </event>
</config>

Observer Code from Learning\Custom\Model\Observer.php file

<?php

namespace Learning\Custom\Model;

class Observer{

   protected $orderFactory;

   protected $productFactory;

    public function __construct( \Magento\Sales\Model\OrderFactory $orderFactory,\Magento\Catalog\Model\ProductFactory $productFactory)
    {
        $this->orderFactory = $orderFactory;
        $this->productFactory = $productFactory;
    }

    /**
     * Get the Order Information
     *
     * @param \Magento\Framework\Event\Observer $observer
     */
    public function getOrder(\Magento\Framework\Event\Observer $observer)
    {

        /* this 4 lines of code working fine */
        $event = $observer->getEvent();
        $order = $event->getOrder();
        $order_id = $order->getIncrementId();

        file_put_contents('Order ID.txt',print_r($order_id,true));

        /* when this 3 lines of code executing website is hanging */
        $order_factory = $this->orderFactory->create();
        $order_information = $order_factory->loadByIncrementId($order_id);

        file_put_contents('Order.txt',print_r($order_information,true));

        /* when this line of code executing website is hanging */
        file_put_contents('Order Information.txt',\Zend_Debug::dump($order->getData(), $label = null, $echo = true));

    }

    /**
     * Get the Product Information
     *
     * @param \Magento\Framework\Event\Observer $observer
     */
    public function getProduct(\Magento\Framework\Event\Observer $observer)
    {

        /* this 2 lines of code working fine */
        $product_id = $observer->getProduct()->getId();

        file_put_contents('Product ID.txt',print_r($product_id,true));

        /* when this 3 lines of code executing website is hanging */
        $product_information = $this->productFactory->create();

        $prod = $product_information->load($product_id);

        file_put_contents('Product.txt',print_r($prod,true));

    }

}

Please let me know where I went wrong? or Any suggestions?

Was it helpful?

Solution

Check following code. Should replace following code to your Observer class. Find /varlog/system.log file for log

namespace Learning\Custom\Model;

use Psr\Log\LoggerInterface as Logger;

class Observer
{
    /**
     * Logger.
     *
     * @var Logger
     */
    protected $logger;

    public function __construct(
        Logger $logger
    ) {
        $this->logger = $logger;
    }

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

        $order = $observer->getOrder();
        $order_id = $order->getIncrementId();
        $this->logger->info($order_id);

        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $order = $objectManager->get('Magento\Sales\Model\Order');
        $order_information = $order->loadByIncrementId($order_id);
        $this->logger->info('Order Info', $order_information->getData());
    }

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

        $product_id = $observer->getProduct()->getId();
        $this->logger->info($product_id);
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $product = $objectManager->get('Magento\Catalog\Model\Product')->load($product_id);
        $this->logger->info('Product Info', $product->getData());

    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top