Hello Magento Folks,
Hope all are doing well, I am here back with one of the interesting articles on Magento 2 How to Series. Previously, we have learned about How to add Custom Options to the Product Programmatically in Magento 2. So, today we will learn How to display extra product attributes in the checkout summary in Magento 2.
Introduction
If you are thinking about How to display extra product attributes in the checkout summary in Magento 2, then you are at the right place to get rich knowledge. Mainly, the custom options feature is an inbuilt feature by which you will be capable to help you make your product customize by all the available options. But if there is any requirement that you need to display custom options in the admin order programmatically then kindly follow the below given easy steps.
Let’s Start to Code:
Step 1: Firstly, create a registration file as per given below
app\code\Vendor\Extension\registration.php
1 2 3 4 5 6 7 8 9 10 11 |
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Vendor_Extension', __DIR__ ); |
Step 2: Create a module file as per given below
app\code\Vendor\Extension\etc\module.php
1 2 3 4 5 6 7 |
<?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="Vendor_Extension" setup_version="1.0.0"> </module> </config> |
Step 3: Create a catalog_attributes file as per given below
app\code\Vendor\Extension\etc\catalog_attributes.php
1 2 3 4 5 6 7 8 9 10 11 |
<?xml version="1.0" ?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Catalog:etc/catalog_attributes.xsd"> <group name="quote_item"> <attribute name="custom_attribute_new"/> // your attribute name here... </group> </config> |
Step 4: Create a di.xml file as per given below.
app\code\Vendor\Extension\etc\di.xml
1 2 3 4 5 6 7 8 9 10 11 |
<?xml version="1.0" encoding="UTF-8" ?> <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="show-extra-attribute-checkout-summary-product" type="Vendor\Extension\Plugin\Checkout\Model\Defaultconfigprovider" /> </type> </config> |
Step 5: Create a Defaultconfigprovider.php file as per given below.
app\code\Vendor\Extension\Plugin\Checkout\Model\Defaultconfigprovider.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
<?php namespace Vendor\Extension\Plugin\Checkout\Model; use Magento\Checkout\Model\Session as CheckoutSession; class Defaultconfigprovider { /** * @var CheckoutSession */ protected $checkoutSession; /** * Constructor * * @param CheckoutSession $checkoutSession */ public function __construct( CheckoutSession $checkoutSession ) { $this->checkoutSession = $checkoutSession; } public function afterGetConfig( \Magento\Checkout\Model\DefaultConfigProvider $subject, array $result ) { $items = $result['totalsData']['items']; foreach ($items as $index => $item) { $quoteItem = $this->checkoutSession->getQuote()->getItemById($item['item_id']); $result['quoteItemData'][$index]['custom_attribute_new'] = $quoteItem->getProduct()->getData('custom_attribute_new'); } return $result; } } |
Step 6: Create a file checkout_index_index.xml as per given below.
app\code\Vendor\Extension\view\frontend\layout\checkout_index_index.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
<?xml version="1.0" ?> <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> <body> <referenceBlock name="checkout.root"> <arguments> <argument name="jsLayout" xsi:type="array"> <item name="components" xsi:type="array"> <item name="checkout" xsi:type="array"> <item name="children" xsi:type="array"> <item name="sidebar" xsi:type="array"> <item name="children" xsi:type="array"> <item name="summary" xsi:type="array"> <item name="children" xsi:type="array"> <item name="cart_items" xsi:type="array"> <item name="children" xsi:type="array"> <item name="details" xsi:type="array"> <item name="component" xsi:type="string">Vendor_Extension/js/view/summary/item/details</item> </item> </item> </item> </item> </item> </item> </item> </item> </item> </item> </argument> </arguments> </referenceBlock> </body> </page> Step 7: Create a file details.js as per given below. app\code\Vendor\Extension\view\frontend\web\js\view\summary\item\details.js define( [ 'uiComponent' ], function (Component) { "use strict"; var quoteItemData = window.checkoutConfig.quoteItemData; return Component.extend({ defaults: { template: 'Vendor_Extension/summary/item/details' }, quoteItemData: quoteItemData, getValue: function(quoteItem) { return quoteItem.name; }, getCustomValue: function(quoteItem) { var item = this.getItem(quoteItem.item_id); if(item.custom_attribute_new){ return 'Custom Attribute: '+item.custom_attribute_new; }else{ return ''; } }, getItem: function(item_id) { var itemElement = null; _.each(this.quoteItemData, function(element, index) { if (element.item_id == item_id) { itemElement = element; } }); return itemElement; } }); } ); |
Step 8: Create a file details.html as per given below.
app\code\Vendor\Extension\view\frontend\web\template\summary\item\details.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
<!-- ko foreach: getRegion('before_details') --> <!-- ko template: getTemplate() --><!-- /ko --> <!-- /ko --> <div class="product-item-details"> <div class="product-item-inner"> <div class="product-item-name-block"> <strong class="product-item-name" data-bind="text: $parent.name"></strong> <!-- ko if: (getCustomValue($parent))--> <span class="product-item-pcb-master" data-bind="text: getCustomValue($parent)"></span> <!-- /ko --> <div class="details-qty"> <span class="label"><!-- ko i18n: 'Qty' --><!-- /ko --></span> <span class="value" data-bind="text: $parent.qty"></span> </div> </div> <!-- ko foreach: getRegion('after_details') --> <!-- ko template: getTemplate() --><!-- /ko --> <!-- /ko --> </div> |
At last, you will be able to Display Custom Product Attribute in Checkout Summary.
Over to You
Therefore, by completing the above steps you will be able to Display Custom Product Attribute in Checkout Summary and improve your store. Kindly contact our Support Team if you face any difficulties in implementing the above-given solution. Feel free to reach us if you have an interesting technical problem that we can solve for you.
And if you find the article interesting don’t forget to share with you Magento Buddies and also comment down your views in the comment section below.
Happy Coding.
I’m calling tax attribute from the custom tax table and trying to show the tax percentage based on delivery country but it’s not refreshing after selecting for a different country. Can you please tell me how to refresh the custom attribute value based on the chosen delivery country at the checkout?
Hello, I’m trying to complete this tutorial but I’m getting an error in checkout: Element ‘body’: Non-whitespace character content is not allowed because the content type is ‘element-only’.
Please, give me an email and I’ll contact you. Thank you!
Please reach out support@magecomp.com
app\code\Vendor\Extension\etc\catalog_attributes.php
this file should be .xml