How to Get Product Options in Magento 2
Magento 2 supports the functionality for creating and assigning the custom options to the products. The process to add new custom product options is simple for any store owner.
Follow the steps given below for getting Product Options in Magento 2:
- Generate Object Manager
1$_objectManager = \Magento\Framework\App\ObjectManager::getInstance(); - Generate the Product
1$product = $_objectManager->get('\Magento\Catalog\Model\Product')->load($id); - Custom Options
1$customOptions = $_objectManager->get('Magento\Catalog\Model\Product\Option')->getProductOptionCollection($product); - Configurable Options
123$productTypeInstance = $_objectManager->get('Magento\ConfigurableProduct\Model\Product\Type\Configurable');$productAttributeOptions = $productTypeInstance->getConfigurableAttributesAsArray($product); - Grouped Childs
1$typeInstance = $_objectManager->get('Magento\GroupedProduct\Model\Product\Type\Grouped');$childs = $typeInstance->getAssociatedProducts($product); - Bundle Selection
12345678$store_id = $_storeManager->getStore()->getId();$options = $_objectManager->get('Magento\Bundle\Model\Option')->getResourceCollection()->setProductIdFilter($product->getId())->setPositionOrder();$options->joinValues($store_id);$typeInstance = $_objectManager->get('Magento\Bundle\Model\Product\Type');$selections = $typeInstance->getSelectionsCollection($typeInstance->getOptionsIds($product), $product);
Following these steps will enable Product Options in your Magento 2 Store.
If you have faced any issue while using this guide, do let us know by commenting below and we will help you out.
Hope to see you in the Next Tutorial. B’bye. ?
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
2 Comments
Thanks very useful. Exactly what I was looking for.
Hello Hardik,
We’re glad to help you.
Thank You