Magento has an awesome way to manage configuration values. Under Admin>System>Configuration all useful configuration for different module resides. Today I am going to show you how to create a new configuration value in admin and how use them from your code.

For example Lets create 2 fields under configuration, Enable Minimum Production Calculation , a drop down with Yes/No value and another a text input field Minimum Production Cost

Step 1: 

Set path for your configuration field in \app\code\local\Mypackage\Mymodule\etc\Config.xml by create a default node under config.


<?xml version="1.0"?>
<config>

    <modules>
        <Mypackage_Mymodule>
            <version>0.3.0</version>
        </Mypackage_Mymodule>
    </modules>

	<global>

        <models>
            <mymodule>
                <class>Mypackage_Mymodule_Model</class>
                <resourceModel>mymodule_mysql4</resourceModel>
            </mymodule>
            <mymodule_mysql4>
                <class>Mypackage_Mymodule_Model_Mysql4</class>
                <entities>
                    <mymodule>
                        <table>mytablename</table>
                    </mymodule>
                </entities>
            </mymodule_mysql4>
        </models>

        <resources>
            <mymodule_setup>
                <setup>
                    <module>Mypackage_Mymodule</module>
                </setup>
                <connection>
                    <use>core_setup</use>
                </connection>
            </mymodule_setup>
            <mymodule_write>
                <connection>
                    <use>core_write</use>
                </connection>
            </mymodule_write>
            <mymodule_read>
                <connection>
                    <use>core_read</use>
                </connection>
            </mymodule_read>
        </resources>

	    <helpers>
            <mymodule>
                <class>Mypackage_Mymodule_Helper</class>
            </mymodule>
        </helpers>

    </global>
	<!-- Create we create config path for cofiguration -->

	<default>
		<!-- Under checkout/mimimumproduction we create out config paths and their default values -->
		<checkout>
			<minimumproduction>
				<!-- Config path checkout/mimimumproduction/minimum_production_enabled with defaule value 1 -->
				<minimum_production_enabled>1</minimum_production_enabled>
				<!-- Config path checkout/mimimumproduction/minimum_production_cost with defaule value 200 -->
				<minimum_production_cost>200</minimum_production_cost>

			</minimumproduction>
		</checkout>
	</default>
</config>

That’s it, now can from your code you can access this config values using Mage::getStoreConfigFlag() and Mage::getStoreConfig like following:-

//return true false based on config settings
Mage::getStoreConfigFlag('checkout/minimumproduction /minimum_production_enabled');
//return 200
Mage::getStoreConfig('checkout/minimumproduction/minimum_production_cost');

But still you are not done.Customer not gonna change these values from XML. You need to give your client a way to set these values from admin panel. So, lets make our hands dirty on admin panel changing 🙂

Step 2:-

Create your configuraion alias node under \app\code\local\Mypackage\Mymodule\etc\system.xml

<?xml version="1.0"?>

<config>
    <sections>
        <checkout translate="label" module="checkout">

			<!-- If want to set these config values under youw own module, only then change the following commented fields -->
			<!--
            <label>Checkout</label>
            <tab>sales</tab>
            <frontend_type>text</frontend_type>
            <sort_order>305</sort_order>
            <show_in_default>1</show_in_default>
            <show_in_website>1</show_in_website>
            <show_in_store>0</show_in_store> -->

            <groups>

				<!-- Create a field group for your config paths -->
                <minimumproduction translate="label">
                    <label>Checkout Minimum Production Cost</label>
                    <frontend_type>text</frontend_type>
                    <sort_order>500</sort_order>
                    <show_in_default>1</show_in_default>
                    <show_in_website>1</show_in_website>
                    <show_in_store>1</show_in_store>

					<!-- List your config values here -->
                    <fields>

						<!-- This goes for checkout/minimumproduction/minimum_production_enabled -->
						<minimum_production_enabled translate="label">
                            <label>Enable Minimum Production Calculation</label>
                            <frontend_type>select</frontend_type>
                            <source_model>adminhtml/system_config_source_yesno</source_model>
                            <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>
                        </minimum_production_enabled>

						<!-- This goes for checkout/minimumproduction/minimum_production_cost -->
						<minimum_production_cost translate="label">
                            <label>Minimum Production Cost</label>
                            <frontend_type>text</frontend_type>
                            <sort_order>2</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                        </minimum_production_cost>

                    </fields>

                </minimumproduction>

            </groups>

        </checkout>

    </sections>

</config><!--more-->

Ya, now you are done. If you go to Admin>System>Configuration you can see a new field group with name Checkout Minimum Production Cost created, under which you will find your configuration values with default values. You can change your default settings from here.

Have a happy time with Magento 😀 !!!

Advertisements