0

want to print order details and product details (variants) which are ordered.

My xml file

<?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_save_after">
        <observer name="recipes_sales_order_save_after" 
         instance="Vendorname\Modulename\Observer\Observer" />
    </event>

    </config>

My observer file

<?php namespace Vendorname\Modulename\Observer; 
        use Magento\Framework\Event\ObserverInterface; 
             public function __construct(
            \Magento\Sales\Model\Order $order
        )
        {
            $this->order = $order;
        }
           public function execute(\Magento\Framework\Event\Observer 
          $observer)
         {
            $order = $observer->getEvent()->getOrder();
           $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/nitesh.log');
            $logger = new \Zend\Log\Logger();
            $logger->addWriter($writer);
           $logger->info($order->getOrderId());


         foreach( $order->getAllVisibleItems() as $item ) {

         $logger->info( $item->getHana());  //" HANA is new attribute name"
          $logger->info( $item->getProductId());
         } 

but getHana not print

enter image description here

2
  • have you check getQty, getPrice and getHana values in your database? Commented Dec 1, 2018 at 11:27
  • Please check my answer. Commented Dec 4, 2018 at 10:14

2 Answers 2

1

You need to load product and then get attribute value using below code. Add that code in your execute() function :

<?php

namespace Vendorname\Modulename\Observer; 

use Magento\Framework\Event\ObserverInterface; 

protected $order;
protected $_productFactory;
public function __construct(
  \Magento\Sales\Model\Order $order,
  \Magento\Catalog\Model\ProductFactory $productFactory
)
{
  $this->order = $order;
  $this->_productFactory = $productFactory;
}

public function execute(\Magento\Framework\Event\Observer $observer) {
    $order = $observer->getEvent()->getOrder();
    $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/nitesh.log');
    $logger = new \Zend\Log\Logger();
    $logger->addWriter($writer);
    $logger->info($order->getOrderId());
    $product = $this->_productFactory->create();
    foreach($order->getAllVisibleItems() as $item ) {
      $productColl = $product->load($item->getProductId());
      $logger->info($productColl->getData("Hana"));
    }
}

Remove generation or generated folder and clean cache.

4
  • not working get empty details $logger->info('hana===>'.$productColl->getData("Hana")); 2018-12-04T10:41:14+00:00 INFO (6): hana===> Commented Dec 4, 2018 at 10:44
  • I think Hana is not attribute_code it's attribute label. You need to add attribute_code in getData() function Commented Dec 4, 2018 at 10:54
  • how to set attribute_code in getData() function magento . Rohan Hapani Commented Dec 4, 2018 at 11:09
  • Go to admin -> store -> Attributes -> Product. Now, write Hana in Default Label and search. Then, get value of attribute_code column and add into getData() Commented Dec 4, 2018 at 11:14
0

To get Order Item Qty

Use below code

$item->getQtyOrdered();

Instead of

$item->getQty();
1
  • want to print $logger->info( $item->getHana()); //" HANA is new attribute name in a variants product" Commented Dec 4, 2018 at 8:57

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.