1

I have created a sales_order_save_after event observer. i will show the code below

SalesOrderSaveAfter.php

public function execute(\Magento\Framework\Event\Observer $observer)
{   
    //get the order object
    $order = $observer->getEvent()->getOrder();
    if ($order instanceof \Magento\Framework\Model\AbstractModel) 
    {   
        //get status
        $new_status =   $order->getStatus();
        //check the new status is confirm or canceled

        if ($new_status == 'confirm' || $new_status == 'canceled') 
        {    
            //parameters = array()
            $params         =   array();
            $order_id       =   $order->getId();
            $customer_id    =   $order->getCustomerId();

            if( !empty($order_id) ) {
                $product_ids = array();
                foreach( $order->getAllVisibleItems() as $item ) {
                    $product_ids[] =    $item->getProductId();
                    $product       =    $item->getData();
                    //$nrv         =    $item->getProduct()->getAttributeText('nrv');

                }        
            }


            //$nrv = Mage::getModel('catalog/product')->load($product_ids)->getAttributeText('nrv');

            $params     =   array(
                'order_id'      =>  $order_id,
                'customer_id'   =>  $customer_id,
                'product_id'    =>  $product_ids,
                'status'        =>  $new_status,
                'product'       =>  $product, 
                //'nrv'         =>  $nrv,
            ); 
        }

        //call the curl
        $this->curlCall($params);

    }
    return $this;
}

Everything is working correctly. Am getting All the information except custom attribute. I added custom attribute nrv to each of the product. but i didn't get value of custom attribute. How can I get the custom attribute value in observer?. Thanks in advance for your valuable answer.

  • nrv is custom option? – Dhiren Vasoya Jan 20 '18 at 11:18
  • @DhirenVasoya Yes It was a custom attribute of the product. I tried with Object manager and got the value. Is there any better method other than object manager? – Midlaj Jan 20 '18 at 11:24
  • I post answer, please check. I will give value of selected custom option with product. – Dhiren Vasoya Jan 20 '18 at 11:35
  • Try this code: $product = $item->getProduct(); $nrvAttribute = $product->getResource()->getAttribute('nrv'); echo $nrvAttribute->getFrontend()->getValue($product); – Abdul Jan 20 '18 at 12:17
4

Try following code:

  1. Attribute without custom options
$value = $item->getProduct()->getData('attribute_code');
  1. Attribute with custom options
$optionId = $item->getProduct()->getData('attribute_code');

$optionValue = $item->getProduct()->getAttributeText('attribute_code');

OR

$optionValue = $item->getProduct()->getResource()->getAttribute('attribute_code')->getFrontend()->getValue($item->getProduct());

Check output in log file using following code:

$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/test.log');
$logger = new \Zend\Log\Logger();
$logger->addWriter($writer);
$logger->info('Option Value: ', $optionValue);
1

You can get custom option value of product using this code in observer,

$order = $observer->getEvent()->getOrder();

foreach($order->getAllVisibleItems() as $item ) {
    $options = $item->getProduct()->getTypeInstance(true)->getOrderOptions($item->getProduct());

    if(array_key_exists("attributes_info",$options) && count($options['attributes_info']) >= 1) :

       // THis code give all the custom attribute value
       foreach($options['attributes_info'] as $myopt)
       {
            echo $myopt['label']." : ".$myopt['value']; 
       }

     endif;
}
1

Try by Factory Method:

protected $request;
protected $productFactory;  

public function __construct(
    \Magento\Framework\App\RequestInterface $request,
    \Magento\Catalog\Model\ProductFactory $productFactory,

) { 
    $this->request = $request;
    $this->productFactory = $productFactory;


/**
 *
 * @param Observer $observer
 * @return void
 */
public function execute(Observer $observer)
{
    $order = $observer->getEvent()->getOrder(); 
    foreach( $order->getAllVisibleItems() as $item ) {
        $product = $this->productFactory->create()
            ->load($item->getProductId());
        echo $product->getNrv();
    } 
}

After run compile command:

php bin/magento setup:di:compile
0
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
                    $product_data = $objectManager->create('Magento\Catalog\Model\Product')->load($product_ids);
                    $nrv = $product_data->getData('nrv');

Atlast i got the answer by using object manager.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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