Magento 2 KnockoutJS – Additional Data in Checkout Order Summary

knockoutjsmagento2

I have overidden the template:

/Magento_Checkout/web/template/summary/item/details.html

For each row, if set, I need to display the "additional_data" field. I can get the value for an arbitrary row by using (e.g. for the first row):

window.checkoutConfig.quoteItemData[0].additional_data

But I can't work out how to display it for the current row in the Knockout template. The $parent object which is used to output the product's price, custom options, etc, does not expose this information.

Can anyone help?

Best Answer

You can add additional options to order items using observer and it will show everywhere. First need to create events.xml for the observer:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="catalog_product_load_after">
    <observer name="add_additional_option" instance="Namespace\Module\Observer\AddAdditionalOption"/>
</event>

After that write below code in observer file:

<?php
namespace Namespace\Module\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\RequestInterface;

class AddAdditionalOption implements ObserverInterface
{

protected $_request;

public function __construct(
    RequestInterface $request
) {
    $this->_request = $request;
}

public function execute(\Magento\Framework\Event\Observer $observer)
{
    if ($this->_request->getFullActionName() == 'checkout_cart_add') {             
        $product = $observer->getProduct();
        $additionalOptions = [];
        $additionalOptions[] = array(
            'label' => "Custom Label",
            'value' => "Custom Info",
        );
        $observer->getProduct()->addCustomOption('additional_options', serialize($additionalOptions));
    }
}
}