0

How to add a custom field with its value in the checkout cart summary in Magento 2 ?

I have followed many links, but none helped me.

Any help would be appreciated.

  • 2
    any more precise description of where your custom field comes from and what is the purpose of your customisation would help us to see what is your issue besides customising summary block in the checkout.. – Herve Tribouilloy Jan 29 at 10:24
  • A buyer purchases a package of 20% which we are maintaining in a custom table, so that value I need to show in the summary page of a current user. – Amy Jan 29 at 10:30
0

If you need just dynamic data at the cart or checkout page

1.create etc/frontend/di.xml file

<?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\CompositeConfigProvider">
       <arguments>
           <argument name="configProviders" xsi:type="array">
               <item name="custom_provider" xsi:type="object">Vendor\Namespace\Model\CustomConfigProvider</item>
           </argument>
       </arguments>
   </type>
</config>

2.create the CustomConfigProvider.php in Vendor\Namespace\Model\ folder

<?php

namespace Vendor\Namespace\Model;
class CustomConfigProvider implements \Magento\Checkout\Model\ConfigProviderInterface
{
   public function getConfig()
   {
       $output['custom_config'] = 'Custom Config';
       return $output;
   }
}

3.Now Run the command

php bin/magento c:f

4. Please open the console of website and type the command

console.log(window.checkoutConfig.custom_config); OR window.checkoutConfig.custom_config

If the result is 'Custom Config', Now you can use this variable for your purpose where you want.

I hope this is helpful to you!!

| improve this answer | |

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.