3

I found that all knockout JS config data are coming from window.checkoutConfig.

Then I check onepage.phtml and found that they used this code to store data in window.checkoutConfig

window.checkoutConfig = <?php /* @escapeNotVerified */ echo \Zend_Json::encode($block->getCheckoutConfig()); ?>;

So my question is that how we add our custom data in window.checkoutConfig and use in checkout knockout js like this

var customData = window.checkoutConfig.myCustomData;

alert(customData);
12

You have to just override getConfig() function of checkout module.

app/code/Packagename/Checkout/etc/frontend/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <!-- pass custom variable to checkout page -->
    <type name="Magento\Checkout\Model\CompositeConfigProvider">
        <arguments>
            <argument name="configProviders" xsi:type="array">
                <item name="checkout_custom_shipping_block" xsi:type="object">Packagename\Checkout\Model\CustomConfigProvider</item>
            </argument>
        </arguments>
    </type>
</config>

app/code/Packagename/Checkout/Model/CustomConfigProvider.php file,

<?php
namespace Packagename\Checkout\Model;

use Magento\Checkout\Model\ConfigProviderInterface;

class CustomConfigProvider implements ConfigProviderInterface
{
    public function getConfig()
    {
        $config = [];
        $config['myCustomData'] = 'your custom value';

        return $config;
    }
}

Clear Cache and remove var/generation folder,

Inside your knockout js file, You can get value like,

var customData = window.checkoutConfig.myCustomData;

alert(customData);
  • 1
    Thanks... It working like charm. You are a master of checkout. – Prince Patel May 17 '17 at 6:40
  • Does this approach update the "window.checkoutConfig" variable even if you are right in the checkout? My background: I am adding a product to cart if you click on a checkbox in onepagecheckout. The minicart gets updated, but the quote items in the summary don't. They pull their information from this variable. That's why I am asking. – hallleron Nov 9 '17 at 7:51
  • important here is to put it really into the di.xml in frontend folder !! – roman204 May 11 '18 at 7:44

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.