How To

How to Get Store Config Value in Magento 2

Hello Magento Friends,

In this blog, we will learn about getting store config value in Magento 2.

Magento stores all the configuration values in the core_config_data table. When you want to retrieve store config data, you can use the below steps.

Steps to Get Store Config Value in Magento 2:

Step 1: Create the system.xml file inside the etc folder

Path – Vendor/Extension/etc/adminhtml/system.xml     

Then add the code as follows

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
    <system>
        <section id="custom" translate="label" type="text" sortOrder="310" showInDefault="1" showInWebsite="1" showInStore="1">
            <label>Custom Settings</label>
            <group id="custom_group_id" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>Main label</label>
                <field id="custom_field_id" translate="label" type="text" sortOrder="30" showInDefault="1" showInWebsite="1"  showInStore="1">
                    <label>Field Name</label>
                </field>
            </group>
        </section>
    </system>
</config>

Step 2: Now create Data.php file inside the Helper folder

Path – Vendor/Extension/Helper/Data.php    

Then add the below code snippet

<?php
namespace Vendor\Extension\Helper;
use Magento\Store\Model\ScopeInterface;
use Magento\Framework\App\Helper\AbstractHelper;

class Data extends AbstractHelper
{
   const XML_CONFIG_PATH = 'custom/custom_group_id/custom_field_id';
   
   public function getConfig()
    {
        $configValue = $this->scopeConfig->getValue(self::XML_CONFIG_PATH,ScopeInterface::SCOPE_STORE);
        return $configValue;
    }
}

Note – You can inject the helper class anywhere and use the getConfig() function to get the config value.

Conclusion:

This way you can Get Store Config Value in Magento 2. If you have any doubts let me know through the comment part. Share the article with your friends and stay updated with us for the latest tutorials.

Happy Coding!

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

Generating Thumbnails with Spatie Media Library in Laravel 11: A Step-by-Step Guide

Generating image thumbnails is a common requirement in web applications, especially when handling media-heavy content.…

6 hours ago

Enhancing Web Application Security with Laravel’s Built-In Features

In today’s digital landscape, web application security is paramount. As a powerful PHP framework, Laravel…

1 day ago

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

October was an exciting month for MageComp! From significant updates across our Magento 2 extension…

1 day ago

Improving Error Handling and Transition Management in Remix with useRouteError and useViewTransitionState

In modern web development, seamless navigation and state management are crucial for delivering a smooth…

1 week ago

Magento Open Source 2.4.8-Beta Release Notes

Magento Open Source 2.4.8 beta version released on October  8, 2024. The latest release of…

1 week ago

How to Create Catalog Price Rule in Magento 2 Programmatically?

Hello Magento Friends, Creating catalog price rules programmatically in Magento 2 can be a valuable…

1 week ago