Archive for July, 2009


Here I come to know about an awesome feature of Magento. When we add any eav attribute to an entity we can give a backend model name. Whenever Entity will be loaded magento initiates Mage_Eav_Model_Entity_Attribute model for each attribut. If it finds any backend model name for a eav attribut, it loads the corresponding model and based on what it is doing it will call different method from that model.

Here I am giving a complete example how you can use this feature. We will add an attribue to address model as “address_verified” , use a backend model to set this value whenever the any address will be save.

Step 1.Create a custom model “Mymodule”

Step 2.Write an sql setup for this module. It will add an “address_verified” attribute to address entity

<?php $installer = $this; $installer->startSetup();

$installer->run("
INSERT INTO {$this->getTable('eav_attribute')}  (
`entity_type_id` ,
`attribute_code` ,
`attribute_model` ,
`backend_model` ,
`backend_type` ,
`backend_table` ,
`frontend_model` ,
`frontend_input` ,
`frontend_input_renderer` ,
`frontend_label` ,
`frontend_class` ,
`source_model` ,
`is_global` ,
`is_visible` ,
`is_required` ,
`is_user_defined` ,
`default_value` ,
`is_searchable` ,
`is_filterable` ,
`is_comparable` ,
`is_visible_on_front` ,
`is_html_allowed_on_front` ,
`is_unique` ,
`is_used_for_price_rules` ,
`is_filterable_in_search` ,
`used_in_product_listing` ,
`used_for_sort_by` ,
`is_configurable` ,
`apply_to` ,
`position` ,
`note` ,
`is_visible_in_advanced_search`
)
VALUES (
'2', 'is_validated', NULL , 'customer_entity/address_attribute_backend_validate', 'int', NULL , NULL , 'text', NULL , NULL , NULL , NULL , '1', '1', '0', '0', NULL , '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '', '', '', '0'
);

");

$installer->endSetup(); 

Step3.Write the back end model in your local code pool


<?php 

class Mypackage_Customer_Model_Entity_Address_Attribute_Backend_Validate
	extends Mage_Eav_Model_Entity_Attribute_Backend_Abstract
{
	public function beforeSave($object)
	{
		//Write your code to validate address
		$object->setIsValidated($satoriStatus);
		return $this;
    }
}

Step 4. Override the backend model so that it runs from your local pool

</config>
 <models>
 <customer_entity>
 <rewrite>
 <address_attribute_backend_satori>Mypackage_Customer_Model_Entity_Address_Attribute_Backend_Satori</address_attribute_backend_satori>
 </rewrite>
 </customer_entity>
 </models>
<config>

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 :-D !!!

Follow

Get every new post delivered to your Inbox.