- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Get product options value in Magento2
Hi All,
I want to fetch product options value in my code, So anyone can explain me how can i get this as i am unable to fetch these value.
I want these data :
"options": [{
"label": "Candles",
"value": "2",
"print_value": "2"
}, {
"label": "Message",
"value": "Happyyyy",
"print_value": "Happyyyy"
}, {
"label": "Box Type",
"value": "Baby Blue",
"print_value": "Baby Blue"
}, {
"label": "Remarks",
"value": "A",
"print_value": "A"
}]
As you can see this the data is in json format. but i am unable to fetch these values. Can somebody please help.
Ankita Biswas
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Re: Get product options value in Magento2
Can you please tell me where you need to get this data?
In PHP you can use JSON decode method to get values in Associative array form.
Here is the example:
$array = json_decode($response, TRUE);
If you are getting array somewhere else. Let me know.
--
If my answer is useful, please Accept as Solution & give Kudos
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Re: Get product options value in Magento2
Hello @Ankita Biswas ,
You can get value by using below code
foreach($customOptions as $option) { $values = $option->getValues(); if (empty($values)) { //check if option has values (Option can have values only if option type is checkbox, radio, multiselect or drop-down) continue; } foreach($values as $value) { $valueTitle = $value->getTitle(); } }
--
If my answer is useful, please Accept as Solution & give Kudos
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Re: Get product options value in Magento2
Hello @Ankita Biswas
you need to use /V1/products/{sku}/options api for this.
https://www.screencast.com/t/QAD6MxpJhk
also, you can swagger into your Magento installation
let's example your domain name is
test.com then swager url is
check Magento swagger url
https://devdocs.magento.com/swagger/
All api mention into there, you can find appropriate API for same
Problem solved? Click Kudos & Accept as Solution!
Sunil Patel
Magento 2 Certified Professional Developer & Frontend Developer
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Re: Get product options value in Magento2
Hi @PankajS_Magento @gelanivishal @Sunil Patel
I want to fetch value of label, value, print_value from options. So how can i achieve this.
[product_options] => Array ( [info_buyRequest] => Array ( [qty] => 1.0000 [use_discount] => 1 [action] => [configured] => 1 [options] => Array ( [1] => 1 [2] => 4 ) [reset_count] => 1 ) [options] => Array ( [0] => Array ( [label] => Core Size [value] => Size M [print_value] => Size M [option_id] => 1 [option_type] => drop_down [option_value] => 1 [custom_view] => ) [1] => Array ( [label] => Color [value] => Black [print_value] => Black [option_id] => 2 [option_type] => drop_down [option_value] => 4 [custom_view] => ) ) )
Ankita Biswas
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Re: Get product options value in Magento2
Hello @Ankita Biswas ,
Firstly, you can use ObjectManager:
- Get custom options of products present in shopping cart
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $cart = $objectManager->get('\Magento\Checkout\Model\Cart'); // get cart items $items = $cart->getItems(); // get custom options value of cart items foreach ($items as $item) { $options = $item->getProduct()->getTypeInstance(true)->getOrderOptions($item->getProduct()); $customOptions = $options['options']; if (!empty($customOptions)) { foreach ($customOptions as $option) { $optionTitle = $option['label']; $optionId = $option['option_id']; $optionType = $option['type']; $optionValue = $option['value']; } } }
- Retrieve custom options of products present any order
$orderObject = $objectManager->get('\Magento\Sales\Model\Order'); // load by order id $orderId = 1; // YOUR ORDER ID $order = $orderObject->load($orderId); // load by order increment id // $incrementId = '000000001'; // YOUR ORDER INCREMENT ID // $order = $orderObject->loadByIncrementId($incrementId); $items = $order->getAllVisibleItems(); // get all items aren't marked as deleted and that do not have parent item; for e.g. here associated simple products of a configurable products are not fetched // Order items can also be fetched with the following functions // $items = $order->getAllItems(); // get all items that are not marked as deleted // $items = $order->getItems(); // get all items foreach ($items as $item) { $options = $item->getProductOptions(); if (isset($options['options']) && !empty($options['options'])) { foreach ($options['options'] as $option) { echo 'Title: ' . $option['label'] . '<br />'; echo 'ID: ' . $option['option_id'] . '<br />'; echo 'Type: ' . $option['option_type'] . '<br />'; echo 'Value: ' . $option['option_value'] . '<br />' . '<br />'; } } }
However, using ObjectManager directly for that is not the great solution. You can still use the code with dependency injection as the below:
- Open the block class Extension_HelloWorld, then inject the object of \Magento\Sales\Model\Order in the constructor of my module’s block class.
app/code/Extension/HelloWorld/Block/HelloWorld.php
<?php namespace Extension\HelloWorld\Block; class HelloWorld extends \Magento\Framework\View\Element\Template { protected $_orderModel; public function __construct( \Magento\Backend\Block\Template\Context $context, \Magento\Sales\Model\Order $orderModel, array $data = [] ) { $this->_orderModel = $orderModel; parent::__construct($context, $data); } public function getOrderItems($orderId) { $order = $this->_orderModel->load($orderId); return $order->getAllVisibleItems(); } } ?>
- Like that, you can use the function in the .phtml file.
$orderId = 1; // YOUR ORDER ID $items = $block->getOrderItems($orderId); foreach ($items as $item) { $options = $item->getProductOptions(); if (isset($options['options']) && !empty($options['options'])) { foreach ($options['options'] as $option) { echo 'Title: ' . $option['label'] . '<br />'; echo 'ID: ' . $option['option_id'] . '<br />'; echo 'Type: ' . $option['option_type'] . '<br />'; echo 'Value: ' . $option['option_value'] . '<br />' . '<br />'; } } }
--
If my answer is useful, please Accept as Solution & give Kudos