My Observer Class

    namespace GCV\GenerateCSV\Observer;
    use Magento\Framework\Event\ObserverInterface;
    class GenerateCSV implements ObserverInterface
     protected $_objectManager;
     private $logger;
     private $productFactory;
      public function __construct(
        \Magento\Framework\ObjectManagerInterface $objectManager,
        \Psr\Log\LoggerInterface $logger,
        \Magento\Catalog\Model\ProductFactory $productFactory
      ) {
          $this->_objectManager = $objectManager;
          $this->logger = $logger;
          $this->productFactory = $productFactory;
      public function execute(\Magento\Framework\Event\Observer $observer)
        $order = $observer->getEvent()->getOrder();
        $order_id = $order->getIncrementId();
        $this->logger->info("observer working"); 
        $productIds = array();
        $product_qty = array();
        $product_price = array();
        $i = 0;
        foreach ($order->getAllItems() as $item)
        $productIds[] = $item->getProductId();
        $product_qty[] = $item->getQtyOrdered();
        $product_price[] = $item->getPrice();


<?xml version='1.0'?>
<config xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:noNamespaceSchemaLocation='urn:magento:framework/Event/etc/events.xsd'>
   <event name='sales_order_place_after'>
  • Please add more details to your question, such as which observer you're using, what solutions have you tried, and what output are you getting on the current posted code. Mentioning small details will be more helpful to understand the issue and provide a appropriate solution
    – Mohit Rane
    Commented May 19, 2021 at 10:01
  • @mohit Rane: I am using the sales_order_place_after event. And with the above Observer class, I am not able to get the product price inside the foreach statement. However, I am getting product id and quantity in the log file. Commented May 19, 2021 at 10:23

1 Answer 1


try below solution


<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="sales_order_place_after">
    <observer name="vendor_sales_order_place_after" instance="Vendor\Extensoin\Observer\OrderObserver" />

After that, you need to create Observer file at following location.


namespace Vendor\Extension\Observer;
use Magento\Framework\Event\ObserverInterface;
class OrderObserver implements ObserverInterface
  public function execute(\Magento\Framework\Event\Observer $observer)
        $statuscode = $observer->getEvent()->getOrder()->getStatus();
        $statuslabel = $observer->getEvent()->getOrder()->getStatusLabel();
        $products= $observer->getEvent()->getOrder()->getAllItems()

if works for you then please accept as a solution

  • what are all available in $products object? Can you be more specific? I've tried to dump the $products variable but it is giving me a timeout error. Commented May 20, 2021 at 17:35
  • What exactly u need..u can use foreach ($products as $item) { print_r($item->getData()); } Commented May 20, 2021 at 17:59
  • foreach ($products as $item) { print_r($item->getData()); } I've tried this already In fact I tried var_dump() also. The problem when I try to print this data in log or checkout page, my RAM is getting full or I am visually getting an endless loop in checkout page. Commented May 23, 2021 at 11:31
  • Finally I am able to print the item object using $this->logger->debug("webarro", $item->getData()); but I dont know how to extract the price, id and quantity ordered. I have tried $this->logger->debug('ID:', $item->getData('product_id')); $this->logger->debug('Quantity: ', $item->getData('qty_ordered')); $this->logger->debug('Price: ', $item->getData('price')); but its not working! Commented May 23, 2021 at 14:15
  • Whats is the output of $this->logger->debug($item->getData()); ?? Commented May 23, 2021 at 18:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.