7

In magento 2, how to get value in core_config_data table.

Anyone know, show me step by step.Thanks!

Teja Bhagavan Kollepara
  • 3,816
  • 5
  • 32
  • 69
MrTo-Kane
  • 3,656
  • 10
  • 44
  • 74

3 Answers3

10

First you need to include the Magento\Store\Model\ScopeInterface class in your constructor:

protected $_scopeConfig;

public function __construct(
     ...
     \Magento\Store\Model\ScopeInterface $scopeInterface,
     ...
)
{
    ...
    $this->_scopeConfig = $scopeInterface;
    ...
}

Then in your class' method you can call the following:

$this->scopeConfig->getValue('path/of/config', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
Raphael at Digital Pianism
  • 70,385
  • 34
  • 188
  • 352
  • $this->scopeConfig->getValue() must be in multi line because as Magento2 EcgM2 standard show error "Line exceeds 120 characters". – Prince Patel May 20 '17 at 13:36
6

you need to inject the an instance of \Magento\Framework\App\Config\ScopeConfigInterface in your block.

$protected $scopeConfig;
protected $storeManager; 
public function __construct(
    ....
    \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
    \Magento\Store\Model\StoreManagerInterface $storeManager, 
    ....
) {
    ...
    $this->scopeConfig = $scopeConfig;
    $this->storeManager = $storeManager;  
    ....
}

Then create the method getStoreName()

public function getStoreName()
{
    return $this->scopeConfig->getValue(
        'general/store_information/name',
        \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
        $this->storeManager->getStore()->getStoreId()
 );
}

and call in your template echo $this->getStoreName()

Yogesh
  • 1,503
  • 1
  • 18
  • 37
MrTo-Kane
  • 3,656
  • 10
  • 44
  • 74
1

You can use \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, In your constructor argument and create an Object $this->scopeConfig = $scopeConfig;

Now, you can get config value as below:

$this->_scopeConfig->getValue('path of config', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);

For more details, check these:

http://magehelper.blogspot.in/2015/06/get-system-config-values-in-magento-2.html https://maxyek.wordpress.com/2015/04/03/building-magento-2-extension-extendedconfig/

Shyam
  • 1,690
  • 17
  • 31