Magento – Get Config Value for Specific Store from Admin Area

core-config-datamagento-1.9magento2system-configsystem.xml

I'm building a custom module for both Magento 1.9.x and 2.x. In the module's settings I want to get one of the config value i.e. I have a text field and when the user save the settings, I use the string put in this text area, saved in the database, to fill a select box and do other things in my module's settings.

My question is how can I access this value if I have a multistore? I'm able to access only the value of default config.

I think I need to get the scope id and use it to access the value but I don't know how to get it within the settings.

Update 1:

I don't know how can I get the store id from admin (in particular in configuration settings area). How can I get it?

I'll explain better my problem.
In my system.xml I have some fields that have a frontend model that I wrote (like a checkbox and a select box). In the frontend model, I need to get a value of the configuration itself. It's an api key (I can get it only after the user saves the settings). This api key can be different for each store, so in the frontend model I should get the value of the specific store, but I don't know how can I get the store id.

Update 2 (Solution in Magento 1.9.x)

I found the solution for Magento 1.9.x in this question:
How to get current store id from current scope in admin. This is the function that returns the store_id

public function getStoreId()
{
   if (strlen($code = Mage::getSingleton('adminhtml/config_data')->getStore())) // store level
   {
      $store_id = Mage::getModel('core/store')->load($code)->getId();
   }
   elseif (strlen($code = Mage::getSingleton('adminhtml/config_data')->getWebsite())) // website level
   {
      $website_id = Mage::getModel('core/website')->load($code)->getId();
      $store_id = Mage::app()->getWebsite($website_id)->getDefaultStore()->getId();
   }
   else // default level
   {
      $store_id = 0;
   }
   return $store_id;
}

And this is the code for get the config value

Mage::getStoreConfig('sectionName/groupName/fieldName', $this->getStoreId());

I'm searching the solution for Magento 2.x

Update 3 (Solution in Magento 2.x)

I solved my problem also in Magento 2.x. I use this to get the storeId:

$storeId = (int) $this->getRequest()->getParam('store', 0);

It works only in my frontend model because it extends \Magento\Config\Block\System\Config\Form\Field.

In the helper I have the function:

public function getConfig($configPath, $storeId = null)
{
   return $this->scopeConfig->getValue($configPath, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $storeId);
}

When I am in the frontend model I obtain $storeId and I use:

$this->helper->getConfig('sectionName/groupName/fieldName', $storeId);

When I am anywhere I use:

$this->helper->getConfig('sectionName/groupName/fieldName');

Best Answer

=> For Magento 1.9 :

Get Store Id :

Mage::app()->getStore()->getStoreId();

Get Config Value :

$configValue = Mage::getStoreConfig('sectionName/groupName/fieldName');

as per store vise :

$storeId = 2; // ID of the store you want to fetch the value of
$configValue = Mage::getStoreConfig('sectionName/groupName/fieldName', $storeId);

=> For Magento 2 :

Get Store Id :

protected $storeManager;    

public function __construct(
    \Magento\Backend\Block\Template\Context $context,        
    \Magento\Store\Model\StoreManagerInterface $storeManager,        
    array $data = []
)
{        
    $this->storeManager = $storeManager;        
    parent::__construct($context, $data);
}

/**
 * Get store identifier
 *
 * @return  int
 */
public function getStoreId()
{
    return $this->storeManager->getStore()->getId();
}

Get Config Value :

protected $_scopeConfig;

public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
    array $data = []
    ){
     parent::__construct($context, $data);
     $this->_scopeConfig = $scopeConfig;
    }
    public function getMyValue(){
        $myvalue = $this->_scopeConfig->getValue('sectionName/groupName/fieldName', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
         return $myvalue;
    }
Related Topic