How to Get Product Custom Options Value From Cart and Order in Magento 2
The customers like to receive a personalized experience while purchasing anything from the online store, especially when it comes to the custom options assigned to a particular product. The more options, the more enhanced the customer experience!
An easy way to offer a selection of options with various text, selection, and date input types is to add customizable options to a product.
When you add custom options to a product in Magento 2, you allow your customers to choose product options according to their needs without relying on the product attributes.
After adding custom options to the product page, what if you want to deal with third-party API and need to get that product custom option value from cart or order? What if you’re going to pass that custom option value immediately after the order placed?
You can get product custom options value from cart and order in Magento 2 by following the below solution.
Method to Get Product Custom Options Value From Cart and Order in Magento 2
Use the below code in the helper file.
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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
<?php namespace Vendor\Extension\Helper; use Magento\Checkout\Model\Cart; use Magento\Framework\App\Helper\AbstractHelper; use Magento\Framework\App\Helper\Context; use Magento\Sales\Model\Order; class Data extends AbstractHelper { protected $orderModel; protected $quote; public function __construct( Context $context, Order $orderModel, Cart $cart ) { $this->orderModel = $orderModel; $this->quote = $cart; parent::__construct($context); } public function getProductOptions() { $orderId = 23; $order = $this->orderModel->load($orderId); $orderItems = $order->getAllVisibleItems(); foreach ($orderItems as $item) { $options = $item->getProductOptions(); $AllOptionsData = $options; if (isset($options['attributes_info'])) { $attributesOptions = $options['attributes_info']; $optionValue = $attributesOptions['option_value']; } if (isset($options['additional_options'])) { $additionalOptions = $options['additional_options']; $optionValue = $additionalOptions['option_value']; } if (isset($options['options'])) { $productOptions = $options['options']; $optionId = $productOptions['option_id']; $optionValue = $productOptions['option_value']; } } } public function getCartItemOptions() { $cartItems = $this->quote->getItems(); foreach ($cartItems as $item) { $options = $item->getProduct()->getTypeInstance(true)->getOrderOptions($item->getProduct()); $AllOptionsData = $options; // you check particular options data same as above. } } } |
That’s it!
Also Read: SQL query to get all products with custom options in Magento 2.
Any doubts about this solution can be mentioned in the Comments section below.
I’d be happy to help.
Also, do share the post with Magento Community via social media.
Thank you.
Chandresh Chauhan
He has been with Meetanshi for more than three years now as a certified Magento developer. A silent guy whom you can always find solving clients' issues, is an avid reader too.
Prev
How to Override Category View Page in Magento 2
How to Fetch Bearer Token in Magento 2
Next