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!