2

I am creating additional_option so that i can add option in cart and order detail and it is working fine in both way but problem is that when i try to fetch order data using REST api it is not getting that additional_option. Following is working code for adding option:

Step1: Add option in cart

event.xml

 <event name="checkout_cart_product_add_after">
  <observer name="bhupendra_custommodule_observer_process_custommodule" instance="Bhupendra\Custommodule\Model\ProcessOptions"/>
 </event> 

ProcessOptions.php(model)

public function execute(Observer $observer)
    {

        /* @var Magento\Quote\Model\Quote\Item $item */
        $item = $observer->getQuoteItem();
        $postdata = $this->_request->getPost();
        $additionalOptions = array();
        $product = $this->_productModel->load($postdata['product']);
        if ($additionalOption = $item->getOptionByCode('additional_options')){
            $additionalOptions = (array) unserialize($additionalOption->getValue());
        }

        $content= $postdata['product_information'];
        if(isset($content)  && $content!='' )
        {

                $additionalOptions[] = [
                    'label' => 'Custom Information',
                    'value' => $content
                ];

        }
        if(count($additionalOptions) > 0)
        {
            $item->addOption(array(
                'code' => 'additional_options',
                'value' => serialize($additionalOptions)
            ));
        }
    }

Step2: convert quote to order

di.xml

 <type name="Magento\Quote\Model\Quote\Item\ToOrderItem">
  <plugin name="bhupendra_custommodule_Sales_Quote_Item_ToOrderItem" type="Bhupendra\Custommodule\Plugin\QuoteItemToOrderItemPlugin" />
 </type>

plugin/QuoteItemToOrderItemPlugin.php

<?php

namespace Bhupendra\Custommodule\Plugin;

class QuoteItemToOrderItemPlugin
{

    public function aroundConvert(\Magento\Quote\Model\Quote\Item\ToOrderItem $subject, callable $proceed, $quoteItem, $data)
    {

        // get order item
        $orderItem = $proceed($quoteItem, $data);


        if(!$orderItem->getParentItemId() && $orderItem->getProductType() == \Magento\Catalog\Model\Product\Type::TYPE_SIMPLE){
            if ($additionalOptionsQuote = $quoteItem->getOptionByCode('additional_options')) {
                //To do
                // - check to make sure element are not added twice
                // - $additionalOptionsQuote - may not be an array
                if($additionalOptionsOrder = $orderItem->getProductOptionByCode('additional_options')){
                    $additionalOptions = array_merge($additionalOptionsQuote, $additionalOptionsOrder);
                }
                else{
                    $additionalOptions = $additionalOptionsQuote;
                }
                if(count($additionalOptions) > 0){
                    $options = $orderItem->getProductOptions();
                    $options['additional_options'] = unserialize($additionalOptions->getValue());
                    $orderItem->setProductOptions($options);
                }
            }
        }

        return $orderItem;
    }
}

Make REST API order call http://localhost/magento2test/rest/V1/orders/5

All data coming only additional_options are not there in that data. Any help into this ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.