Get Store System Configuration Value in Magento 2

In this quick article, I am going to show you how to get the store system configuration value in Magento 2 programmatically. In the below example, I will be using both Dependency Injection (DI) and Object Manager to get the system configuration value.

Using Dependency Injection (DI)

Here is the example code to get the store system configuration value in Magento 2 using dependency injection.

In this, we might need to inject the object of \Magento\Framework\App\Config\ScopeConfigInterface class to get the store system configuration data in the constructor of our module’s block class and access it from the view ( .phtml ) file.

Sample File Path: app/code/YourCompanyName/YourModuleName/Helper/Data.php

<?php
namespace YourCompanyName\YourModuleName\Helper;

class Data extends \Magento\Framework\App\Helper\AbstractHelper
{ /** * @var \Magento\Framework\App\Config\ScopeConfigInterface */ protected $scopeConfig; /** * Data constructor * @param \Magento\Framework\App\Helper\Context $context * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig */ public function __construct( \Magento\Framework\App\Helper\Context $context, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig ) { parent::__construct($context); $this->scopeConfig = $scopeConfig; } /** * @return $telephoneConfig */ public function getTelephoneConfig() { $telephoneConfig = $this->scopeConfig->getValue( 'general/store_information/phone', \Magento\Store\Model\ScopeInterface::SCOPE_STORE ); return $telephoneConfig; } }

Then you can inject the above helper file anywhere in the Magento 2 store and get the store system configuration value.

Here is the example code to inject the above helper file in the block,

protected $yourHelperData;

public function __construct(
     ....
    \YourCompanyName\YourModuleName\Helper\Data $yourHelperData,
    ....
) {
    ....
    $this->yourHelperData = $yourHelperData;
    ....
}

public function getTelephoneConfig() {
    return $this->yourHelperData->getTelephoneConfig();
}

Now we can access it from template file as follows,

$block->getTelephoneConfig();

You may also directly access the above helper function from the template file as follows,

<?php 
     $yourHelperData = $this->helper('YourCompanyName\YourModuleName\Helper\Data');
     echo $yourHelperData->getTelephoneConfig();

Using Object Manager

Here is the example code to get the store system configuration value in Magento 2 using object manager.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$scopeConfig = $objectManager->get('\Magento\Framework\App\Config\ScopeConfigInterface');

$telephoneConfig = $scopeConfig->getValue(
            'general/store_information/phone', 
            \Magento\Store\Model\ScopeInterface::SCOPE_STORE
        );

echo $telephoneConfig;

Note: Magento recommends using the dependency injection method. Whether you are developing an extension or a theme, avoid using the object manager method as you may receive technical rejection while submitting your extension to the Magento marketplace.

Hope this helps.

Leave a Reply

Your email address will not be published. Required fields are marked *