Magento 2: Get Product’s Custom Option Value from Cart & Order
This article shows how to get custom option values of products/items added to cart or products of any order in Magento 2.
I will simply be using ObjectManager for this example.
Get custom options of products present in shopping cart
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | $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']; } } } |
Get custom options of products present any order
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | $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 />'; } } } |
It is always recommended to use the above code with dependency injection in your custom module instead of directly using Object Manager. Here is how you can do it:
Below is a block class of my custom module (Chapagain_HelloWorld). I have injected object of \Magento\Sales\Model\Order in the constructor of my module’s block class.
app/code/Chapagain/HelloWorld/Block/HelloWorld.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <?php namespace Chapagain\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(); } } ?> |
Now, we use can the function in our template (.phtml) file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | $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 />'; } } } |
Hope this helps. Thanks.





Mukesh Chapagain is a graduate of Kathmandu University (Dhulikhel, Nepal) from where he holds a Masters degree in Computer Engineering. Mukesh is a passionate web developer who has keen interest in open source technologies, programming & blogging.