Cookies setting

Cookies help us enhance your experience on our site by storing information about your preferences and interactions. You can customize your cookie settings by choosing which cookies to allow. Please note that disabling certain cookies might impact the functionality and features of our services, such as personalized content and suggestions. Cookie Policy

Cookie Policy
Essential cookies

These cookies are strictly necessary for the site to work and may not be disabled.

Information
Always enabled
Advertising cookies

Advertising cookies deliver ads relevant to your interests, limit ad frequency, and measure ad effectiveness.

Information
Analytics cookies

Analytics cookies collect information and report website usage statistics without personally identifying individual visitors to Google.

Information
mageplaza.com

How to Get Value of Product Custom Option from Cart & Order in Magento 2

Vinh Jacker | 12-18-2024

How to Get Value of Product Custom Option from Cart & Order in Magento 2

When shopping online, customers will prefer shopping from a store that has multiple product types and product options. They have various options to choose from, which enhances their shopping experience with no limitations.

Therefore, it’s critical to add customizable product options to your products. Sometimes, after adding product options to a product page, you will want to get that product custom option value from cart and order, you will need to know how it is done correctly in Magento 2.

If you are wondering how to get the value of product custom option from cart and order in Magento 2, this is the right place for you to learn that. Published by Mageplaza, this topic will point you how to do that with two examples as the following.


Magento 2 extensions

Magento 2 extensions

Allow you to achieve more with your online store

Check it out!


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 Mageplaza_HelloWorld, then inject the object of \Magento\Sales\Model\Order in the constructor of my module’s block class.

app/code/Mageplaza/HelloWorld/Block/HelloWorld.php

<?php
namespace Mageplaza\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 />';
        }
    }
}

Wrap Up

That is all things you will use to retrieve value product custom option from cart and order in Magento 2. Thanks for your reading and please comment below if you face any issues in the process. We’ll try our best to help you.

x
    Jacker

    With over a decade of experience crafting innovative tech solutions for ecommerce businesses built on Magento, Jacker is the mastermind behind our secure and well-functioned extensions. With his expertise in building user-friendly interfaces and robust back-end systems, Mageplaza was able to deliver exceptional Magento solutions and services for over 122K+ customers around the world.



    Related Post

    Website Support
    & Maintenance Services

    Make sure your store is not only in good shape but also thriving with a professional team yet at an affordable price.

    Get Started
    mageplaza services