How To

Magento 2: How to Get System Configuration Value Into JS

Hello Magento Folks?,

How are you all working? Welcome to How To tutorial blog for all the Magento development solutions. As always today I will guide you all Magento 2: How to Get System Configuration Value Into JS. Also go through our previously solved problem Magento 2: Regenerate Catalog Product Cache Images Programmatically. Let’s Do This?

Introduction:

Magento 2 based store which is generally pronounced as Luma store that works as a demo store where one can learn all the basic configurations. It is basically used for personal purposes or for learning purposes. Many times if there is more than one store working and to change the default configuration does not meet your requirements. When any custom field is made in System->Configuration then how to get the saved value in js. No worries here is the detailed guide for Magento 2: How to Get System Configuration Value Into JS.

Steps to Get System Configuration Value Into JS:

Step 1: First add your method in the Block:

app\code\Vendor\Extension \Block\Checkout\ Js.php

<?php
namespace Vendor\Extension\Block\Checkout;

use Magento\Framework\App\Config\ScopeConfigInterface;

class Js extends \Magento\Framework\View\Element\Template
{
      public function __construct(ScopeConfigInterface $scopeConfig)
      {
          $this->_scopeConfig = $scopeConfig;
      }

      public function getZipcodeOptionalYesNo()
      {
          return $this->_scopeConfig->getValue("section id/ group id / field id ");
      }
}

Step 2: Then set a global Js variable in your template:

app\code\Vendor\Extension\view\frontend\templates\ js.phtml

<script>
  window.valuesConfig = '<?php echo $block->getZipcodeOptionalYesNo(); ?>';
</script>

Step 3: And then, in your custom js, you can get:

app\code\ Vendor\Extension \view\frontend\web\js\ checkout_js.js

getValues: function() 
{
     return window.valuesConfig;
}

That’s how we can access a store->configurations value inside a JS file.

Final Words:

Hopefully, all are able to Get System Configuration Value Into JS by going through the above steps given. But in case of any difficulties just write down in the comment section below. If the article was helpful to you then do share with your Magento developer friends and make yourself helpful. Till then stay safe and keep developing.

Happy Coding?

Click to rate this post!
[Total: 11 Average: 4.2]
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

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…

2 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…

3 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

How Upcoming Cookie Changes Will Affect Your E-commerce Website?

The e-commerce world is constantly in flux. New tech and strategies emerge daily to help…

6 days ago