<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Atwix</title>
	<atom:link href="http://www.atwix.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.atwix.com</link>
	<description>Custom Magento development, Magento modules and design</description>
	<lastBuildDate>Thu, 23 May 2013 14:21:54 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Get configurable product options in product list (category view)</title>
		<link>http://www.atwix.com/magento/configurable-product-options-category-view/</link>
		<comments>http://www.atwix.com/magento/configurable-product-options-category-view/#comments</comments>
		<pubDate>Thu, 23 May 2013 06:05:21 +0000</pubDate>
		<dc:creator>Volodymyr Vygovskyi</dc:creator>
				<category><![CDATA[Magento]]></category>
		<category><![CDATA[configurable product]]></category>
		<category><![CDATA[magento]]></category>
		<category><![CDATA[options]]></category>

		<guid isPermaLink="false">http://www.atwix.com/?p=2748</guid>
		<description><![CDATA[It's a pleasure to give you word of the fact that we've prepared some useful information on how to get configurable product options in the product list. ]]></description>
				<content:encoded><![CDATA[<p>It is a pretty common task to display some product options like colour, size at the product list (category view) page in Magento. And it can not be done just with the default installation. Such decision is very reasonable as it is a hard task to load options for every configurable product on the page. Therefore, we tried to find the most efficient method of doing this, so below &#8211; there is one of the solutions that we came up with. <span id="more-2748"></span><br />
Before we begin, take a look at the related articles <a title="How to get configurable product options and attributes" href="http://www.atwix.com/magento/configurable-product-attributes/#" target="_blank">How to get configurable product options and attributes</a> and <a title="Custom resource model for Magento product flat tables" href="http://www.atwix.com/magento/custom-resource-model-magento-product-flat-tables/" target="_blank">Custom resource model for Magento product flat tables</a>, where two ways of getting options of configurable products have been described. </p>
<p>The first one uses <em>getConfigurableAttributesAsArray</em> function. Applying it to every product can affect performance. The second way is &#8211; to use flat tables (it&#8217;s very interesting solution and such solution may be helpful in many situations).</p>
<p>However, it is time to describe another approach. While digging into core code you can notice that to generate associated product filters the <em>getUsedProducts</em> (see <em>app/code/core/Mage/Catalog/Model/Product/Type/Configurable.php</em> file) method is called for every configurable product. The collection of subproducts is being loaded here. What we are going to do is to use <em>catalog_product_collection_load_after</em> event (dispatched in <em>_afterLoad()</em>) related to this collection to store all needed options for the particular configurable product. We create a simple model to store options data grabbed from the subproducts collection. So, when the time to display products comes up we will have all needed options in our storage. To make it clearer let&#8217;s put some code. In this example let&#8217;s display the available product colours.<br />
Config file:</p>
<pre class="brush: xml; gutter: true">&lt;?xml version=&quot;1.0&quot;?&gt;
&lt;!--
/app/code/local/Atwix/ProductOptions/etc/config.xml 
--&gt;
&lt;config&gt;
    &lt;modules&gt;
        &lt;Atwix_ProductOptions&gt;
            &lt;version&gt;1.0.0&lt;/version&gt;
        &lt;/Atwix_ProductOptions&gt;
    &lt;/modules&gt;
    &lt;global&gt;
        &lt;helpers&gt;
            &lt;atwix_productoptions&gt;
                &lt;class&gt;Atwix_ProductOptions_Helper&lt;/class&gt;
            &lt;/atwix_productoptions&gt;
        &lt;/helpers&gt;
        &lt;models&gt;
            &lt;atwix_productoptions&gt;
                &lt;class&gt;Atwix_ProductOptions_Model&lt;/class&gt;
            &lt;/atwix_productoptions&gt;
        &lt;/models&gt;
    &lt;/global&gt;
    &lt;frontend&gt;
        &lt;events&gt;
            &lt;catalog_product_collection_load_after&gt;
                &lt;observers&gt;
                    &lt;atwix_productoptions&gt;
                        &lt;class&gt;atwix_productoptions/observer&lt;/class&gt;
                        &lt;method&gt;storeAttributes&lt;/method&gt;
                    &lt;/atwix_productoptions&gt;
                &lt;/observers&gt;
            &lt;/catalog_product_collection_load_after&gt;
        &lt;/events&gt;
    &lt;/frontend&gt;
&lt;/config&gt;</pre>
<p>Observer:</p>
<pre class="brush: php; gutter: true">&lt;?php
/* /app/code/local/Atwix/ProductOptions/Model/Observer.php */
class Atwix_ProductOptions_Model_Observer
{
    /**
     * Checks if needed collection catched and passes it to Storage Model to store options
     * @param $observer
     * @return $this
     */
    public function storeAttributes($observer)
    {
        $collection = $observer-&gt;getCollection();
        if ($collection instanceof Mage_Catalog_Model_Resource_Product_Type_Configurable_Product_Collection)
        {
            $storageModel = Mage::getSingleton(&#039;atwix_productoptions/storage&#039;);
            $storageModel-&gt;storeData($collection);
        }
        return $this;
    }
}</pre>
<p>Model used to store options:</p>
<pre class="brush: php; gutter: true">&lt;?php
/* /app/code/local/Atwix/ProductOptions/Model/Storage.php */
class Atwix_ProductOptions_Model_Storage extends Mage_Core_Model_Abstract
{
    /**
     * options will be stored in this array 
     * @var null|array
     */
    protected $_attributes = null;

    /**
     * options labels
     * @var null|array
     */
    protected $_options = null;

    /**
     * Puts Color options to $_attributes array 
     * @param $collection
     * @return $this
     */
    public function storeData($collection)
    {
        foreach($collection-&gt;getItems() as $item){
            if ($color = $item-&gt;getColor()){
                $this-&gt;_attributes[$item-&gt;getParentId()][&#039;color&#039;][$color] = $this-&gt;getOptionLabel(&#039;color&#039;, $color);
            }
        }
        return $this;
    }

    /**
     * Retrieves option label by attribute name and value
     * stores all labels in array on first calling 
     * @param $attrName
     * @param $value
     * @return mixed
     */
    public function getOptionLabel($attrName, $value){
        if (!isset($this-&gt;_options[$attrName])){
            $attribute = Mage::getModel(&#039;eav/config&#039;)-&gt;getAttribute(&#039;catalog_product&#039;, $attrName);
            $optArray = $attribute-&gt;getSource()-&gt;getAllOptions(false);
            foreach($optArray as $item){
                $this-&gt;_options[$attrName][$item[&#039;value&#039;]] = $item[&#039;label&#039;];
            }
        }
        return $this-&gt;_options[$attrName][$value];
    }

    /**
     * Returns options for products 
     * @param product id $id  
     * @param $code
     * @return bool|array
     */
    public function getOptions($id, $code)
    {
        if (isset($this-&gt;_attributes[$id][$code])){
            return $this-&gt;_attributes[$id][$code];
        }
        return false;
    }
}</pre>
<p>Helper class:</p>
<pre class="brush: php; gutter: true">&lt;?php
/* /app/code/local/Atwix/ProductOptions/Helper/Data.php */
class Atwix_ProductOptions_Helper_Data extends Mage_Core_Helper_Abstract
{
    protected $_storageModel = null;

    public function  __construct()
    {
        $this-&gt;_storageModel = Mage::getSingleton(&#039;atwix_productoptions/storage&#039;);
    }

    /**
     * gets options from storage and generates string
     * @param $id
     * @param $code
     * @return string
     */
    public function getConfigurableProductAttributeOptions($id, $code)
    {
        $values = $this-&gt;_storageModel-&gt;getOptions($id, $code);
        if ($values){
            return implode(&#039;, &#039;,$values);
        }
        return &#039;&#039;;
    }
}</pre>
<p>And finally, if you place the following line:</p>
<pre class="brush: php; gutter: true">&lt;b&gt;&lt;?php echo Mage::helper(&#039;atwix_productoptions&#039;)-&gt;getConfigurableProductAttributeOptions($_product-&gt;getId(), &#039;color&#039;); ?&gt;&lt;/b&gt;&lt;/br&gt;</pre>
<p>to your template file (in our case it is <em>app/design/frontend/base/default/template/catalog/product/list.phtml</em>) somewhere after the construction:</p>
<pre class="brush: php; gutter: true">&lt;?php if($_product-&gt;isSaleable()): ?&gt;</pre>
<p>you will get something like this:<br />
<a href="http://www.atwix.com/magento/configurable-product-options-category-view/attachment/magento_product_options_category_view/" rel="attachment wp-att-2868"><img class="aligncenter size-full wp-image-2868" alt="Magento product options - category view" src="http://www.atwix.com/wp-content/uploads/2013/04/Magento_product_options_category_view.png" width="494" height="837" /></a><br />
The complete module code can be found <a href="https://github.com/vovsky/ProductOptions">here</a>. If you know better/alternative way of getting configurable product options in the product list, you&#8217;re welcome to share your knowledge in the comments below.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.atwix.com/magento/configurable-product-options-category-view/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Customizing Magento System Configuration. Part 2</title>
		<link>http://www.atwix.com/magento/customize-system-settings/</link>
		<comments>http://www.atwix.com/magento/customize-system-settings/#comments</comments>
		<pubDate>Mon, 13 May 2013 10:28:40 +0000</pubDate>
		<dc:creator>Sergiy Vasyutynsky</dc:creator>
				<category><![CDATA[Magento]]></category>

		<guid isPermaLink="false">http://www.atwix.com/?p=2686</guid>
		<description><![CDATA[We gladly present the continuation of the article on Customizing Magento System Configuration. In this post,  you will learn how to customize tabs and sections, groups and fields without using system.xml.]]></description>
				<content:encoded><![CDATA[<p>From the <a title="Customizing Magento System Configuration. Part 1" href="http://www.atwix.com/magento/customizing-system-configuration-part-1/">previous article</a>, we&#8217;ve learned how to change Magento System Configuration using the system configuration file. Today we will customize tabs and sections, groups and fields without using system.xml.<br />
<span id="more-2686"></span></p>
<p>Certainly, we know that Magento has a great event system and this knowledge will help us to make changes in the System Configuration.<br />
For our purposes, we will create a new module and call it &#8216;SystemConfig&#8217;. It consists of three files. First of them is for initialing our module:<br />
<em><strong>app/etc/modules/Atwix_SystemConfig.xml</strong></em></p>
<pre class="brush: xml; gutter: true">&lt;?xml version=&quot;1.0&quot;?&gt;
&lt;config&gt;
    &lt;modules&gt;
        &lt;Atwix_SystemConfig&gt;
            &lt;active&gt;true&lt;/active&gt;
            &lt;codePool&gt;local&lt;/codePool&gt;
        &lt;/Atwix_SystemConfig&gt;
    &lt;/modules&gt;
&lt;/config&gt;</pre>
<p>Afterwards, we need to create module configuration file. That is the second one:</p>
<p><em><strong>app/code/local/Atwix/SystemConfig/etc/config.xml</strong></em></p>
<pre class="brush: xml; gutter: true">&lt;?xml version=&quot;1.0&quot;?&gt;
&lt;config&gt;
    &lt;modules&gt;
        &lt;Atwix_SystemConfig&gt;
            &lt;version&gt;1.0.0&lt;/version&gt;
        &lt;/Atwix_SystemConfig&gt;
    &lt;/modules&gt;
    &lt;global&gt;
        &lt;models&gt;
            &lt;atwix_systemconfig&gt;
                &lt;class&gt;Atwix_SystemConfig_Model&lt;/class&gt;
            &lt;/atwix_systemconfig&gt;
        &lt;/models&gt;
        &lt;events&gt;
            &lt;adminhtml_init_system_config&gt;
                &lt;observers&gt;
                    &lt;atwix_init_system_config&gt;
                        &lt;class&gt;atwix_systemconfig/observer&lt;/class&gt;
                        &lt;method&gt;changeSystemConfig&lt;/method&gt;
                    &lt;/atwix_init_system_config&gt;
                &lt;/observers&gt;
            &lt;/adminhtml_init_system_config&gt;
        &lt;/events&gt;
    &lt;/global&gt;
&lt;/config&gt;</pre>
<p>However, let’s check what we have now. So, in the configuration file we define model, configure event, and also, the event &#8216;adminhtml_init_system_config&#8217; which makes possible to work with System Configuration data.</p>
<p>And, the third  one, the last file &#8211; it is an observer, where we do all the magic:<br />
<em><strong>app/code/local/Atwix/SystemConfig/Model/Observer.php</strong></em></p>
<pre class="brush: php; gutter: true">&lt;?php

class Atwix_SystemConfig_Model_Observer
{
    public function changeSystemConfig(Varien_Event_Observer $observe)
    {
        $config = $observe-&gt;getConfig();

        return $this;
    }
}</pre>
<p>Okey, we put debug breakpoint to the line with the return statement and have a good inspect of the picture being used:</p>
<p style="text-align: center;"><img class="aligncenter  wp-image-2690" alt="inspect" src="http://www.atwix.com/wp-content/uploads/2013/03/inspect.jpg" width="671" height="421" /></p>
<p>Check this out!<br />
In the config, there are all the tabs and sections, and in the section – groups and fields, but what about to try some changes. For example, let&#8217;s change sort order and label for &#8216;advanced&#8217; tab and add a comment to the &#8216;Startup Page&#8217; field.</p>
<p>Thuswise, at first &#8211; change code in the observer:</p>
<p><em><strong>app/code/local/Atwix/SystemConfig/Model/Observer.php</strong></em></p>
<pre class="brush: php; gutter: true">&lt;?php

class Atwix_SystemConfig_Model_Observer
{
    public function changeSystemConfig(Varien_Event_Observer $observer)
    {
        //get init sections and tabs
        $config = $observer-&gt;getConfig();

        //get tab &#039;advanced&#039;, change sort order and label
        $advancedTab = $config-&gt;getNode(&#039;tabs/advanced&#039;);
        $advancedTab-&gt;sort_order = 1;
        $advancedTab-&gt;label .= &#039; (on top)&#039;;

        //get field &#039;page&#039;, add comment
        $config-&gt;getNode(&#039;sections/admin/groups/startup/fields/page&#039;)-&gt;comment = &#039;after successful login you will see this page&#039;;

        return $this;
    }
}</pre>
<p style="text-align: center;"><img class="wp-image-2691 aligncenter" alt="first_example" src="http://www.atwix.com/wp-content/uploads/2013/03/first_example.jpg" width="693" height="288" /></p>
<p style="text-align: left;">Great, advanced tab on the top!<br />
After what, it is possible to make a lot of changes.<br />
Moreover, we can also create tabs and section, group and field, besides that, using Mage_Core_Model_Config_Element class we are able to work with elements: create, edit, append, extend, etc. And the next example shows us how to add a new group with the fields to the &#8216;admin&#8217; section. In this case, the observer should have the following code:</p>
<p style="text-align: left;"><em><strong>app/code/local/Atwix/SystemConfig/Model/Observer.php</strong></em></p>
<pre class="brush: php; gutter: true">&lt;?php

class Atwix_SystemConfig_Model_Observer
{
    public function changeSystemConfig(Varien_Event_Observer $observer)
    {
        //get init sections and tabs
        $config = $observer-&gt;getConfig();

        //get tab &#039;advanced&#039;, change sort order and label
        $advancedTab = $config-&gt;getNode(&#039;tabs/advanced&#039;);
        $advancedTab-&gt;sort_order = 1;
        $advancedTab-&gt;label .= &#039; (on top)&#039;;

        //get field &#039;page&#039;, add comment
        $config-&gt;getNode(&#039;sections/admin/groups/startup/fields/page&#039;)-&gt;comment = &#039;after successful login you will see this page&#039;;

        //add new group with fields in section &#039;admin&#039;
        $adminSectionGroups = $config-&gt;getNode(&#039;sections/admin/groups&#039;);
        $new_group_xml = new Mage_Core_Model_Config_Element(&#039;
            &lt;atwix_new_group&gt;
                &lt;label&gt;New Group&lt;/label&gt;
                &lt;sort_order&gt;99&lt;/sort_order&gt;
                &lt;show_in_default&gt;1&lt;/show_in_default&gt;
                &lt;show_in_website&gt;1&lt;/show_in_website&gt;
                &lt;show_in_store&gt;1&lt;/show_in_store&gt;
                &lt;fields&gt;
                        &lt;disable_message&gt;
                            &lt;label&gt;Disable Message&lt;/label&gt;
                            &lt;frontend_type&gt;select&lt;/frontend_type&gt;
                            &lt;source_model&gt;adminhtml/system_config_source_yesno&lt;/source_model&gt;
                            &lt;sort_order&gt;10&lt;/sort_order&gt;
                            &lt;show_in_default&gt;1&lt;/show_in_default&gt;
                            &lt;show_in_website&gt;1&lt;/show_in_website&gt;
                            &lt;show_in_store&gt;1&lt;/show_in_store&gt;
                        &lt;/disable_message&gt;
                        &lt;message&gt;
                            &lt;label&gt;Message&lt;/label&gt;
                            &lt;frontend_type&gt;text&lt;/frontend_type&gt;
                            &lt;sort_order&gt;20&lt;/sort_order&gt;
                            &lt;show_in_default&gt;1&lt;/show_in_default&gt;
                            &lt;show_in_website&gt;1&lt;/show_in_website&gt;
                            &lt;show_in_store&gt;1&lt;/show_in_store&gt;
                            &lt;depends&gt;
                                &lt;disable_message&gt;0&lt;/disable_message&gt;
                            &lt;/depends&gt;
                        &lt;/message&gt;
                &lt;/fields&gt;
            &lt;/atwix_new_group&gt;
        &#039;);
        $adminSectionGroups-&gt;appendChild($new_group_xml);

        return $this;
    }
}</pre>
<p style="text-align: center;"><img class="aligncenter  wp-image-2689" alt="second_example" src="http://www.atwix.com/wp-content/uploads/2013/03/second_example.jpg" width="694" height="313" /></p>
<p>But, if we wish it is possible to implement a lot of things and reinvent the wheel =)</p>
<p>At the same time, it will be highly helpful to make some changes in the System Configuration while using the observer.</p>
<p>You&#8217;re always welcome to leave your comments below if you found this post helpful! Thanks for reading our blog!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.atwix.com/magento/customize-system-settings/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Prevent JS and CSS caching during deployment</title>
		<link>http://www.atwix.com/magento/prevent-js-and-css-caching-during-deployment/</link>
		<comments>http://www.atwix.com/magento/prevent-js-and-css-caching-during-deployment/#comments</comments>
		<pubDate>Thu, 25 Apr 2013 08:31:51 +0000</pubDate>
		<dc:creator>Nick Kravchuk</dc:creator>
				<category><![CDATA[Magento]]></category>
		<category><![CDATA[cache]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[head]]></category>
		<category><![CDATA[js]]></category>
		<category><![CDATA[layout]]></category>
		<category><![CDATA[theme]]></category>

		<guid isPermaLink="false">http://www.atwix.com/?p=2821</guid>
		<description><![CDATA[This article includes useful tips on how to prevent JS and CSS caching during deployment.]]></description>
				<content:encoded><![CDATA[<p>Hi guys, as you know, while moving changes to the production server very often we need to flush CSS or JS cache on the client&#8217;s side, as without this action clients will see broken design or, in case with JS cache, might get broken functionality. Therefore, we guess it is a good idea to describe how to prevent JS/CSS caching.<br />
<span id="more-2821"></span><br />
In this case, the simplest solution for this challenge is file renaming, but this way is not very nice, so we would like to find out the alternative solution.</p>
<p>When we get deal with JS file it is only needed to add after file name the following chars &#8220;?1&#8243;, look through example:</p>
<pre class="brush: xml; gutter: true">&lt;reference name=&quot;head&quot;&gt;
    &lt;action method=&quot;addJs&quot;&gt;&lt;script&gt;atwix/test.js?1&lt;/script&gt;&lt;/action&gt;
&lt;/reference&gt;</pre>
<p>But we can&#8217;t apply this way for CSS cache, as Magento doesn&#8217;t want to include CSS file described that way (since Magento only checks for file existing but can&#8217;t find the file with regular file name plus extra symbols).</p>
<p>That&#8217;s why it is necessary to find how to implement it without file renaming. The first way can be enabling merging of CSS. Then, you just clear the cache and a new merged file will be created with a new file name (this ability can be found here: System -&gt; Configuration -&gt; Developer -&gt; CSS settings -&gt; Merge CSS Files ).  But as far as we know, the hash code of the merged CSS file stays the same even if the underlying files changed &#8211; but only when the new files are added to the set of merged files, the hash changes. So, that way is not applicable especially you just changed existing file.</p>
<p>Let&#8217;s check another way &#8211; it is when instead of using layout.xml, we just put them in our <code>page/html/head.phtml</code> or, create a block that contains the <code>&lt;style&gt;</code> tag with version number and put it in the XML in our head &#8211; so we can have it load in only specific pages and still sticking to using the XML layouts. We guess that it&#8217;s not very helpful either, so we have found one more solution. We need to create our own functionality that allows to include the CSS file with revision number. First of all, we should create block, thus you can check the following code:</p>
<pre class="brush: php; gutter: true">class Atwix_Css_Block_Test extends Mage_Core_Block_Text
{
    /** @var string Current theme url */
    private $_url;

    /**
     * Get current theme url
     */
    public function _construct()
    {
        $this-&gt;_url = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_SKIN) .
            &#039;frontend&#039; . DS .
            Mage::getSingleton(&#039;core/design_package&#039;)-&gt;getPackageName() . DS .
            Mage::getDesign()-&gt;getTheme(&#039;frontend&#039;) . DS;
    }

    /**
     * Generate code that is needed to insert into head block
     * 
     * @param $file string file name
     */
    public function setCss($file)
    {
        $this-&gt;setText(&#039;&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;&#039; . $this-&gt;_url . $file . &#039;&quot; media=&quot;all&quot;/&gt;&#039;);
    }
}</pre>
<p>then, you should use the functionality of this block in your layout, see the following code as well:</p>
<pre class="brush: xml; gutter: true">&lt;reference name=&quot;head&quot;&gt;
    &lt;block type=&quot;atwixcss/test&quot; name=&quot;test&quot;&gt;
        &lt;action method=&quot;setCss&quot;&gt;
            &lt;css&gt;&lt;![CDATA[atwix/test.css?1]]&gt;&lt;/css&gt;
        &lt;/action&gt;
    &lt;/block&gt;
&lt;/reference&gt;</pre>
<p>That&#8217;s it, after all steps you can just change the revision number when your CSS/JS-file has been edited.<br />
At the same time, you are able to use different modules that allow to rule CSS/JS files, in this case, you will find them on the Magento website.</p>
<p>Hope this article will be helpful. Good luck!</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.atwix.com/magento/prevent-js-and-css-caching-during-deployment/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Headers already sent. How to find the reason quickly</title>
		<link>http://www.atwix.com/magento/headers-already-sent/</link>
		<comments>http://www.atwix.com/magento/headers-already-sent/#comments</comments>
		<pubDate>Thu, 11 Apr 2013 08:31:55 +0000</pubDate>
		<dc:creator>Yaroslav Rogoza</dc:creator>
				<category><![CDATA[Magento]]></category>
		<category><![CDATA[core]]></category>
		<category><![CDATA[error]]></category>
		<category><![CDATA[log]]></category>
		<category><![CDATA[magento]]></category>

		<guid isPermaLink="false">http://www.atwix.com/?p=2794</guid>
		<description><![CDATA[Let's find out how to resolve 'headers already sent' warning in Magento fast.]]></description>
				<content:encoded><![CDATA[<p dir="ltr">If you look inside of your Magento system.log from time to time, you will notice an error there like the following one:</p>
<pre class="brush: shell; gutter: false">2011-01-12T14:16:52+00:00 DEBUG (7): HEADERS ALREADY SENT:

Call stack is here..</pre>
<p><b><b> </b></b></p>
<p dir="ltr">As you may already know, it can take a lot of time to find the place where the issues like this one appeared. So, let&#8217;s investigate why it happens and what we can do to speed up the bug fixing process.</p>
<p><span id="more-2794"></span></p>
<p dir="ltr">PHP has such definition as session. Session, it’s a global data which stores only on the server&#8217;s side. In other words, it’s a small temporary ‘storage’ with a predefined lifetime which contains data available globally for PHP scripts on the server side. Session provides opportunity to save an information which is required between requests from client to server. For example, if customer is logged in, his information (name, email, group etc..) stores in the session and becomes available between different scripts.</p>
<p dir="ltr">To get/set the session&#8217;s data PHP uses global variable $_SESSION by default. Actually, $_SESSION it’s an array, so you can access the data via array indexes i.e $_SESSION[‘customer’], $_SESSION[‘customer’][‘email’] etc.. PHP sessions have one singularity: you need to ‘start’ the session for each request to the server to initialize a current session and make $_SESSION variable available. For this purpose, PHP has built in function <em>session_start()</em>. However, you can’t initialize the session everywhere you want. You have to do it only before the server send some data to the client&#8217;s browser. Practically, you need to start the session before <em>echo()</em>, <em>print()</em>, <em>readfile()</em> and other output functions. If you don’t follow that rule your session won’t be initialized and, in addition, you’ll have PHP notice that says about sent headers.</p>
<p dir="ltr">Thereby, we are getting messages in Magento system.log about sent headers because of incorrect session initialization described above. The problem is the place of <em>echo()</em> might be everywhere and finding the place which is causing that error might cost you a whole day. Fortunately, Magento has a controller responsible for sending responses to a client side. As you may guess, we can use this controller to collect additional data that will help us to find the error’s place.</p>
<p dir="ltr">Let’s do a bad thing: make changes directly in the core file. Since it’s just a temporary change, we don’t have to create a copy of that file in the local codepool or create our own extension. Just modify the file directly and don’t forget to remove the changes after the debug process will be finished.</p>
<p dir="ltr">Open the following file with favourite text editor of yours: app/code/local/Mage/Core/Controller/Response/Http.php</p>
<p dir="ltr">and find the line says:</p>
<pre class="brush: php; gutter: true">Mage::log(&#039;HEADERS ALREADY SENT: &#039;.mageDebugBacktrace(true, true, true));</pre>
<p dir="ltr">Then, insert the following line before the previous one:</p>
<pre class="brush: php; gutter: true">Mage::Log(print_r(get_included_files(),true));</pre>
<p dir="ltr">Save the file and move forward.</p>
<p dir="ltr">As you have already guessed, that change allows you to view the list of included files in the log. So, now you can open each of these files and search for <em>echo()</em>, <em>print()</em>, <em>readfile()</em> and other output functions.</p>
<p>The last thing: do not forget to roll back your changes after you’ll be finished. Otherwise you&#8217;ll be wondered how big the logs might be <img src='http://www.atwix.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.atwix.com/magento/headers-already-sent/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Customizing Magento System Configuration. Part 1</title>
		<link>http://www.atwix.com/magento/customizing-system-configuration-part-1/</link>
		<comments>http://www.atwix.com/magento/customizing-system-configuration-part-1/#comments</comments>
		<pubDate>Tue, 09 Apr 2013 08:19:59 +0000</pubDate>
		<dc:creator>Sergiy Vasyutynsky</dc:creator>
				<category><![CDATA[Magento]]></category>

		<guid isPermaLink="false">http://www.atwix.com/?p=2772</guid>
		<description><![CDATA[You’re welcome to check out our following selection about customizing Magento system configuration.]]></description>
				<content:encoded><![CDATA[<p>After reading Atwix blog you should know a lot about <a title="Magento system settings overview" href="http://www.atwix.com/magento/system-settings/" target="_blank">Magento System Configuration</a>, especially how to create tabs and sections, groups and fields, etc. In this article we&#8217;ve described how to customize all this things in the configuration file<del></del>.</p>
<p><span id="more-2772"></span> <img title="More..." alt="" src="http://www.atwix.com/wp-includes/js/tinymce/plugins/wordpress/img/trans.gif" /></p>
<p>As you should already know, for creating own tab, section, group or field we need to use the system.xml file. So, let’s try to customize System Configuration elements in it.</p>
<p>For example, we need to do the following steps:</p>
<p>1. Tab &#8216;Catalog&#8217; – move to the top.<br />
2. Section &#8216;General&#8217; – change a label in &#8216;General Information&#8217;.<br />
3. Group &#8216;Store Information&#8217; (section &#8216;General&#8217;) – add a new field &#8216;Show VAT Number&#8217; (dropdown yes/no).<br />
4. Fields &#8216;VAT Number&#8217; and &#8216;Validate VAT Number&#8217; – add them dependence on the &#8216;Show VAT Number&#8217; field.<br />
5. Section &#8216;Moneybookers&#8217; &#8211; change a label in &#8216;Phoenix Moneybookers&#8217;.</p>
<p>Now, create a new module &#8217;SystemConfig&#8217; for experiments, here is the initial and module configuration files:</p>
<p><em><strong>app/etc/modules/Atwix_SystemConfig.xml</strong></em></p>
<pre class="brush: xml; gutter: true">&lt;?xml version=&quot;1.0&quot;?&gt;
&lt;config&gt;
    &lt;modules&gt;
        &lt;Atwix_SystemConfig&gt;
            &lt;active&gt;true&lt;/active&gt;
            &lt;codePool&gt;local&lt;/codePool&gt;
        &lt;/Atwix_SystemConfig&gt;
    &lt;/modules&gt;
&lt;/config&gt;</pre>
<p><em><strong>app/code/local/Atwix/SystemConfig/etc/config.xml</strong></em></p>
<pre class="brush: xml; gutter: true">&lt;?xml version=&quot;1.0&quot;?&gt;
&lt;config&gt;
    &lt;modules&gt;
        &lt;Atwix_SystemConfig&gt;
            &lt;version&gt;1.0.0&lt;/version&gt;
        &lt;/Atwix_SystemConfig&gt;
    &lt;/modules&gt;
&lt;/config&gt;</pre>
<p>Also, note please &#8211; in the system configuration file we should put nodes of those elements (tabs, sections, groups, fields) that are also needed to be changed:</p>
<p><em><strong>app/code/local/Atwix/SystemConfig/etc/system.xml</strong></em></p>
<pre class="brush: xml; gutter: true">&lt;?xml version=&quot;1.0&quot;?&gt;
&lt;config&gt;
    &lt;tabs&gt;
        &lt;!-- 1 --&gt;
        &lt;catalog&gt;
            &lt;sort_order&gt;1&lt;/sort_order&gt;
        &lt;/catalog&gt;
    &lt;/tabs&gt;

    &lt;sections&gt;
        &lt;general&gt;
            &lt;!-- 2 --&gt;
            &lt;label&gt;General Information&lt;/label&gt;
            &lt;groups&gt;
                &lt;store_information&gt;
                    &lt;fields&gt;
                        &lt;!-- 3 --&gt;
                        &lt;show_vat&gt;
                            &lt;label&gt;Show VAT Number&lt;/label&gt;
                            &lt;frontend_type&gt;select&lt;/frontend_type&gt;
                            &lt;source_model&gt;adminhtml/system_config_source_yesno&lt;/source_model&gt;
                            &lt;sort_order&gt;26&lt;/sort_order&gt;
                            &lt;show_in_default&gt;1&lt;/show_in_default&gt;
                            &lt;show_in_website&gt;1&lt;/show_in_website&gt;
                            &lt;show_in_store&gt;1&lt;/show_in_store&gt;
                        &lt;/show_vat&gt;
                        &lt;!-- 4 --&gt;
                        &lt;merchant_vat_number&gt;
                            &lt;depends&gt;
                                &lt;show_vat&gt;1&lt;/show_vat&gt;
                            &lt;/depends&gt;
                        &lt;/merchant_vat_number&gt;
                        &lt;validate_vat_number&gt;
                            &lt;depends&gt;
                                &lt;show_vat&gt;1&lt;/show_vat&gt;
                            &lt;/depends&gt;
                        &lt;/validate_vat_number&gt;
                    &lt;/fields&gt;
                &lt;/store_information&gt;
            &lt;/groups&gt;
        &lt;/general&gt;
        &lt;!--5--&gt;
        &lt;moneybookers&gt;
            &lt;label&gt;Phoenix Moneybookers&lt;/label&gt;
        &lt;/moneybookers&gt;
    &lt;/sections&gt;
&lt;/config&gt;</pre>
<p>After all, let&#8217;s check what we have:</p>
<p style="text-align: center;"><img class="aligncenter  wp-image-2785" alt="example" src="http://www.atwix.com/wp-content/uploads/2013/03/example.jpg" width="690" height="266" /></p>
<p style="text-align: center;"><img class="aligncenter  wp-image-2827" alt="example2" src="http://www.atwix.com/wp-content/uploads/2013/04/example2.png" width="759" height="27" /></p>
<p>As you can see, everything works fine except the last task, what means that in the &#8216;Moneybookers&#8217; section the label wasn&#8217;t changed. Besides that, we did not get the desired result, because system configuration file <em>app\code\community\Phoenix\Moneybookers\etc\system.xml</em> has loaded only after our file <em>app/code/local/Atwix/SystemConfig/etc/system.xml </em> and our values were overwritten. Therefore, for solving this problem we can choose one of the following options:</p>
<p>1. First, add &#8216;depends&#8217; function to our initial configuration files:</p>
<p><em><strong>app/etc/modules/Atwix_SystemConfig.xml</strong></em></p>
<pre class="brush: xml; gutter: true">&lt;?xml version=&quot;1.0&quot;?&gt;
&lt;config&gt;
    &lt;modules&gt;
        &lt;Atwix_SystemConfig&gt;
            &lt;active&gt;true&lt;/active&gt;
            &lt;codePool&gt;local&lt;/codePool&gt;
            &lt;depends&gt;
                &lt;Phoenix_Moneybookers/&gt;
            &lt;/depends&gt;
        &lt;/Atwix_SystemConfig&gt;
    &lt;/modules&gt;
&lt;/config&gt;</pre>
<p>2. Or, you can create a new module with namespace that will take position after Phoenix namespace in alphabetical order.<br />
3. Make change using the observer.<br />
This post might help if you`ll face the similar situation. However, if you have any ideas or suggestions we would be glad to accept them. Comments are most welcome!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.atwix.com/magento/customizing-system-configuration-part-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to create custom router</title>
		<link>http://www.atwix.com/magento/create-custom-router/</link>
		<comments>http://www.atwix.com/magento/create-custom-router/#comments</comments>
		<pubDate>Wed, 03 Apr 2013 08:07:54 +0000</pubDate>
		<dc:creator>Nick Kravchuk</dc:creator>
				<category><![CDATA[Magento]]></category>
		<category><![CDATA[controller]]></category>
		<category><![CDATA[route]]></category>
		<category><![CDATA[router]]></category>
		<category><![CDATA[url]]></category>

		<guid isPermaLink="false">http://www.atwix.com/?p=2677</guid>
		<description><![CDATA[One more post from our team with helpful tips on how to create custom router.]]></description>
				<content:encoded><![CDATA[<p>Greeting! It&#8217;s time to share our knowledge on how to create custom router, as many of us use the standard router. You should know that Magento has four types of the routers: admin, standard, cms and default &#8211; they are loading in the order we&#8217;ve described. But, have you ever faced with such configuration?<span id="more-2677"></span></p>
<pre class="brush: xml; gutter: true">&lt;routers&gt;
    &lt;atwixtest&gt;
        &lt;use&gt;standard&lt;/use&gt;
        &lt;args&gt;
            &lt;module&gt;Atwix_Test&lt;/module&gt;
            &lt;frontName&gt;test-frontname&lt;/frontName&gt;
        &lt;/args&gt;
    &lt;/atwixtest&gt;
&lt;/routers&gt;</pre>
<p>Yes, if you look to  &lt;use&gt;standard&lt;/use&gt;  code, that means &#8211; Magento will use the standard router for processing requests and URLs,  and when you follow magento.dev/test-frontname URL, in this case, Magento says for the standard router that it should take action indexAction from IndexController from module Atwix_Test &#8211; it is not so complicated, we guess. Actually, when the requests come to Magento, then it calls all routers till one of them will response &#8211; otherwise, there 404 page will be displayed.</p>
<p>Now, let&#8217;s start creating own router. First of all, we need to declare router in config.xml:</p>
<pre class="brush: xml; gutter: true">&lt;default&gt;
    &lt;web&gt;
        &lt;routers&gt;
            &lt;atwixtest&gt;
                &lt;area&gt;frontend&lt;/area&gt;
                &lt;class&gt;Atwix_Test_Controller_Router&lt;/class&gt;
            &lt;/atwixtest&gt;
        &lt;/routers&gt;
    &lt;/web&gt;
&lt;/default&gt;</pre>
<p>After this, we should create file that will contain class  Atwix_Test_Controller_Router app/code/local/Atwix/Test/Controller/Router.php:</p>
<pre class="brush: php; gutter: true">class Atwix_Test_Controller_Router extends Mage_Core_Controller_Varien_Router_Standard
{
    public function match(Zend_Controller_Request_Http $request)
    {
        $request-&gt;setModuleName(&#039;test-frontname&#039;)
            -&gt;setControllerName(&#039;index&#039;)
            -&gt;setActionName(&#039;index&#039;);

        return true;
    }

}</pre>
<p>The previous code explains Magento that it should transmit control to the indexAction function which is placed in app/code/local/Atwix/Test/controllers/IndexController.php., and your router will be called after admin and standard, but if these routers do not respond for the requests &#8211; then your router will be called.</p>
<p>So, now you can check request, proceed it and show page you want  before Magento will throw &#8220;Not found&#8221; page. Hope that simple tutorial will be useful. We are waiting for your comments and happy, non-crappy coding!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.atwix.com/magento/create-custom-router/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to insert your block into any place in Magento</title>
		<link>http://www.atwix.com/magento/insert-blocks/</link>
		<comments>http://www.atwix.com/magento/insert-blocks/#comments</comments>
		<pubDate>Tue, 05 Mar 2013 10:22:53 +0000</pubDate>
		<dc:creator>Nick Kravchuk</dc:creator>
				<category><![CDATA[Magento]]></category>
		<category><![CDATA[blocks]]></category>
		<category><![CDATA[layout]]></category>
		<category><![CDATA[magento]]></category>
		<category><![CDATA[observer]]></category>

		<guid isPermaLink="false">http://www.atwix.com/?p=2614</guid>
		<description><![CDATA[Our experience on how to insert the block into any place in Magento is shared in this instructive article.]]></description>
				<content:encoded><![CDATA[<p>Hello guys, it&#8217;s time to tell you how to insert block into any place you want in Magento. As you know, very often we need to set some block into selected weird place without editing template, so we&#8217;ll try to describe the way how to do this using layouts and observers.<span id="more-2614"></span>In our case, for such experiments will be used category page.<br />
Let&#8217;s say, you need to place your block inside the other one and this block is instance of the Mage_Core_Block_Text_List class. That means, the block renders his children automatically, what&#8217;s easy. Then, you just need to follow the steps which are below:</p>
<pre class="brush: xml; gutter: true">&lt;reference name=&quot;left&quot;&gt;
    &lt;block type=&quot;core/template&quot; name=&quot;test&quot; template=&quot;at.phtml&quot;&gt;&lt;/block&gt;
&lt;/reference&gt;</pre>
<p>This block will be displayed after all content of the &#8220;left<strong>&#8220;</strong> block. But, what if we need it to be displayed before all content &#8211; just add the &#8220;before&#8221; directive. See below such example:</p>
<pre class="brush: xml; gutter: true">&lt;reference name=&quot;left&quot;&gt;
    &lt;block type=&quot;core/template&quot; name=&quot;test&quot; template=&quot;at.phtml&quot; before=&quot;-&quot;&gt;&lt;/block&gt;
&lt;/reference&gt;</pre>
<p>In case, you need to place your block between first level children of the &#8220;left&#8221; one, you should add before=&#8221;name&#8221; or after=&#8221;name&#8221;, and check following code:</p>
<pre class="brush: xml; gutter: true">&lt;reference name=&quot;left&quot;&gt;
    &lt;block type=&quot;core/template&quot; name=&quot;test&quot; template=&quot;at.phtml&quot; after=&quot;tags_popular&quot;&gt;&lt;/block&gt;
&lt;/reference&gt;</pre>
<p><a href="http://www.atwix.com/magento/insert-blocks/attachment/2013-02-25_1851/" rel="attachment wp-att-2620"><img class="alignnone size-full wp-image-2620" alt="2013-02-25_1851" src="http://www.atwix.com/wp-content/uploads/2013/02/2013-02-25_1851.png" width="402" height="307" /></a></p>
<p>&nbsp;</p>
<p>That was pretty easy, but when we deal with none first level blocks &#8211; supposing, it is necessary to insert the block after price on the category page, and for such implementation you should use the observer <strong>core_block_abstract_to_html_before</strong>. Besides that, don&#8217;t forget to declare model in your config.xml:</p>
<pre class="brush: xml; gutter: true">&lt;?xml version=&quot;1.0&quot;?&gt;
&lt;config&gt;
    &lt;global&gt;
    &lt;!-- ... --&gt;
        &lt;models&gt; 
            &lt;atwix_test&gt;
                &lt;class&gt;Atwix_Test_Model&lt;/class&gt;
            &lt;/atwix_test&gt;
        &lt;/models&gt;
    &lt;/global&gt;
    &lt;frontend&gt;
        &lt;events&gt;
            &lt;core_block_abstract_to_html_before&gt;
                &lt;observers&gt;
                    &lt;atwix_test&gt;
                        &lt;type&gt;model&lt;/type&gt;
                        &lt;class&gt;atwix_test/observer&lt;/class&gt;
                        &lt;method&gt;insertBlock&lt;/method&gt;
                    &lt;/atwix_test&gt;
                &lt;/observers&gt;
            &lt;/core_block_abstract_to_html_before&gt;
        &lt;/events&gt;
    &lt;/frontend&gt;
&lt;/config&gt;</pre>
<p>The next step is creating the observer and your template file.<br />
You can place the template to app/design/frontend/&lt;package&gt;/&lt;theme&gt;/at.phtml for example. For this, look at the observer&#8217;s code below:</p>
<pre class="brush: php; gutter: true">&lt;?php

class Atwix_Test_Model_Observer
{
    public function insertBlock($observer)
    {
        /** @var $_block Mage_Core_Block_Abstract */
        /*Get block instance*/
        $_block = $observer-&gt;getBlock();
        /*get Block type*/
        $_type = $_block-&gt;getType();
       /*Check block type*/
        if ($_type == &#039;catalog/product_price&#039;) {
            /*Clone block instance*/
            $_child = clone $_block;
            /*set another type for block*/
            $_child-&gt;setType(&#039;test/block&#039;);
            /*set child for block*/
            $_block-&gt;setChild(&#039;child&#039;, $_child);
            /*set our template*/
            $_block-&gt;setTemplate(&#039;at.phtml&#039;);
        }
    }
}</pre>
<p>And finally, here is a template at.phtml code:</p>
<pre class="brush: php; gutter: true">&lt;?php echo $this-&gt;getChildHtml(&#039;child&#039;) ?&gt;
&lt;h1&gt;Hello&lt;/h1&gt;</pre>
<p>The result is:</p>
<p><a href="http://www.atwix.com/magento/insert-blocks/attachment/2013-02-26_1017/" rel="attachment wp-att-2635"><img class="alignnone size-full wp-image-2635" alt="2013-02-26_1017" src="http://www.atwix.com/wp-content/uploads/2013/02/2013-02-26_1017.png" width="487" height="358" /></a></p>
<p>&nbsp;</p>
<p>That&#8217;s all folks! However, if you know another good way how to insert the blocks, please share your knowledges with us.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.atwix.com/magento/insert-blocks/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Source, frontend and backend models in Magento system settings</title>
		<link>http://www.atwix.com/magento/frontend-backend-source/</link>
		<comments>http://www.atwix.com/magento/frontend-backend-source/#comments</comments>
		<pubDate>Mon, 25 Feb 2013 08:37:40 +0000</pubDate>
		<dc:creator>Yaroslav Rogoza</dc:creator>
				<category><![CDATA[Magento]]></category>
		<category><![CDATA[admin]]></category>
		<category><![CDATA[backend]]></category>
		<category><![CDATA[customize]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[extensions]]></category>
		<category><![CDATA[field]]></category>
		<category><![CDATA[form]]></category>
		<category><![CDATA[magento]]></category>
		<category><![CDATA[settings]]></category>

		<guid isPermaLink="false">http://www.atwix.com/?p=2592</guid>
		<description><![CDATA[You're welcome to check our new finding on source, frontend and backend models in Magento system settings.]]></description>
				<content:encoded><![CDATA[<p>In the previous <a title="article" href="http://www.atwix.com/magento/system-settings/">article</a> we have described how the different settings can be applied to a custom extension. There&#8217;s nothing difficult for the simple settings such as text field or text area. But, if you look at the drop down, multiselect or editable items list you&#8217;ll notice that these items have additional fields, e.g. source_model, backend_model, frontend_model. Let&#8217;s focus on these models and find out more info about creating settings that require data models. <span id="more-2592"></span></p>
<p>Source Model &#8211; a model class that serves to get existing values (stored in the db or somewhere else) for further displaying inside the setting&#8217;s field.</p>
<p>Frontend Model &#8211; as a rule, it&#8217;s a block&#8217;s class. Methods of this class return html of setting&#8217;s field. To be more specific, the block had to have method _getElementHtml() described inside the class which returns the raw html of setting&#8217;s field.</p>
<p>Backend Model &#8211; a class which allows to operate with configuration data on the different stages (save, load). It contains three major methods respectively for each event: _afterLoad(), _beforeSave() and _afterSave().</p>
<p>If you are interested in, you can always find the working examples of each of them in the Magento core itself. But firstly, let&#8217;s try to look on some custom examples of these models. The following part describes how to create a drop down menu with the custom values:</p>
<p>Init setting&#8217;s field in your system.xml:</p>
<pre class="brush: xml; gutter: true">&lt;color&gt;
    &lt;label&gt;Color&lt;/label&gt;
    &lt;frontend_type&gt;select&lt;/frontend_type&gt;
    &lt;source_model&gt;Atwix_Shoppingbar_Model_Adminhtml_System_Config_Source_Color&lt;/source_model&gt;
    &lt;sort_order&gt;2&lt;/sort_order&gt;
    &lt;show_in_default&gt;1&lt;/show_in_default&gt;
    &lt;show_in_website&gt;1&lt;/show_in_website&gt;
    &lt;show_in_store&gt;1&lt;/show_in_store&gt;
&lt;/color&gt;</pre>
<p>Then, create the source model that was specified above:</p>
<pre class="brush: php; gutter: true">class Atwix_Shoppingbar_Model_Adminhtml_System_Config_Source_Color
{
   public function toOptionArray()
   {
       $themes = array(
           array(&#039;value&#039; =&gt; &#039;green&#039;, &#039;label&#039; =&gt; &#039;Green&#039;),
           array(&#039;value&#039; =&gt; &#039;blue&#039;, &#039;label&#039; =&gt; &#039;Blue&#039;),
           array(&#039;value&#039; =&gt; &#039;beige&#039;, &#039;label&#039; =&gt; &#039;Beige&#039;),
       );

       return $themes;
   }
}</pre>
<p>As you can see, there&#8217;s only one method toOptionArray(). It returns an array with the items (value -&gt; label) for the drop down menu. Nothing difficult is here <img src='http://www.atwix.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  Let&#8217;s look onto another example to find out how to use frontend and source models. The best point is to review editable items list setting since it has both. The setting described below operates with the text items. It allows to add/remove email addresses:</p>
<pre class="brush: xml; gutter: true">&lt;addresses&gt;
    &lt;label&gt;Blocked Email Addresses&lt;/label&gt;
    &lt;frontend_model&gt;atwix_emailblocker/adminhtml_addresses&lt;/frontend_model&gt;
    &lt;backend_model&gt;adminhtml/system_config_backend_serialized&lt;/backend_model&gt;
    &lt;sort_order&gt;2&lt;/sort_order&gt;
    &lt;show_in_default&gt;1&lt;/show_in_default&gt;
    &lt;show_in_website&gt;0&lt;/show_in_website&gt;
    &lt;show_in_store&gt;0&lt;/show_in_store&gt;
    &lt;can_be_empty&gt;1&lt;/can_be_empty&gt;
&lt;/addresses&gt;</pre>
<p>Frontend model is a block:</p>
<pre class="brush: php; gutter: true">class Atwix_Emailblocker_Block_Adminhtml_Addresses extends Mage_Adminhtml_Block_System_Config_Form_Field
{
   protected $_addRowButtonHtml = array();
   protected $_removeRowButtonHtml = array();

   /**
    * Returns html part of the setting
    *
    * @param Varien_Data_Form_Element_Abstract $element
    * @return string
    */
   protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element)
   {
       $this-&gt;setElement($element);

       $html = &#039;&lt;div id=&quot;emailblocker_addresses_template&quot; style=&quot;display:none&quot;&gt;&#039;;
       $html .= $this-&gt;_getRowTemplateHtml();
       $html .= &#039;&lt;/div&gt;&#039;;

       $html .= &#039;&lt;ul id=&quot;emailblocker_addresses_container&quot;&gt;&#039;;
       if ($this-&gt;_getValue(&#039;addresses&#039;)) {
           foreach ($this-&gt;_getValue(&#039;addresses&#039;) as $i =&gt; $f) {
               if ($i) {
                   $html .= $this-&gt;_getRowTemplateHtml($i);
               }
           }
       }
       $html .= &#039;&lt;/ul&gt;&#039;;
       $html .= $this-&gt;_getAddRowButtonHtml(&#039;emailblocker_addresses_container&#039;,
           &#039;emailblocker_addresses_template&#039;, $this-&gt;__(&#039;Add New Email&#039;));

       return $html;
   }

   /**
    * Retrieve html template for setting
    *
    * @param int $rowIndex
    * @return string
    */
   protected function _getRowTemplateHtml($rowIndex = 0)
   {
       $html = &#039;&lt;li&gt;&#039;;

       $html .= &#039;&lt;div style=&quot;margin:5px 0 10px;&quot;&gt;&#039;;
       $html .= &#039;&lt;input style=&quot;width:100px;&quot; name=&quot;&#039;
           . $this-&gt;getElement()-&gt;getName() . &#039;[addresses][]&quot; value=&quot;&#039;
           . $this-&gt;_getValue(&#039;addresses/&#039; . $rowIndex) . &#039;&quot; &#039; . $this-&gt;_getDisabled() . &#039;/&gt; &#039;;

       $html .= $this-&gt;_getRemoveRowButtonHtml();
       $html .= &#039;&lt;/div&gt;&#039;;
       $html .= &#039;&lt;/li&gt;&#039;;

       return $html;
   }

   protected function _getDisabled()
   {
       return $this-&gt;getElement()-&gt;getDisabled() ? &#039; disabled&#039; : &#039;&#039;;
   }

   protected function _getValue($key)
   {
       return $this-&gt;getElement()-&gt;getData(&#039;value/&#039; . $key);
   }

   protected function _getSelected($key, $value)
   {
       return $this-&gt;getElement()-&gt;getData(&#039;value/&#039; . $key) == $value ? &#039;selected=&quot;selected&quot;&#039; : &#039;&#039;;
   }

   protected function _getAddRowButtonHtml($container, $template, $title=&#039;Add&#039;)
   {
       if (!isset($this-&gt;_addRowButtonHtml[$container])) {
           $this-&gt;_addRowButtonHtml[$container] = $this-&gt;getLayout()-&gt;createBlock(&#039;adminhtml/widget_button&#039;)
               -&gt;setType(&#039;button&#039;)
               -&gt;setClass(&#039;add &#039; . $this-&gt;_getDisabled())
               -&gt;setLabel($this-&gt;__($title))
               -&gt;setOnClick(&quot;Element.insert($(&#039;&quot; . $container . &quot;&#039;), {bottom: $(&#039;&quot; . $template . &quot;&#039;).innerHTML})&quot;)
               -&gt;setDisabled($this-&gt;_getDisabled())
               -&gt;toHtml();
       }
       return $this-&gt;_addRowButtonHtml[$container];
   }

   protected function _getRemoveRowButtonHtml($selector = &#039;li&#039;, $title = &#039;Delete&#039;)
   {
       if (!$this-&gt;_removeRowButtonHtml) {
           $this-&gt;_removeRowButtonHtml = $this-&gt;getLayout()-&gt;createBlock(&#039;adminhtml/widget_button&#039;)
               -&gt;setType(&#039;button&#039;)
               -&gt;setClass(&#039;delete v-middle &#039; . $this-&gt;_getDisabled())
               -&gt;setLabel($this-&gt;__($title))
               -&gt;setOnClick(&quot;Element.remove($(this).up(&#039;&quot; . $selector . &quot;&#039;))&quot;)
               -&gt;setDisabled($this-&gt;_getDisabled())
               -&gt;toHtml();
       }
       return $this-&gt;_removeRowButtonHtml;
   }
}</pre>
<p>It&#8217;s pretty huge. We have described the full version to show what&#8217;s going on there. You can simple extend your block from Mage_GoogleCheckout_Block_Adminhtml_Shipping_Merchant and override the necessary methods.</p>
<p>Backend model, in this case, it&#8217;s a standard Magento model:</p>
<pre class="brush: php; gutter: true">class Mage_Adminhtml_Model_System_Config_Backend_Serialized extends Mage_Core_Model_Config_Data
{
    protected function _afterLoad()
    {
        if (!is_array($this-&gt;getValue())) {
            $value = $this-&gt;getValue();
            $this-&gt;setValue(empty($value) ? false : unserialize($value));
        }
    }

    protected function _beforeSave()
    {
        if (is_array($this-&gt;getValue())) {
            $this-&gt;setValue(serialize($this-&gt;getValue()));
        }
    }
}</pre>
<p>As you can see, it simply unserializes/serializes field&#8217;s values before load/save respectively. That&#8217;s all. You are always able to improvise with the models described below to get the best result. If you want non-trivial form element &#8211; use your own fronted model. To get available values for settings you should use the source model. And if you need to save/load data in your format or make some other actions on these events &#8211; feel free to use the backend model.<br />
Good luck and graceful solutions <img src='http://www.atwix.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.atwix.com/magento/frontend-backend-source/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Magento system settings overview</title>
		<link>http://www.atwix.com/magento/system-settings/</link>
		<comments>http://www.atwix.com/magento/system-settings/#comments</comments>
		<pubDate>Fri, 08 Feb 2013 08:26:59 +0000</pubDate>
		<dc:creator>Yaroslav Rogoza</dc:creator>
				<category><![CDATA[Magento]]></category>
		<category><![CDATA[admin]]></category>
		<category><![CDATA[backend]]></category>
		<category><![CDATA[customize]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[extensions]]></category>
		<category><![CDATA[magento]]></category>
		<category><![CDATA[menus]]></category>

		<guid isPermaLink="false">http://www.atwix.com/?p=2542</guid>
		<description><![CDATA[Our new article includes helpful snippets which contain xml code of the most recent settings used in Magento admin.]]></description>
				<content:encoded><![CDATA[<p>From time to time  Magento developers face with a need to add settings for their extensions. As it&#8217;s hard to remember all necessary details for each setting, so we would like to share our snippets which contain xml code of the most recent settings used in Magento admin.<span id="more-2542"></span></p>
<p>First of all, we would recommend you always place everything, that refers to Magento system settings, in the system.xml file, since it&#8217;s the main and the only role of this file.<br />
If you need to create your own tab in the admin system settings, you should simply add the following lines to the file:</p>
<pre class="brush: xml; gutter: true">&lt;config&gt;
    &lt;tabs&gt;
        &lt;atwix translate=&quot;label&quot;&gt;
            &lt;label&gt;Atwix Extensions&lt;/label&gt;
            &lt;sort_order&gt;150&lt;/sort_order&gt;
        &lt;/atwix&gt;
    &lt;/tabs&gt;    
&lt;/config&gt;</pre>
<p>As a result, you&#8217;ll get the new group (tab) in the admin settings. When you need to complete the procedure of adding settings you should add the following items: section, settings groups and the settings (fields) themselves:</p>
<pre class="brush: xml; gutter: true">&lt;config&gt;
    &lt;sections&gt;
        &lt;atwix_shoppingbar translate=&quot;label&quot; module=&quot;atwix_shoppingbar&quot;&gt;
            &lt;class&gt;separator-top&lt;/class&gt;
            &lt;label&gt;Shopping Bar&lt;/label&gt;
            &lt;tab&gt;atwix&lt;/tab&gt;
            &lt;sort_order&gt;130&lt;/sort_order&gt;
            &lt;show_in_default&gt;1&lt;/show_in_default&gt;
            &lt;show_in_website&gt;1&lt;/show_in_website&gt;
            &lt;show_in_store&gt;1&lt;/show_in_store&gt;
            &lt;groups&gt;
                &lt;quicksearch&gt;
                    &lt;label&gt;Quick search&lt;/label&gt;
                    &lt;frontend_type&gt;text&lt;/frontend_type&gt;
                    &lt;sort_order&gt;20&lt;/sort_order&gt;
                    &lt;show_in_default&gt;1&lt;/show_in_default&gt;
                    &lt;show_in_website&gt;0&lt;/show_in_website&gt;
                    &lt;show_in_store&gt;1&lt;/show_in_store&gt;
                    &lt;fields&gt;
                        &lt;quicksearch_enabled translate=&quot;label comment&quot;&gt;
                            &lt;label&gt;Enable Quick Search&lt;/label&gt;
                            &lt;comment&gt;&lt;![CDATA[Enable quick search block]]&gt;&lt;/comment&gt;
                            &lt;frontend_type&gt;select&lt;/frontend_type&gt;
                            &lt;source_model&gt;adminhtml/system_config_source_yesno&lt;/source_model&gt;
                            &lt;sort_order&gt;1&lt;/sort_order&gt;
                            &lt;show_in_default&gt;1&lt;/show_in_default&gt;
                            &lt;show_in_website&gt;0&lt;/show_in_website&gt;
                            &lt;show_in_store&gt;1&lt;/show_in_store&gt;
                        &lt;/quicksearch_enabled&gt;
                        &lt;results_count translate=&quot;label comment&quot;&gt;
                            &lt;label&gt;Results count&lt;/label&gt;
                            &lt;comment&gt;&lt;![CDATA[Quick search results count (min 1, max 20)]]&gt;&lt;/comment&gt;
                            &lt;frontend_type&gt;text&lt;/frontend_type&gt;
                            &lt;sort_order&gt;5&lt;/sort_order&gt;
                            &lt;show_in_default&gt;1&lt;/show_in_default&gt;
                            &lt;show_in_website&gt;0&lt;/show_in_website&gt;
                            &lt;show_in_store&gt;1&lt;/show_in_store&gt;
                        &lt;/results_count&gt;
                    &lt;/fields&gt;
                &lt;/quicksearch&gt;
            &lt;/groups&gt;
        &lt;/atwix_shoppingbar&gt;
    &lt;/sections&gt;
&lt;/config&gt;</pre>
<p>Here is an illustration what means each of these terms:</p>
<p><img class="alignnone size-full wp-image-2557" alt="sections" src="http://www.atwix.com/wp-content/uploads/2013/01/sections.png" width="1250" height="556" /></p>
<p>Next question is how will the different settings in the xml config look like. As you already know, each setting is placed in the &lt;fields&gt; node, then follows the unique setting&#8217;s id and finally &#8211; setting&#8217;s attributes. Here is a small review of the most useful settings:</p>
<p><strong>Text field</strong></p>
<p>Simple input field for short text values:</p>
<p><img class="size-full wp-image-2556 alignnone" alt="text_field" src="http://www.atwix.com/wp-content/uploads/2013/01/text_field.png" width="609" height="31" /></p>
<pre class="brush: xml; gutter: true"> &lt;text_field translate=&quot;label&quot;&gt;
    &lt;label&gt;Text Field&lt;/label&gt;
    &lt;frontend_type&gt;text&lt;/frontend_type&gt;
    &lt;sort_order&gt;10&lt;/sort_order&gt;
    &lt;show_in_default&gt;1&lt;/show_in_default&gt;
    &lt;show_in_website&gt;1&lt;/show_in_website&gt;
    &lt;show_in_store&gt;1&lt;/show_in_store&gt;
&lt;/text_field&gt;</pre>
<p><strong>Textarea</strong></p>
<p>Wide input field for long text values:</p>
<p><img class="alignnone size-full wp-image-2555" alt="textarea" src="http://www.atwix.com/wp-content/uploads/2013/01/textarea.png" width="606" height="201" /></p>
<pre class="brush: xml; gutter: true">&lt;textarea translate=&quot;label&quot;&gt;
    &lt;label&gt;Textarea&lt;/label&gt;
    &lt;frontend_type&gt;textarea&lt;/frontend_type&gt;
    &lt;sort_order&gt;20&lt;/sort_order&gt;
    &lt;show_in_default&gt;1&lt;/show_in_default&gt;
    &lt;show_in_website&gt;1&lt;/show_in_website&gt;
    &lt;show_in_store&gt;1&lt;/show_in_store&gt;
&lt;/textarea&gt;</pre>
<p><strong>Drop down with Yes/No values</strong></p>
<p>Drop down menu with the values &#8220;Yes&#8221; and &#8220;No&#8221;. Commonly it is used as a flag for enabling/disabling something:</p>
<p><img class="alignnone size-full wp-image-2554" alt="dropdown_yesno" src="http://www.atwix.com/wp-content/uploads/2013/01/dropdown_yesno.png" width="582" height="31" /></p>
<pre class="brush: xml; gutter: true">&lt;enabled translate=&quot;label&quot;&gt;
    &lt;label&gt;Enabled&lt;/label&gt;
    &lt;frontend_type&gt;select&lt;/frontend_type&gt;
    &lt;source_model&gt;adminhtml/system_config_source_yesno&lt;/source_model&gt;
    &lt;sort_order&gt;1&lt;/sort_order&gt;
    &lt;show_in_default&gt;1&lt;/show_in_default&gt;
    &lt;show_in_website&gt;1&lt;/show_in_website&gt;
    &lt;show_in_store&gt;1&lt;/show_in_store&gt;
&lt;/enabled&gt;</pre>
<p><strong>Drop down with Enable/Disable values</strong></p>
<p>Almost the same as previous one, but instead of &#8220;Yes&#8221; and &#8220;No&#8221; you will get &#8220;Enable&#8221; and &#8220;Disable&#8221;:</p>
<p><img class="alignnone size-full wp-image-2551" alt="enableddisabled" src="http://www.atwix.com/wp-content/uploads/2013/01/enableddisabled.png" width="606" height="34" /></p>
<pre class="brush: xml; gutter: true">&lt;active translate=&quot;label&quot;&gt;
    &lt;label&gt;Enable/Disable&lt;/label&gt;
    &lt;frontend_type&gt;select&lt;/frontend_type&gt;
    &lt;sort_order&gt;40&lt;/sort_order&gt;
    &lt;source_model&gt;adminhtml/system_config_source_enabledisable&lt;/source_model&gt;
    &lt;show_in_default&gt;1&lt;/show_in_default&gt;
    &lt;show_in_website&gt;1&lt;/show_in_website&gt;
    &lt;show_in_store&gt;1&lt;/show_in_store&gt;
&lt;/active&gt;</pre>
<p><strong>Drop down with custom values</strong></p>
<p>Drop down with the custom set of values, generated by source model:</p>
<p><img class="alignnone size-full wp-image-2553" alt="custom_dropdown" src="http://www.atwix.com/wp-content/uploads/2013/01/custom_dropdown.png" width="607" height="35" /></p>
<pre class="brush: xml; gutter: true">&lt;default translate=&quot;label&quot;&gt;
    &lt;label&gt;Select&lt;/label&gt;
    &lt;frontend_type&gt;select&lt;/frontend_type&gt;
    &lt;!-- Source model for countries list. For custom list you need to use your own source model --&gt;
    &lt;source_model&gt;adminhtml/system_config_source_country&lt;/source_model&gt;
    &lt;sort_order&gt;50&lt;/sort_order&gt;
    &lt;show_in_default&gt;1&lt;/show_in_default&gt;
    &lt;show_in_website&gt;1&lt;/show_in_website&gt;
    &lt;show_in_store&gt;1&lt;/show_in_store&gt;
&lt;/default&gt;</pre>
<p><strong>Multiselect with custom values</strong></p>
<p>Field with the ability to select few items at the same time:</p>
<p><img class="alignnone size-full wp-image-2550" alt="multiselect" src="http://www.atwix.com/wp-content/uploads/2013/01/multiselect.png" width="590" height="228" /></p>
<pre class="brush: xml; gutter: true">&lt;multiselect translate=&quot;label&quot;&gt;
    &lt;label&gt;Multiselect&lt;/label&gt;
    &lt;frontend_type&gt;multiselect&lt;/frontend_type&gt;
    &lt;!-- Source model for countries list. For custom list you need to use your own source model --&gt;
    &lt;source_model&gt;adminhtml/system_config_source_country&lt;/source_model&gt;
    &lt;sort_order&gt;60&lt;/sort_order&gt;
    &lt;show_in_default&gt;1&lt;/show_in_default&gt;
    &lt;show_in_website&gt;1&lt;/show_in_website&gt;
    &lt;show_in_store&gt;1&lt;/show_in_store&gt;
    &lt;can_be_empty&gt;1&lt;/can_be_empty&gt;
&lt;/multiselect&gt;</pre>
<p><strong>File</strong></p>
<p>Allows to choose a file for uploading. In this example the file will be saved in var/uploads directory:</p>
<p><img class="alignnone size-full wp-image-2548" alt="file" src="http://www.atwix.com/wp-content/uploads/2013/01/file.png" width="601" height="52" /></p>
<pre class="brush: xml; gutter: true">&lt;file translate=&quot;label comment&quot;&gt;
    &lt;label&gt;File&lt;/label&gt;
    &lt;frontend_type&gt;file&lt;/frontend_type&gt;
    &lt;backend_model&gt;adminhtml/system_config_backend_file&lt;/backend_model&gt;
    &lt;upload_dir&gt;var/uploads&lt;/upload_dir&gt;
    &lt;sort_order&gt;70&lt;/sort_order&gt;
    &lt;show_in_default&gt;1&lt;/show_in_default&gt;
    &lt;show_in_website&gt;0&lt;/show_in_website&gt;
    &lt;show_in_store&gt;0&lt;/show_in_store&gt;
&lt;/file&gt;</pre>
<p><strong>Time</strong></p>
<p>Is used to create three drop down menus for setting up the hours, minutes and seconds respectively:</p>
<p><img class="alignnone size-full wp-image-2552" alt="time" src="http://www.atwix.com/wp-content/uploads/2013/01/time.png" width="609" height="28" /></p>
<pre class="brush: xml; gutter: true">&lt;time translate=&quot;label comment&quot;&gt;
    &lt;label&gt;Time&lt;/label&gt;
    &lt;frontend_type&gt;time&lt;/frontend_type&gt;
    &lt;sort_order&gt;80&lt;/sort_order&gt;
    &lt;show_in_default&gt;1&lt;/show_in_default&gt;
    &lt;show_in_website&gt;1&lt;/show_in_website&gt;
    &lt;show_in_store&gt;1&lt;/show_in_store&gt;
&lt;/time&gt;</pre>
<p><strong>Editable items list</strong></p>
<p>Allows to add/remove text values for the setting:</p>
<p><img class="alignnone size-full wp-image-2547" alt="editable_list" src="http://www.atwix.com/wp-content/uploads/2013/01/editable_list.png" width="577" height="113" /></p>
<pre class="brush: xml; gutter: true">&lt;addresses&gt;
    &lt;label&gt;Blocked Email Addresses&lt;/label&gt;
    &lt;frontend_model&gt;atwix_emailblocker/adminhtml_addresses&lt;/frontend_model&gt;
    &lt;backend_model&gt;adminhtml/system_config_backend_serialized&lt;/backend_model&gt;
    &lt;sort_order&gt;90&lt;/sort_order&gt;
    &lt;show_in_default&gt;1&lt;/show_in_default&gt;
    &lt;show_in_website&gt;1&lt;/show_in_website&gt;
    &lt;show_in_store&gt;1&lt;/show_in_store&gt;
    &lt;can_be_empty&gt;1&lt;/can_be_empty&gt;
&lt;/addresses&gt;</pre>
<p><strong>Heading</strong></p>
<p>Is used to entitle some settings inside the group:</p>
<p><img class="alignnone size-full wp-image-2549" alt="heading" src="http://www.atwix.com/wp-content/uploads/2013/01/heading.png" width="604" height="63" /></p>
<pre class="brush: xml; gutter: true">&lt;heading translate=&quot;label&quot;&gt;
    &lt;label&gt;Heading&lt;/label&gt;
    &lt;frontend_model&gt;adminhtml/system_config_form_field_heading&lt;/frontend_model&gt;
    &lt;sort_order&gt;90&lt;/sort_order&gt;
    &lt;show_in_default&gt;1&lt;/show_in_default&gt;
    &lt;show_in_website&gt;1&lt;/show_in_website&gt;
    &lt;show_in_store&gt;1&lt;/show_in_store&gt;
&lt;/heading&gt;</pre>
<p><strong>Dependent field</strong></p>
<p>It can be any field. The main purpose of this one &#8211; it&#8217;s to hide/show the field depending on the state of  some other field. In the following example the field depends on the field&#8217;s <em>enablemethod</em> state. If <em>enablemethod</em> value is 1 &#8211; dependent_field will appear and vice versa:</p>
<pre class="brush: xml; gutter: true">&lt;dependent_field translate=&quot;label&quot;&gt;
    &lt;label&gt;Dependent Field&lt;/label&gt;
    &lt;frontend_type&gt;textarea&lt;/frontend_type&gt;
    &lt;sort_order&gt;100&lt;/sort_order&gt;
    &lt;show_in_default&gt;1&lt;/show_in_default&gt;
    &lt;show_in_website&gt;1&lt;/show_in_website&gt;
    &lt;show_in_store&gt;1&lt;/show_in_store&gt;
    &lt;depends&gt;
        &lt;enablemethod&gt;1&lt;/enablemethod&gt;
    &lt;/depends&gt;
&lt;/dependent_field&gt;</pre>
<p>There is also one interesting cheat. You are able to use javascript inside of the &lt;comment&gt;&lt;/comment&gt; node:</p>
<pre class="brush: xml; gutter: true">&lt;specificerrmsg translate=&quot;label&quot;&gt;
    &lt;label&gt;Displayed Error Message&lt;/label&gt;
    &lt;frontend_type&gt;textarea&lt;/frontend_type&gt;
    &lt;comment&gt;
    &lt;![CDATA[
        &lt;script type=&quot;text/javascript&quot;&gt;
            Event.observe(&#039;carriers_flatrate_active&#039;, &#039;change&#039;, function() {
                alert(&#039;Be careful with this one&#039;);
            })
        &lt;/script&gt;
    ]]&gt;
    &lt;/comment&gt;
    &lt;sort_order&gt;2013&lt;/sort_order&gt;
    &lt;show_in_default&gt;1&lt;/show_in_default&gt;
    &lt;show_in_website&gt;1&lt;/show_in_website&gt;
    &lt;show_in_store&gt;1&lt;/show_in_store&gt;
&lt;/specificerrmsg&gt;</pre>
<p>It might be useful for changing field&#8217;s value depending on different credentials. We would not recommend you to use it very often there, especially for the big javascript code.</p>
<p>Moreover, as you might have noticed, there are some attributes like source_model for the custom drop downs or, all the more so, frontend model for editable list which may disappoint a bit. We are going to describe how to operate with these models in our next article, which will see the light in the nearest future. Thank you for reading us <img src='http://www.atwix.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.atwix.com/magento/system-settings/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Adding a column to Magento orders grid &#8211; alternative way using layout handles</title>
		<link>http://www.atwix.com/magento/column-to-orders-grid/</link>
		<comments>http://www.atwix.com/magento/column-to-orders-grid/#comments</comments>
		<pubDate>Mon, 28 Jan 2013 09:57:02 +0000</pubDate>
		<dc:creator>Volodymyr Vygovskyi</dc:creator>
				<category><![CDATA[Magento]]></category>
		<category><![CDATA[column]]></category>
		<category><![CDATA[grid]]></category>
		<category><![CDATA[magento]]></category>
		<category><![CDATA[order]]></category>

		<guid isPermaLink="false">http://www.atwix.com/?p=2378</guid>
		<description><![CDATA[You are welcome to check the alternative way of adding a column to Magento orders grid using layout handles.]]></description>
				<content:encoded><![CDATA[<p>In the previous articles (<a title="Adding a custom attribute/column to the orders grid in Magento admin" href="http://www.atwix.com/magento/customize-orders-grid/">1</a>, <a title="Adding a column to the customers grid in Magento admin. Alternative way" href="http://www.atwix.com/magento/add-column-to-customers-grid-alternative-way/">2</a>) you might find the way how to add custom column/attribute to adminhtml (orders, customers, ect) grid. But this is another example. In our article we will use the layout handles for inserting columns to the orders grid.<span id="more-2378"></span><br />
First of all, you should create a new module (in our case Atwix_ExtendedGrid) and define layout update for adminhtml:</p>
<pre class="brush: xml; gutter: true">&lt;!--file: app/code/local/Atwix/ExtendedGrid/etc/config.xml--&gt;
&lt;adminhtml&gt;
    ...
    &lt;layout&gt;
        &lt;updates&gt;
            &lt;atwix_extendedgrid&gt;
                &lt;file&gt;atwix/extendedgrid.xml&lt;/file&gt;
            &lt;/atwix_extendedgrid&gt;
        &lt;/updates&gt;
    &lt;/layout&gt;
    ...
&lt;/adminhtml&gt;</pre>
<p>Note, this configuration is enough for adding fields from <em>sales_flat_order_grid</em> table (no sql joins are needed). But most likely, you will also need to add some field from the external table. For this purpose we use <em>sales_order_grid_collection_load_before</em> event to join extra tables. Here is how the observer configuration looks like:</p>
<pre class="brush: xml; gutter: true">&lt;!--file: app/code/local/Atwix/ExtendedGrid/etc/config.xml--&gt;
&lt;adminhtml&gt;
    ...
    &lt;events&gt;
        &lt;sales_order_grid_collection_load_before&gt;
            &lt;observers&gt;
                &lt;atwix_extendedgrid&gt;
                    &lt;model&gt;atwix_extendedgrid/observer&lt;/model&gt;
                    &lt;method&gt;salesOrderGridCollectionLoadBefore&lt;/method&gt;
                &lt;/atwix_extendedgrid&gt;
            &lt;/observers&gt;
        &lt;/sales_order_grid_collection_load_before&gt;
    &lt;/events&gt;
    ...
&lt;/adminhtml&gt;</pre>
<p>Then, add a function that handles our event to the observer:</p>
<pre class="brush: php; gutter: true">/*file: app/code/local/Atwix/ExtendedGrid/Model/Observer.php*/
    /**
     * Joins extra tables for adding custom columns to Mage_Adminhtml_Block_Sales_Order_Grid
     * @param $observer
     */
    public function salesOrderGridCollectionLoadBefore($observer)
    {
        $collection = $observer-&gt;getOrderGridCollection();
        $select = $collection-&gt;getSelect();
        $select-&gt;joinLeft(array(&#039;payment&#039;=&gt;$collection-&gt;getTable(&#039;sales/order_payment&#039;)), &#039;payment.parent_id=main_table.entity_id&#039;,array(&#039;payment_method&#039;=&gt;&#039;method&#039;));
    }</pre>
<p>As you can see, we join <em>sales_flat_order_payment</em> table to get a <em>payment method</em> field.</p>
<p>Now, it is time to add this field to the grid. We will use layout handles to call <em>addColumnAfter</em> method of <em>sales_order.grid</em> (<em>Mage_Adminhtml_Block_Sales_Order_Grid</em>) block. There are two handles that we are interested in: <em>adminhtml_sales_order_index</em> and <em>adminhtml_sales_order_grid</em>. The same code goes to both sections, so we will define a new handle <em>sales_order_grid_update_handle</em> and use <em>update</em> directive. And finally, here is how our layout file looks like:</p>
<pre class="brush: xml; gutter: true">&lt;!--file: app/design/adminhtml/default/default/layout/atwix/extendedgrid.xml --&gt;
&lt;layout&gt;
    &lt;sales_order_grid_update_handle&gt;
        &lt;reference name=&quot;sales_order.grid&quot;&gt;
            &lt;action method=&quot;addColumnAfter&quot;&gt;
                &lt;columnId&gt;payment_method&lt;/columnId&gt;
                &lt;arguments&gt;
                    &lt;header&gt;Payment Method&lt;/header&gt;
                    &lt;index&gt;payment_method&lt;/index&gt;
                    &lt;filter_index&gt;payment.method&lt;/filter_index&gt;
                    &lt;type&gt;text&lt;/type&gt;
                &lt;/arguments&gt;
                &lt;after&gt;shipping_name&lt;/after&gt;
            &lt;/action&gt;
        &lt;/reference&gt;
    &lt;/sales_order_grid_update_handle&gt;
    &lt;adminhtml_sales_order_grid&gt;
        &lt;!-- apply layout handle defined above --&gt;
        &lt;update handle=&quot;sales_order_grid_update_handle&quot; /&gt;
    &lt;/adminhtml_sales_order_grid&gt;
    &lt;adminhtml_sales_order_index&gt;
        &lt;!-- apply layout handle defined above --&gt;
        &lt;update handle=&quot;sales_order_grid_update_handle&quot; /&gt;
    &lt;/adminhtml_sales_order_index&gt;
&lt;/layout&gt;</pre>
<p>After all steps you should see your column:<br />
<a href="http://www.atwix.com/uncategorized/column-to-orders-grid/attachment/orders_sales_magento_admin/" rel="attachment wp-att-2391"><img class="aligncenter size-full wp-image-2391" alt="Orders_Sales_Magento_Admin" src="http://www.atwix.com/wp-content/uploads/2013/01/Orders_Sales_Magento_Admin.png" width="919" height="284" /></a><br />
Here is a <a title="Source Code at Github" href="https://github.com/vovsky/ExtendedGrid/">source code</a> of the completed module. I hope you will find this article useful! Thanks for reading us!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.atwix.com/magento/column-to-orders-grid/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
