6

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');

5 Answers 5

14

=> 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;
    }
2
  • My problem is that I don't know $storeId. I updated my question
    – Vixent
    Sep 13, 2017 at 7:38
  • I update my answer. Please check it @Vixent Sep 13, 2017 at 9:14
1

For Magento 1.9.X you will need to pass value of store id:

Mage::getStoreConfig('sectionName/groupName/fieldName', $store_id)

and for Magento 2, follow link https://magento.stackexchange.com/a/78473/24878

Thanks

1
  • My problem is that I don't know $store_id. I updated my question
    – Vixent
    Sep 13, 2017 at 7:39
0

For Magento 2 you need to use following code.

Just Use \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig in your constructor argument and set the class property:

$this->scopeConfig = $scopeConfig;

Below is complete code:

protected $scopeConfig;
public function __construct(
    Context $context,
   \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig

) 
{
    $this->scopeConfig = $scopeConfig;
    parent::__construct($context,
      scopeConfig
    );
}
$this->scopeConfig->getValue('sectionName/groupName/fieldName', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
2
  • Thanks but it isn't a solution for my problem. This is solution for a single store.
    – Vixent
    Sep 13, 2017 at 7:37
  • How to set the scope i mean store view for selected admin user using module magento.stackexchange.com/q/322871/57334
    – zus
    Sep 23, 2020 at 16:32
0

Update code config.xml

<adminhtml>
    <events>
        <admin_system_config_changed_section_custommodule>
            <observers>
                <custommodulesave>
                    <class>admin_system_config_changed_section_custommodule/adminhtml_system_config_backend_admin_observer</class>
                    <method>saveConfig</method>
                </custommodulesave>
            </observers>
        </admin_system_config_changed_section_custommodule>
    </events>
</adminhtml>

Add observer at Model/Adminhtml/System/Config/Backend/Admin/Observer.php

<?php 

class Namespace_Custommodule_Model_Adminhtml_System_Config_Backend_Admin_Observer
{
    /**
    * Save configuration with encryption
    */
    public function saveConfig($observer)
    {
        $user = 'my custom field data';
        $password = 'my custom field data';
        $data = $observer->getEvent()->getData();
        $scope = $data['store'];
        if($scope == ''){
            $scope = 'default';
        }
        $store = Mage::getModel('core/store')->load($scope);
        $scopeId = $store->getId();
        $store = Mage::getModel('core/store')->load($storeCode);
        Mage::getConfig()->saveConfig($userConfig, $user, $scope, $scopeId);
        Mage::getConfig()->saveConfig($passConfig, md5($password), $scope, $scopeId);
    }   
}
0

Another solution from Magento 1, adminhtml controller ajax call to get and set value in the correct view

in phtml ajax Request add

parameters: {store: "<?php echo (string)Mage::getSingleton('adminhtml/config_data')->getStore() ?>", website: "<?php echo (string)Mage::getSingleton('adminhtml/config_data')->getWebsite() ?>"},

In controller add these functions

private function getAdminStoreConfig($params, $path)
{
    if (isset($params['store']) && $params['store']) {
        $store = Mage::getModel('core/store')->load($params['store']);
        return Mage::getStoreConfig($path, $store);
    } elseif (isset($params['website']) && $params['website']) {
        $website = Mage::getModel('core/website')->load($params['website']);
        return $website->getConfig($path);
    } else {
        return Mage::getStoreConfig($path);
    }
}

private function saveAdminStoreConfig($params, $path, $value)
{
    if (isset($params['store']) && $params['store']) {
        $store = Mage::getModel('core/store')->load($params['store']);
        Mage::getConfig()->saveConfig($path, $value, 'stores', $store->getId());
    } elseif (isset($params['website']) && $params['website']) {
        $website = Mage::getModel('core/website')->load($params['website']);
        Mage::getConfig()->saveConfig($path, $value, 'websites', $website->getId());
    } else {
        Mage::getConfig()->saveConfig($path, $value, 'default', Mage_Core_Model_App::ADMIN_STORE_ID);
    }
}

In the Action

$this->saveAdminStoreConfig($this->getRequest()->getParams(), $value, $val);
$this->getAdminStoreConfig($params, $path);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.