Within Magento, we can use useful methods to return data that is saved within Magento’s configuration.
Mage::getStoreConfig() Mage::getConfig()->getNode() Mage::getStoreConfigFlag()
When using the getStoreConfig() method, we can pass in a single string parameter.
Mage::getStoreConfig('sectionName/groupName/fieldName');
The sectionName, groupName and fieldName are present under the <sections>, <groups> and <fields> nodes respectively in the system.xml files of Magento.
As an example, we can look at the system.xml file of the Mage_Catalog module.
<?xml version="1.0"?>
<config>
<sections>
<catalog translate="label" module="catalog">
....
<groups>
<frontend translate="label">
....
<fields>
....
<grid_per_page_values translate="label comment">
....
</grid_per_page_values>
....
</fields>
</frontend>
....
</groups>
....
</catalog>
....
</sections>
</config>
This means that we can get the value of this field by running the following.
Mage::getStoreConfig('catalog/frontend/grid_per_page_values');
Assuming this setting hasn’t been altered, Magento will give us the value.
12,24,36
You may also note that the Mage_Catalog module’s config.xml file has the default values saved for this setting.
<?xml version="1.0"?>
<config>
<default>
<catalog>
....
<frontend>
....
<grid_per_page_values>12,24,36</grid_per_page_values>
....
</frontend>
....
</catalog>
....
</default>
....
</config>
The getStoreConfig() method can be found in the Mage.php class.
public static function getStoreConfig($path, $store = null)
{
return self::app()->getStore($store)->getConfig($path);
}
The getStoreConfig() method delegates to the getConfig() method of the Mage_Core_Model_Store class.
public function getConfig($path)
{
if (isset($this->_configCache[$path])) {
return $this->_configCache[$path];
}
$config = Mage::getConfig();
$fullPath = 'stores/' . $this->getCode() . '/' . $path;
$data = $config->getNode($fullPath);
if (!$data && !Mage::isInstalled()) {
$data = $config->getNode('default/' . $path);
}
if (!$data) {
return null;
}
return $this->_processConfigValue($fullPath, $path, $data);
}
Within the getConfig() method, we can see that the $path variable gets prefixed with stores/ and the store code to bring back a full path using the getNode() method.
The Mage::getConfig()->getNode() line directly reads from configuration files, therefore you have to specify the full path including the scope from within the config nodes of the configuration files, e.g. stores/default/section/group/field. We can also use default/section/group/field as well.
public function getNode($path=null, $scope='', $scopeCode=null)
{
if ($scope !== '') {
if (('store' === $scope) || ('website' === $scope)) {
$scope .= 's';
}
if (('default' !== $scope) && is_int($scopeCode)) {
if ('stores' == $scope) {
$scopeCode = Mage::app()->getStore($scopeCode)->getCode();
} elseif ('websites' == $scope) {
$scopeCode = Mage::app()->getWebsite($scopeCode)->getCode();
} else {
Mage::throwException(Mage::helper('core')->__('Unknown scope "%s".', $scope));
}
}
$path = $scope . ($scopeCode ? '/' . $scopeCode : '' ) . (empty($path) ? '' : '/' . $path);
}
/**
* Check path cache loading
*/
if ($this->_useCache && ($path !== null)) {
$path = explode('/', $path);
$section= $path[0];
if (isset($this->_cacheSections[$section])) {
$res = $this->getSectionNode($path);
if ($res !== false) {
return $res;
}
}
}
return parent::getNode($path);
}
The getStoreConfigFlag() method will always return a boolean, therefore it is useful for returning the values of Yes/No dropdowns that use the in built adminhtml/system_config_source_yesno source model.
<?xml version="1.0"?>
<config>
<sections>
<catalog translate="label" module="catalog">
....
<groups>
<frontend translate="label">
....
<fields>
....
<flat_catalog_product translate="label comment">
....
</flat_catalog_product>
....
</fields>
</frontend>
....
</groups>
....
</catalog>
....
</sections>
</config>
Therefore for the above XML, the following code can be used.
Mage::getStoreConfigFlag('catalog/frontend/flat_catalog_product');
To output either 1 or 0.
If you need to view the complete Magento config XML tree, you can use the code seen below.
Mage::getConfig()->getXmlString()
Note: This article is based on Magento Community/Open Source version 1.9.