Get Store Config Value in Magento 2

When developing custom modules or working on Magento 2 projects, you’ll often need to fetch configuration values from the backend. These values can control features like payment methods, shipping, or custom module settings.

Magento provides a clean way to retrieve configuration values programmatically using the ScopeConfigInterface. In this guide, we’ll walk through the recommended method, scope levels, and even cover the Object Manager approach (not recommended for production but useful in debugging).

Key Takeaways

  • Fetch config values using ScopeConfigInterface::getValue().

  • Preferred method: Dependency Injection.

  • Avoid Object Manager (only for debugging).

  • Scope levels: Store, Website, Default.

  • Config paths are defined in system.xml and stored in core_config_data table.

Why Fetch Store Config Values in Magento 2?

Store owners and developers rely on configuration values to:

  • Control store-specific settings (logos, addresses, shipping zones).

  • Manage payment and shipping modules dynamically.

  • Build custom modules that depend on admin settings.

  • Avoid hardcoding values and make the system more flexible.

Instead of hardcoding, you can fetch these settings from Magento’s core_config_data table or your custom system.xml.

Method 1: Using Dependency Injection (Recommended)

The recommended approach is to inject ScopeConfigInterface into your class constructor.

Step 1: Inject ScopeConfigInterface

<?php
namespace Vendor\Module\Block;

use Magento\Framework\View\Element\Template;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Store\Model\ScopeInterface;

class CustomBlock extends Template
{
    protected $scopeConfig;

    public function __construct(
        Template\Context $context,
        ScopeConfigInterface $scopeConfig,
        array $data = []
    ) {
        $this->scopeConfig = $scopeConfig;
        parent::__construct($context, $data);
    }

    public function getConfigValue($configPath)
    {
        return $this->scopeConfig->getValue(
            $configPath,
            ScopeInterface::SCOPE_STORE // Default: current store
        );
    }
}

Step 2: Call the Function

$value = $this->getConfigValue('section_id/group_id/field_id');

Here:

  • section_id/group_id/field_id comes from your system.xml.

  • ScopeInterface::SCOPE_STORE fetches the current store value.

Method 2: Using Object Manager (Not Recommended)

For debugging or quick scripts, you can also use Object Manager. However, this is not best practice for production because it introduces tight coupling.

<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$scopeConfig = $objectManager->get(\Magento\Framework\App\Config\ScopeConfigInterface::class);

$value = $scopeConfig->getValue(
    'section_id/group_id/field_id',
    \Magento\Store\Model\ScopeInterface::SCOPE_STORE
);

⚠️ Warning: Avoid this in production code. Always prefer Dependency Injection.

Understanding Scope Levels in Magento 2

When fetching config values, you must define the scope level:

  • SCOPE_STORE → Value for the current store view.

  • SCOPE_WEBSITE → Value for the website level.

  • SCOPE_DEFAULT → Global default configuration value.

Example:

$this->scopeConfig->getValue(
    'section_id/group_id/field_id',
    \Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE
);

Finding Your Config Path (system.xml & Database)

The configuration path is defined in etc/adminhtml/system.xml. You can also check the core_config_data table in your Magento database.

Example system.xml:

<?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="test" translate="label" sortOrder="310">
            <label>Test Settings</label>
            <group id="origin" translate="label" sortOrder="1">
                <label>Main label</label>
                <field id="postcode" translate="label" type="text" sortOrder="30">
                    <label>Postal Code</label>
                </field>
            </group>
        </section>
    </system>
</config>

Here, the path is:

test/origin/postcode

Best Practices for Fetching Config Values

  • Always use Dependency Injection (ScopeConfigInterface).

  • ✅ Use constants like ScopeInterface::SCOPE_STORE instead of hardcoding.

  • ✅ Document your system.xml structure for easier debugging.

  • ❌ Avoid using Object Manager in production.

  • ✅ For debugging, check core_config_data table to confirm values.

Conclusion

Fetching store config values in Magento 2 is straightforward with the ScopeConfigInterface. By using Dependency Injection, you ensure clean, testable, and maintainable code.

Remember:

  • Use DI method for production.

  • Understand scope levels (Store, Website, Default).

  • Use system.xml and database for finding paths.

With these best practices, you can make your Magento 2 store configuration flexible and reliable.

We hope we have got everything covered to help you get value from store configuration by scope in Magento 2. If we missed out anything, feel free to get in touch with us.