The information contained in this tutorial is designed to help you obtain further knowledge of System Configuration XML in Magento Adminhtml with a minimum of time and effort. First, I will present the basic terms and elements of system configuration XML and then different methods to fetch a variable from the system configuration.

1. Basic terms and elements of system configuration XML

Magento provides the system configuration for you to configure your system and your modules as well. You are able to add your custom module configuration via system.xml file in the folder etc of your module folder, such as the file app/code/local/Magestore/Ajaxcart/etc/system.xml

<?xml version="1.0"?>
<config>
<tabs>
<magestore translate="label">
<label>Magestore Extension</label>
<sort_order>400</sort_order>
</magestore>
</tabs>
<sections>
<ajaxcart translate="label" module="ajaxcart">
<class>separator-top</class>
<label>Ajax Cart</label>
<tab>magestore</tab>
<frontend_type>text</frontend_type>
<sort_order>299</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
<groups>
<general translate="label">
<label>General Configuration</label>
<frontend_type>text</frontend_type>
<sort_order>1</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
<fields>
<enable translate="label">
<label>Enable Ajax Cart</label>
<frontend_type>select</frontend_type>
<sort_order>1</sort_order>
<source_model>adminhtml/system_config_source_yesno</source_model>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
<comment>Enable using Ajaxcart</comment>
</enable>
</fields>
</general>
</groups>
</ajaxcart>
</sections>
</config>

In this file, you can view some basic tags:
• tabs: tabs of the configuration showed on the left column of the configuration form.

  • magestore: tab’s identity
  • label: tab’s label
  • sort_order: order to show tabs

• sessions: sessions of your configuration, each session corresponds with a configuration form

  • ajaxcart: session’s identity
  • tab: its value is tab’s identity linking to session
  • show_in_default, show_in_website, show_in_store: the scope of this configuration

groups: define groups of your configuration form. Each group corresponds with a form fieldset
fields: define fields of your configuration form

  • frontend_type: the type of fields (similar to form field in previous chapter)
  • source_model: the source providing data for the field (only some types need sample data like select, multiselect…)
  • comment: the comment to explain the field

Besides, you can use some tags such as frontend_model,backend_model,upload_dir,base_url…, depending on your field.
Default, Magento use Mage_Adminhtml_Block_System_Config_Form Mage_Adminhtml_Block_System_Config_Form_Fieldset and Mage_Adminhtml_Block_System_Config_Form_Field blocks to render to HTML form. However, it’s possible to use your custom block to render via the configuration tag frontend_model. You are able to refer to classes above to write your custom block. If data of the form and database is not compatible, you can use tag backend_model to convert.
Your configuration maybe needs the default value for working. In this case, you can add it to config.xml file. For example: the default value is added to the file app/code/local/Magestore/Ajaxcart/etc/config.xml

<config>
...
<default>
<ajaxcart>
<general>
<enable>1</enable>
<timeout>10</timeout>
<cart>1</cart>
</general>
</ajaxcart>
</default>
</config>

That configuration values will be overridden if you save your custom value by the admin form and Magento will store that values to the table core_config_data in database.

• scope: the scope of the configuration (default,website,store)

• path: the path of the configuration, it equals <session_id>/<group_id>/<field_id>

• value: the value of the configuration

2. Fetch a variable from System configuration

It’s necessary to get the configuration value of fields above. To do that, you can choose one of the following methods provided by Magento.

Method 1:  Mage::getStoreConfig($path,$store)

• $path: is the path to your configuration which is similar to the path stored in database, it equals <session_id>/<group_id>/<field_id>

• $store: a store to get configuration. It can be a store object, store id or store code.

For instance:

$enable = Mage::getStoreConfig('socialrecommend/general/enable','default');

Method 2: getConfig
You can use the method getConfig of a store/website object with the $path parameter (as same as above). For example:

$enable = Mage::app()->getStore()-> getConfig('socialrecommend/general/enable');

Method 3: getNode
It’s also possible to use the method getNode of the configuration model. This function has three parameters:
• $path: a path to the configuration. If $scope is store or website, this path will be similar to two methods above. On the other hand, $path will be the path from tag to your tag that you want to get configuration.
• $scope: not required. It can be store or website
• $scopeCode: not required. It can be store/website code.
For example:

$enable = Mage::getConfig()-> getNode('socialrecommend/general/enable','store','default');

Or getting the configuration by the path from tag:

$enable = Mage::getConfig()-> getNode('stores/default/socialrecommend/general/enable');

Enjoy coding!

Author

Why Magestore? We believe in building a meaningful & long-term relationship with you.

3 Comments

  1. HI David,

    Thank you very much for your serie dedicated for magento certification, I think you can modify the session term by section in this topic.

    All the best.

  2. Looking for such great article is too hard, gladly to have found your blog. Keep it up! thanks for sharing.

Write A Comment