Get Custom Options In Order Via Api Magento 2

Today I will show you how to get the value of product custom option from cart and order in Magento 2. You can do this through a few steps.

 

Step 1:

In my case, the option name is "Extra Fees".

Create di.xml under /app/code/Magepow/Custom/etc/

 

File: /app/code/Magepow/Custom/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Sales\Model\Order" type="Magepow\Custom\Model\Order"/>
    <preference for="Magento\Sales\Model\Order\Item" type="Magepow\Custom\Model\Order\Item"/>    
</config>

Step 2:

Create Order.php under /app/code/Magepow/Custom/Model/

 

File: /app/code/Magepow/Custom/Model/Order.php

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magepow\Custom\Model;

class Order extends \Magento\Sales\Model\Order
{   
    public $extrafee = 'Extra Fees';

     /**
     * Get Items
     *
     * @return \Magento\Sales\Api\Data\OrderItemInterface[]
     */
    public function getItems()
    {

        $data = parent::getItems();
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $model = $objectManager->get('\Magento\Catalog\Model\Product');
        $priceHelper = $objectManager->create('Magento\Framework\Pricing\Helper\Data');
        foreach ($data as $item) {
            $options = $item->getProductOption();
            if(!$options) continue;
            $attributes = $options->getExtensionAttributes();
            $customOptions = $attributes->getCustomOptions();
            if(!is_array($customOptions)) continue;
            $productId     = $item->getProductId();
            $product = $model->load($productId);

            if(!$product) continue;
            $item->setSku($product->getSku());

            foreach ($customOptions as $key => $value) {
                $productOptions = $product->getOptions();
                foreach($productOptions as $_opt) {
                    if($_opt->getTitle() != $this->extrafee) continue;
                    $value_str = '';
                    foreach ( $_opt->getValues() as $opt) {
                        $value_str .= $opt->getData('title') . ': ' . $priceHelper->currency($opt->getData('price'), true, false) . ',';
                    }

                }
                $value->setOptionValue(rtrim($value_str,','));
            }
        }
        return $data;
    }
}

Get it in items:

Create Item.php under /app/code/Magepow/Custom/Model/Order/

 

File: /app/code/Magepow/Custom/Model/Order/Item.php

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magepow\Custom\Model\Order;

use Magento\Framework\Api\AttributeValueFactory;
use Magento\Sales\Api\Data\OrderItemInterface;
use Magento\Sales\Model\AbstractModel;

class Item extends \Magento\Sales\Model\Order\Item
{

    public $extrafee = 'Extra Fees';

    public function getProductOption()
    {
        $options = parent::getProductOption();
        $product = $this->getProduct();
        if(!$product) return $options;
        $this->setSku($product->getSku());
        if(!$options) return $options;
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $priceHelper = $objectManager->create('Magento\Framework\Pricing\Helper\Data');
        $attributes = $options->getExtensionAttributes();
        $customOptions = $attributes->getCustomOptions();
        if(!is_array($customOptions)) return $options;
        foreach ($customOptions as $key => $value) {
            $productOptions = $product->getOptions();
            foreach($productOptions as $_opt) {
                if($_opt->getTitle() != $this->extrafee) continue;
                $value_str = '';
                foreach ( $_opt->getValues() as $opt) {
                    $value_str .= $opt->getData('title') . ': ' . $priceHelper->currency($opt->getData('price'), true, false) . ',';
                }     
            }
            $value->setOptionValue(rtrim($value_str,','));
        }
        return $options;
    }
}

Step 3:

Now refresh Magento cache

php bin/magento cache:flush

 

Done. Hope this post helps you in some way.