How To

Magento 2: How to Access Configuration Variables in Checkout Javascript Files?

Hello Magento Friends,

This article is about Magento 2: How to Access Configuration Variables in Checkout Javascript Files?

Sometimes the user needs to add some custom variables to the checkout page and can use it to develop some custom functionality or implement some custom code in Magento 2. Here is how you can Access Configuration Variables in Checkout Javascript Files in Magento 2.

Steps to Access Configuration Variables in Checkout Javascript Files in Magento 2:

Step 1: First, we need to create a “di.xml” file inside our extension at the following path.

app\code\Vendor\Extension\etc\

Now, add the code as follows

<?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_config_provider" xsi:type="object">Vendor\Extension\Model\ConfigProvider</item>
            </argument>
        </arguments>
    </type>
</config>

Step 2: After that, we need to create a “ConfigProvider.php” file inside the extension Model directory

app\code\Vendor\Extension\Model\

And add the below-mentioned code

<?php
namespace Vendor\Extension\Model;

use Magento\Checkout\Model\ConfigProviderInterface;
use Magento\Framework\View\LayoutInterface;

class ConfigProvider implements ConfigProviderInterface
{
    /** @var LayoutInterface  */    protected $_layout;

    public function __construct(LayoutInterface $layout)
    {
        $this->_layout = $layout;
    }

    /**
     * {@inheritdoc}
     */    public function getConfig()
    { 
        $config = [
            'payment' => [    
                'custom_config' => [/*Add your custom variables here*/                    'redirectUrl' => 'www.google.com',
                ]
            ]
        ];
        return $config;
    }
}

Step 3: After that, you can access this from checkout javascript files in the following way

//var redirectUrl = window.checkoutConfig.payment.custom_config.redirectUrl;
console.log(window.checkoutConfig.payment.custom_config.redirectUrl);

Conclusion:

With the help of these steps, you can successfully achieve the task to Access Configuration Variables in Checkout Javascript Files in Magento 2. If you face any issues while implementing, then mention them in the comment box or contact our support team. We will be happy to help you.

Besides this, you can also Get System Configuration Value Into JS in Magento 2. Do not forget to spread this article to your Magento group. Catch you next time, till then remain connected with us!

Happy Coding!

Click to rate this post!
[Total: 4 Average: 5]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

Recent Posts

How to Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

56 mins ago

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

2 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

2 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

3 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

4 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

6 days ago