0

sales_order_view.xml

<?xml version="1.0"?>
<!--
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="admin-2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="default_order_items_renderer">
            <action method="setTemplate">
                <argument name="template" translate="true" xsi:type="string">Vendor_Module::order/view/items/renderer/default.phtml</argument>
            </action>
        </referenceBlock>
    </body>
</page>

default.phtml

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

?>
<?php /** @var \Magento\Sales\Block\Adminhtml\Order\View\Items\Renderer\DefaultRenderer $block */ ?>
<?php $_item = $block->getItem() ?>
<?php $block->setPriceDataObject($_item) ?>
<tr>
    <?php $i = 0;
    $columns = $block->getColumns();
    $lastItemNumber = count($columns) ?>
    <?php foreach ($columns as $columnName => $columnClass):?>
        <?php $i++; ?>
        <td class="<?= /* @noEscape */ $columnClass ?><?= /* @noEscape */ ($i === $lastItemNumber ? ' last' : '') ?>"><?= /* @escapeNotVerified */ $block->getColumnHtml($_item, $columnName) ?>
            <?php if($columnClass == "col-product"){  ?>
                <span><?php echo __("Type:") ?></span>
                <?php
                 //here I'm getting empty array 
                 print_r( $_item->getOptionByCode('additional_options'));

                ?>
            <?php  } ?>
        </td>
    <?php endforeach; ?>
</tr>

$_item->getOptionByCode('additional_options') this lines returns empty array .Please someone guide me hoe to get the additional option value .I have seen the quote_item_option table it contains the data . I have added custom options to quote item using below code

        $quote = $this->quoteFactory->create()->load($quoteId);
        $_product = $this->product->load($productId);
        $additionalOptions = array();
            if ($additionalOption = $_product->getOptionByCode('additional_options')) {
                $additionalOptions = $this->serializer->unserialize($additionalOption->getValue());
            }

            $additionalOptions[] = [
                'label' => 'Type',
                'value' => 'Sample'
            ];
        $quote->addProduct($_product, 1)
                    ->addOption(array(
                        'product_id' => $_product->getId(),
                        'code' => 'additional_options',
                        'value' => $this->serializer->serialize($additionalOptions)
                    ))
                    ->setCustomPrice(2)
                    ->setWeight(5)
                    ->setOriginalCustomPrice(2)
                    ->getProduct()->setIsSuperMode(true);
                $quote->collectTotals()->save();

it is adding the item and to quote.After saving the order I tried to get the additional option value but i'm unable to get it on order details page

5
  • as you are manually adding item to quote you need to add it in order too
    – Ghulam.M
    Sep 28, 2020 at 10:10
  • but how??can you please share it
    – wofang
    Sep 28, 2020 at 10:11
  • You need to add a plugin please visit this link magento.stackexchange.com/questions/178386/…
    – Ghulam.M
    Sep 28, 2020 at 10:13
  • ok i'll try it.
    – wofang
    Sep 28, 2020 at 10:14
  • this is working for me please post it as answer :)
    – wofang
    Sep 28, 2020 at 10:27

1 Answer 1

2

We need to create a plugin for the retrieve custom option from the cart to order.

First We need to create 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">
    <type name="Magento\Quote\Model\Quote\Item\ToOrderItem">
        <plugin name="unique_name" type="Vendor\Module\Model\Plugin\Quote\ToOrderItem" sortOrder="1" />
    </type>
</config>

Once you have created this file, now you need to create another file and named as CompanyName\ModuleName\Model\Plugin\Quote\ToOrderItem.php

<?php
namespace Vendor\Module\Model\Plugin\Quote;

use Magento\Quote\Model\Quote\Item\ToOrderItem as QuoteToOrderItem;
use Magento\Framework\Serialize\Serializer\Json;
class ToOrderItem
{    
    public function __construct(Json $serializer = null) 
    {        
        $this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
            ->get(\Magento\Framework\Serialize\Serializer\Json::class);
    }

    public function aroundConvert(QuoteToOrderItem $subject,
        \Closure $proceed,
        $item,
        $data = []
    ) {
        // Get Order Item
        $orderItem = $proceed($item, $data);               
                
        $additionalOptions = $item->getOptionByCode('additional_options');        
        // Check if there is any additional options in Quote Item                    
        if (count($additionalOptions) > 0) {
            // Get Order Item's other options
            $options = $orderItem->getProductOptions();
            // Set additional options to Order Item
            $options['additional_options'] = $this->serializer->unserialize($additionalOptions->getValue());
            $orderItem->setProductOptions($options);
        }
 
        return $orderItem;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.