1

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?

2

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));
    }
}
}
5
  • 1
    Excellent answer! It now shows up in the cart, summary, order email and admin without the need for any custom templates. Perfect. I now need to see if I can get the additional options to be displayed before the other custom options, as I store the size that the customer ordered in there (hence it's the most important of all of the options)
    – Geat
    May 31 '17 at 16:02
  • 1
    Oh, okay - looks like I still need to programmatically add it to the order email and admin order viewer. But that's not too bad!
    – Geat
    May 31 '17 at 16:08
  • 2
    great.. Happy coding.. :-)
    – Gulshan
    May 31 '17 at 16:11
  • 1
    Okay, just one more question. Do you know how to carry the additional_options from the quote through to the final order? I've found an observer that uses "sales_model_service_quote_submit_before", but it doesn't seem to trigger at any point in the checkout flow.
    – Geat
    May 31 '17 at 16:42
  • 1
    Okay, no worries - I got it. It's not a frontend event so the definition needed to be in /etc/events.xml and not /etc/frontend/events.xml
    – Geat
    May 31 '17 at 16:46

Your Answer

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

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