How to Get Store Config Value in Magento 2

 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.

Comments

Popular posts from this blog

Add Admin User name and Action name in Order Comment Section through Action Performed from Sales Order Grid in Magento 2

How to Update Product Stock Programmatically in Magento 2

How to Set and Get Cookie in Magento 2