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?
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.
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.
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?
In modern web development, seamless navigation and state management are crucial for delivering a smooth…
Magento Open Source 2.4.8 beta version released on October 8, 2024. The latest release of…
Hello Magento Friends, Creating catalog price rules programmatically in Magento 2 can be a valuable…
As the world of eCommerce continues to thrive, Shopify has become one of the most…
Shopify Remix is an innovative framework that provides a streamlined experience for building fast, dynamic,…
Building a successful eCommerce store requires expertise, and for many businesses, Shopify has become the…