Magento 2

How to Get a value of a Custom attribute of Product in Magento 2 Observer

Considering the number of CMS available for eCommerce, Magento is popular & powerful choices from small to large scale shops. Due to its unique features & flexible architecture helps the developer define their own events & observers by extending Native core functionality to fulfill store owner’s requirement.

Basically, these observers are also known as “listener”, that helps you to detect specific events. And when an event takes place, it will perform an action defined by you. Compared to Magento 1, Magento 2 comes with the pretty well-integrated observer system that allows you to run your custom code in response to a specific Magento event or your custom event.

Basically, we want to perform a specific action, once a customer places an order. Here is an example where I have sales_order_save_after event observer.

To do this, you just need to add event observer code in your Observer file.

public function execute(\Magento\Framework\Event\Observer $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;
}

}

You can play with this code and create your event & observer as per your requirement of adding one of more actions according to your needs.

Lastly, Comment down below if you face any issue while using this code.
Happy Coding!

Click to rate this post!
[Total: 5 Average: 3.4]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

Recent Posts

How to Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

1 day ago

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

4 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

4 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

5 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

5 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

1 week ago