Magento 2.x – Add custom product attribute to checkout summary
If you want to add or change the checkout summary section, you can proceed creating a module with a plugin, like this:
- app/code/Offset101/AttributeCheckout/registration.php
1234567<?php\Magento\Framework\Component\ComponentRegistrar::register(\Magento\Framework\Component\ComponentRegistrar::MODULE,'Offset101_AttributeCheckout',__DIR__); - app/code/Offset101/AttributeCheckout/etc/di.xml
12345678<?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\Checkout\Model\DefaultConfigProvider"><plugin name="AddAttPlug" type="Offset101\AttributeCheckout\Plugin\ConfigProviderPlugin" /></type></config> - app/code/Offset101/AttributeCheckout/etc/module.xml
123456<?xml version="1.0"?><config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"><module name="Offset101_AttributeCheckout" setup_version="1.0.0"></module></config> - app/code/Offset101/AttributeCheckout/Plugin/ConfigProviderPlugin.php
12345678910111213141516171819202122232425262728293031<?phpnamespace Offset101\AttributeCheckout\Plugin;class ConfigProviderPlugin extends \Magento\Framework\Model\AbstractModel{public function afterGetConfig(\Magento\Checkout\Model\DefaultConfigProvider $subject, array $result){$items = $result['totalsData']['items'];// Your logic here - for each added productforeach($items as $idx => $item){$options = json_decode($items[$idx]['options']);$output = "";foreach($options as $option){$output .= $option->label.' - '.strip_tags($option->value);}// New attribute to add$items[$idx]['checkoutItems'] = $output;}// Update the items array$result['totalsData']['items'] = $items;return $result;}}
- Then, you will need to override the following phtml in your custom theme app/design/frontend/<your_theme>/blank/Magento_Checkout/web/template/summary/item/details.html Adding your new field, below the name, like this:
1234567891011121314151617<!--/*** Copyright © 2013-2017 Magento, Inc. All rights reserved.* See COPYING.txt for license details.*/--><!-- ko foreach: getRegion('before_details') --><!-- ko template: getTemplate() --><!-- /ko --><!-- /ko --><div class="c-minicart__info"><div class="top-content_minicart"><div class="info-content"><p class="c-minicart__highlight" data-bind="text: $parent.name"></p><p class="c-minicart__highlight" data-bind="text: $parent.checkoutItems"></p> <!-- New field --></div></div> - Afterwards, remember to:
123456php bin/magento module:enable Offset101_AttributeCheckoutphp bin/magento setup:upgradephp bin/magento setup:di:compilephp bin/magento indexer:reindexphp bin/magento setup:static-content:deploy -fphp bin/magento cache:flush