<?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 &#187; magento</title>
	<atom:link href="http://www.atwix.com/tag/magento-2/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.atwix.com</link>
	<description>Custom Magento development, Magento modules and design</description>
	<lastBuildDate>Mon, 13 May 2013 10:28:40 +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>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>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>
		<item>
		<title>Creating CSV files in Magento</title>
		<link>http://www.atwix.com/magento/create-csv-files/</link>
		<comments>http://www.atwix.com/magento/create-csv-files/#comments</comments>
		<pubDate>Tue, 15 Jan 2013 09:00:26 +0000</pubDate>
		<dc:creator>Yaroslav Rogoza</dc:creator>
				<category><![CDATA[Magento]]></category>
		<category><![CDATA[csv]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[extensions]]></category>
		<category><![CDATA[magento]]></category>

		<guid isPermaLink="false">http://www.atwix.com/?p=2319</guid>
		<description><![CDATA[We would like to demonstrate the small example of the ability to generate and download CSV files in Magento.]]></description>
				<content:encoded><![CDATA[<p>There are many tools in Magento which allow you to view or generate and download different reports. So, we can conclude that Magento system has integrated tools for CSV and XML files generation. It might help in the situation when you need to generate a report for some feed or just export a list of structured data. We would like to demonstrate the small example that adds an ability to generate and download CSV files.<br />
Let&#8217;s assume that you have already created your custom extension and wish to add a CSV export functionality. If it&#8217;s not the case and you can&#8217;t guess how to create an extension, then you can always find the light in our previous articles, for example <a href="http://www.atwix.com/magento/add-category-attribute/">this one</a>. We called our extension Atwix_Tweaks so in the code examples below we will use this title.<br />
<span id="more-2319"></span><br />
As we said before, the file is being downloaded within some URL. Well, then we need to create the controller and corresponded method-action. In config.xml file add the section which will tell the system where our controller is located:</p>
<pre class="brush: xml; gutter: true">
   &lt;frontend&gt;
        &lt;routers&gt;
            &lt;atwix_tweaks&gt;
                &lt;use&gt;standard&lt;/use&gt;
                &lt;args&gt;
                    &lt;module&gt;Atwix_Tweaks&lt;/module&gt;
                    &lt;frontName&gt;mln&lt;/frontName&gt;
                &lt;/args&gt;
            &lt;/atwix_tweaks&gt;
        &lt;/routers&gt;
    &lt;/frontend&gt;
</pre>
<p>On the next step, we should create the controller itself (app/code/local/Atwix/Tweaks/controllers/IndexController.php):</p>
<pre class="brush: php; gutter: true">
class Atwix_Tweaks_IndexController extends Mage_Core_Controller_Front_Action
{
    /**
     * Returns generated CSV file
     */
    public function mlnProductsListAction()
    {
        $filename = &#039;mln.csv&#039;;
        $content = Mage::helper(&#039;atwix_tweaks/mln&#039;)-&gt;generateMlnList();

        $this-&gt;_prepareDownloadResponse($filename, $content);
    }
}
</pre>
<p>As you can see, the controller calls method generateMlnList() from helper &#8216;atwix_tweaks/mln&#8217;. What are we going to do next? Right, create the helper (app/code/local/Atwix/Tweaks/Helper/Mln.php):</p>
<pre class="brush: php; gutter: true">
class Atwix_Tweaks_Helper_Mln extends Mage_Core_Helper_Abstract
{
    /**
     * Contains current collection
     * @var string
     */
    protected $_list = null;

    public function __construct()
    {
        $collection = Mage::getModel(&#039;catalog/product&#039;)-&gt;getCollection()
            -&gt;addAttributeToSelect(&#039;*&#039;)
            -&gt;addAttributeToFilter(array(&#039;attribute&#039; =&gt; &#039;wight&#039;, &#039;gt&#039; =&gt; 2));

        $this-&gt;setList($collection);
    }

    /**
     * Sets current collection
     * @param $query
     */
    public function setList($collection)
    {
        $this-&gt;_list = $collection;
    }

    /**
     * Returns indexes of the fetched array as headers for CSV
     * @param array $products
     * @return array
     */
    protected function _getCsvHeaders($products)
    {
        $product = current($products);
        $headers = array_keys($product-&gt;getData());

        return $headers;
    }

    /**
     * Generates CSV file with product&#039;s list according to the collection in the $this-&gt;_list
     * @return array
     */
    public function generateMlnList()
    {
        if (!is_null($this-&gt;_list)) {
            $items = $this-&gt;_list-&gt;getItems();
            if (count($items) &gt; 0) {

                $io = new Varien_Io_File();
                $path = Mage::getBaseDir(&#039;var&#039;) . DS . &#039;export&#039; . DS;
                $name = md5(microtime());
                $file = $path . DS . $name . &#039;.csv&#039;;
                $io-&gt;setAllowCreateFolders(true);
                $io-&gt;open(array(&#039;path&#039; =&gt; $path));
                $io-&gt;streamOpen($file, &#039;w+&#039;);
                $io-&gt;streamLock(true);

                $io-&gt;streamWriteCsv($this-&gt;_getCsvHeaders($items));
                foreach ($items as $product) {
                    $io-&gt;streamWriteCsv($product-&gt;getData());
                }

                return array(
                    &#039;type&#039;  =&gt; &#039;filename&#039;,
                    &#039;value&#039; =&gt; $file,
                    &#039;rm&#039;    =&gt; true // can delete file after use
                );
            }
        }
    }
}
</pre>
<p>The helper uses Varien_Io_File class, integrated in Magento, to generate a custom CSV file. Generated file will be stored in the media folder and deleted before the controller sends header to the client side. If you don&#8217;t want to delete file, you can set parameter rm to false in the return statement. In the constructor (method __construct) you can use any collection you want (customers, orders, custom collections etc.). Also, you are able to pass the collection behind the constructor using method setList().<br />
I hope this small example will help you in your further development. If you have some suggestions or questions you can always place them here ⇩ <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/create-csv-files/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Adding custom product attribute to quote and order items in Magento</title>
		<link>http://www.atwix.com/magento/custom-product-attribute-quote-order-item/</link>
		<comments>http://www.atwix.com/magento/custom-product-attribute-quote-order-item/#comments</comments>
		<pubDate>Thu, 03 Jan 2013 09:20:19 +0000</pubDate>
		<dc:creator>Volodymyr Vygovskyi</dc:creator>
				<category><![CDATA[Magento]]></category>
		<category><![CDATA[attribute]]></category>
		<category><![CDATA[magento]]></category>
		<category><![CDATA[order]]></category>
		<category><![CDATA[quote]]></category>

		<guid isPermaLink="false">http://www.atwix.com/?p=2229</guid>
		<description><![CDATA[Welcome to look through our new article which describes how to add custom product attribute to quote and order items in Magento.]]></description>
				<content:encoded><![CDATA[<p>If you are developer, adding custom product attribute to quote and order items in Magento is pretty common task. And if you face with the same issue we will be very glad when you find this article helpful.<span id="more-2229"></span><br />
Start with creating new module. In <em>config.xml</em> we need to define resources section which allows us to use install and upgrade scripts. This way all necessary changes will be made  in the database &#8211; so, let&#8217;s do it:</p>
<pre class="brush: xml; gutter: true">
&lt;resources&gt;
    &lt;custom_attributes_setup&gt;
        &lt;setup&gt;
            &lt;module&gt;Atwix_CustomAttribute&lt;/module&gt;
            &lt;class&gt;Mage_Catalog_Model_Resource_Setup&lt;/class&gt;
        &lt;/setup&gt;
    &lt;/custom_attributes_setup&gt;
&lt;/resources&gt;
</pre>
<p>Note, that module installation process will be divided in several steps. Using Magento setup classes (in our case <em>Mage_Catalog_Model_Resource_Setup</em>) we add new custom product attribute. This is our first step and here is how install script looks like (<em>install-1.0.1.php</em>):</p>
<pre class="brush: php; gutter: true">
$installer = $this;

$installer-&gt;addAttribute(&#039;catalog_product&#039;, &#039;custom_attribute&#039;, array(
    &#039;group&#039;             =&gt; &#039;General&#039;,
    &#039;type&#039;              =&gt; Varien_Db_Ddl_Table::TYPE_VARCHAR,
    &#039;backend&#039;           =&gt; &#039;&#039;,
    &#039;frontend&#039;          =&gt; &#039;&#039;,
    &#039;label&#039;             =&gt; &#039;Custom Attribute&#039;,
    &#039;input&#039;             =&gt; &#039;text&#039;,
    &#039;class&#039;             =&gt; &#039;&#039;,
    &#039;source&#039;            =&gt; &#039;&#039;,
    &#039;global&#039;            =&gt; Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
    &#039;visible&#039;           =&gt; true,
    &#039;required&#039;          =&gt; false,
    &#039;user_defined&#039;      =&gt; true,
    &#039;default&#039;           =&gt; &#039;&#039;,
    &#039;searchable&#039;        =&gt; true,
    &#039;filterable&#039;        =&gt; true,
    &#039;comparable&#039;        =&gt; true,
    &#039;visible_on_front&#039;  =&gt; true,
    &#039;unique&#039;            =&gt; false,
    &#039;apply_to&#039;          =&gt; &#039;simple,configurable,virtual&#039;,
    &#039;is_configurable&#039;   =&gt; false
));
</pre>
<p>After launching installer script we can see our custom attribute at the backend:<br />
<a href="http://www.atwix.com/magento/custom-product-attribute-quote-order-item/attachment/magento_admin_product_edit/" rel="attachment wp-att-2233"><img src="http://www.atwix.com/wp-content/uploads/2012/12/Magento_Admin_Product_Edit.png" alt="Magento Admin Product Edit" width="664" height="87" class="aligncenter size-full wp-image-2233" /></a><br />
Next installation step is to add our custom attribute to the entities. In other words: to add neccessary fields to the tables, so we will be able to store our attribute. File <em>upgrade-1.0.1-1.0.2.php</em>:</p>
<pre class="brush: php; gutter: true">
$installer = new Mage_Sales_Model_Resource_Setup(&#039;core_setup&#039;);
/**
 * Add &#039;custom_attribute&#039; attribute for entities
 */
$entities = array(
    &#039;quote&#039;,
    &#039;quote_address&#039;,
    &#039;quote_item&#039;,
    &#039;quote_address_item&#039;,
    &#039;order&#039;,
    &#039;order_item&#039;
);
$options = array(
    &#039;type&#039;     =&gt; Varien_Db_Ddl_Table::TYPE_VARCHAR,
    &#039;visible&#039;  =&gt; true,
    &#039;required&#039; =&gt; false
);
foreach ($entities as $entity) {
    $installer-&gt;addAttribute($entity, &#039;custom_attribute&#039;, $options);
}
$installer-&gt;endSetup();
</pre>
<p><em>Note. Exclude entities that you don’t need.</em><br />
So, the database tables are ready. Furthermore, we need to implement the mechanism for converting (copying) our custom product attribute to quote item and order item. Fortunately, it is pretty easy with Magento.<br />
At first, you should add custom attribute to sales->quote->item->product_attributes node:</p>
<pre class="brush: xml; gutter: true">
&lt;sales&gt;
    &lt;quote&gt;
        &lt;item&gt;
            &lt;product_attributes&gt;
                &lt;custom_attribute /&gt;
            &lt;/product_attributes&gt;
        &lt;/item&gt;
    &lt;/quote&gt;
&lt;/sales&gt;
</pre>
<p>This makes attribute accessible when we will be copying it from the product to quote item &#8211; which is our next step. For this task the observer is used, and the event will be called <em>sales_quote_item_set_product</em>:</p>
<pre class="brush: xml; gutter: true">
&lt;sales_quote_item_set_product&gt;
    &lt;observers&gt;
        &lt;atwix_customattribute&gt;
            &lt;class&gt;atwix_customattribute/observer&lt;/class&gt;
            &lt;method&gt;salesQuoteItemSetCustomAttribute&lt;/method&gt;
        &lt;/atwix_customattribute&gt;
    &lt;/observers&gt;
&lt;/sales_quote_item_set_product&gt;
</pre>
<p>Obsever:</p>
<pre class="brush: php; gutter: true">
public function salesQuoteItemSetCustomAttribute($observer)
{
    $quoteItem = $observer-&gt;getQuoteItem();
    $product = $observer-&gt;getProduct();
    $quoteItem-&gt;setCustomAttribute($product-&gt;getCustomAttribute());
}
</pre>
<p>The last thing we need to carry about &#8211; it is converting attribute from quote item to order item. And this can be done with xml:</p>
<pre class="brush: xml; gutter: true">
&lt;fieldsets&gt;
    &lt;sales_convert_quote_item&gt;
        &lt;custom_attribute&gt;
            &lt;to_order_item&gt;*&lt;/to_order_item&gt;
        &lt;/custom_attribute&gt;
    &lt;/sales_convert_quote_item&gt;
    &lt;sales_convert_order_item&gt;
        &lt;custom_attribute&gt;
            &lt;to_quote_item&gt;*&lt;/to_quote_item&gt;
        &lt;/custom_attribute&gt;
    &lt;/sales_convert_order_item&gt;
&lt;/fieldsets&gt;
</pre>
<p>That&#8217;s it. Make a test order and check <em>sales_flat_order_item</em> table. If you see your custom attribute there that means &#8211; everything works. And source code can be found <a href="https://bitbucket.org/vovsky/adding-custom-product-attribute-to-quote-and-order-items-in/">here</a>. Good luck!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.atwix.com/magento/custom-product-attribute-quote-order-item/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Passing multiple entities/collections into Magento email template</title>
		<link>http://www.atwix.com/magento/pass-entities-collections/</link>
		<comments>http://www.atwix.com/magento/pass-entities-collections/#comments</comments>
		<pubDate>Mon, 24 Dec 2012 09:30:23 +0000</pubDate>
		<dc:creator>Nick Kravchuk</dc:creator>
				<category><![CDATA[Magento]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[block]]></category>
		<category><![CDATA[collection]]></category>
		<category><![CDATA[email]]></category>
		<category><![CDATA[foreach]]></category>
		<category><![CDATA[magento]]></category>
		<category><![CDATA[template]]></category>

		<guid isPermaLink="false">http://www.atwix.com/?p=2160</guid>
		<description><![CDATA[We would like to share our new findings about how to pass multiple entities/collections into Magento email template.]]></description>
				<content:encoded><![CDATA[<p>Hi friends, my previous article has been written about email sending feature in Magento, and you can read it <a href="http://www.atwix.com/magento/email-sending-feature/" target="_blank">here</a>. But it seems that we have one more issue &#8211; how to display collections/arrays data in the email templates. We&#8217;ll try to shed the light on this below.<span id="more-2160"></span>The first thing we should know &#8211; is the impossibility to use foreach/for/while in the template. So we should include a block with the template from a current theme inside the email template instead, just like we do for CMS pages. See below:</p>
<pre class="brush: html; gutter: true">{{block type=&#039;core/template&#039; area=&#039;frontend&#039; template=&#039;atwix/email/template.phtml&#039;}}</pre>
<p>Note, that is a very handy feature, but how do we pass data into the phtml file? &#8211; It&#8217;s easy:</p>
<pre class="brush: html; gutter: true">{{block type=&#039;core/template&#039; area=&#039;frontend&#039; template=&#039;atwix/email/template.phtml&#039; items=$items}}</pre>
<p>And if you don&#8217;t remember how to pass data into email template, it will be helpful to read our previous articles <img src='http://www.atwix.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
So, in this case $items variable may be an array or collection, etc&#8230;<br />
Then, you just need to sort out your data in phtml-file, see the following example:</p>
<pre class="brush: php; gutter: true">&lt;?php foreach ($this-&gt;getItems() as $_item): ?&gt;
    &lt;p&gt;&lt;?php echo $_item[&#039;name&#039;] ?&gt;&lt;/p&gt;
&lt;?php endforeach; ?&gt;</pre>
<p>As a result, you will see a dependence &#8211; if you pass <b>items</b>=$items you can get data in a phtml file using $this-&gt;get<b>Items</b>() function.</p>
<p>Seems that&#8217;s all, but we hope this short Magento article will be useful. Thanks for reading us!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.atwix.com/magento/pass-entities-collections/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Filter products by attribute on a Magento CMS page</title>
		<link>http://www.atwix.com/magento/products-list-cms/</link>
		<comments>http://www.atwix.com/magento/products-list-cms/#comments</comments>
		<pubDate>Fri, 21 Dec 2012 08:30:58 +0000</pubDate>
		<dc:creator>Yaroslav Rogoza</dc:creator>
				<category><![CDATA[Magento]]></category>
		<category><![CDATA[blocks]]></category>
		<category><![CDATA[cms]]></category>
		<category><![CDATA[customize]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[extensions]]></category>
		<category><![CDATA[magento]]></category>
		<category><![CDATA[module]]></category>

		<guid isPermaLink="false">http://www.atwix.com/?p=2165</guid>
		<description><![CDATA[Our article describes how to add products list that is filtered by some attribute to Magento CMS page.]]></description>
				<content:encoded><![CDATA[<p>Ability to create pages with custom content is a great feature provided by Magento. You can easily create a new page for your store and add text, html, images, different widgets there. But sometimes people want to have something non-trivial on their pages. Usually it means that you need to operate some knowledge before you&#8217;ll get an appropriate result. In this article we would like to suggest you a simple solution on how to place a products list, prefiltered by some specific attribute, on your CMS page. The example below describes how to create a simple extension for this purpose.<br />
<span id="more-2165"></span><br />
Let&#8217;s call the extension <em>Atwix_Cmsattr</em>. First, you need to add the init file for you extension. The path is app/etc/modules/Atwix_Cmsattr.xml . The file&#8217;s content is:</p>
<pre class="brush: xml; gutter: true">&lt;config&gt;
    &lt;modules&gt;
        &lt;Atwix_Cmsattr&gt;
            &lt;active&gt;true&lt;/active&gt;
            &lt;codePool&gt;local&lt;/codePool&gt;
        &lt;/Atwix_Cmsattr&gt;
    &lt;/modules&gt;
&lt;/config&gt;</pre>
<p>The extension contains one block class and one model, therefore the config file, which you should have by the following path: <em>app/code/local/Atwix/Cmsattr/etc/config.xml</em>, is pretty simple and consists of a few records:</p>
<pre class="brush: xml; gutter: true">&lt;config&gt;
    &lt;modules&gt;
        &lt;Atwix_Cmsattr&gt;
            &lt;version&gt;0.1.0&lt;/version&gt;
        &lt;/Atwix_Cmsattr&gt;
    &lt;/modules&gt;
    &lt;global&gt;
        &lt;blocks&gt;
            &lt;atwix_cmsattr&gt;
                &lt;class&gt;Atwix_Cmsattr_Block&lt;/class&gt;
            &lt;/atwix_cmsattr&gt;
        &lt;/blocks&gt;
        &lt;models&gt;
            &lt;atwix_cmsattr&gt;
                &lt;class&gt;Atwix_Cmsattr_Model&lt;/class&gt;
            &lt;/atwix_cmsattr&gt;
        &lt;/models&gt;
    &lt;/global&gt;
&lt;/config&gt;</pre>
<p>After that, create the block class <em>app/code/local/Atwix/Cmsattr/Block/List.php</em>:</p>
<pre class="brush: php; gutter: true">&lt;?php
class Atwix_Cmsattr_Block_List extends Mage_Catalog_Block_Product_Abstract
{
    protected $_itemCollection = null;

    public function getItems()
    {
        $color = $this-&gt;getColor();
        if (!$color)
            return false;
        if (is_null($this-&gt;_itemCollection)) {
            $this-&gt;_itemCollection = Mage::getModel(&#039;atwix_cmsattr/products&#039;)-&gt;getItemsCollection($color);
        }

        return $this-&gt;_itemCollection;
    }
}</pre>
<p>and the model class <em>app/code/local/Atwix/Cmsattr/Model/Products.php</em>:</p>
<pre class="brush: php; gutter: true">&lt;?php
class Atwix_Cmsattr_Model_Products extends Mage_Catalog_Model_Product
{
    public function getItemsCollection($valueId)
    {
        $collection = $this-&gt;getCollection()
            -&gt;addAttributeToSelect(&#039;*&#039;)
            -&gt;addAttributeToFilter(&#039;color&#039;, array(&#039;eq&#039; =&gt; $valueId));
        Mage::getSingleton(&#039;cataloginventory/stock&#039;)-&gt;addInStockFilterToCollection($collection);
        return $collection;
    }
}</pre>
<p>The next file is the most personal thing. You need the template file with your own mockup corresponding to your design. Here is an example for a simple list (<em>app/design/frontend/base/default/template/atwix/cmsattr/list.phtml</em>):</p>
<pre class="brush: php; gutter: true">&lt;?php $_items = $this-&gt;getItems() ?&gt;
&lt;div class=&quot;block&quot;&gt;
    &lt;div class=&quot;block-title&quot;&gt;
        &lt;strong&gt;&lt;span&gt;&lt;?php echo $this-&gt;__(&#039;Red Products&#039;) ?&gt;&lt;/span&gt;&lt;/strong&gt;
    &lt;/div&gt;
    &lt;div class=&quot;block-content&quot;&gt;
        &lt;ol class=&quot;mini-products-list&quot;&gt;
        &lt;?php foreach ($_items as $_item): ?&gt;
            &lt;li class=&quot;item&quot;&gt;
                &lt;div class=&quot;product&quot;&gt;
                    &lt;a href=&quot;&lt;?php echo $_item-&gt;getProductUrl() ?&gt;&quot; title=&quot;&lt;?php echo $this-&gt;htmlEscape($_item-&gt;getName()) ?&gt;&quot; class=&quot;product-image&quot;&gt;&lt;img src=&quot;&lt;?php echo $this-&gt;helper(&#039;catalog/image&#039;)-&gt;init($_item, &#039;thumbnail&#039;)-&gt;resize(50) ?&gt;&quot; width=&quot;50&quot; height=&quot;50&quot; alt=&quot;&lt;?php echo $this-&gt;htmlEscape($_item-&gt;getName()) ?&gt;&quot; /&gt;&lt;/a&gt;
                    &lt;div class=&quot;product-details&quot;&gt;
                        &lt;p class=&quot;product-name&quot;&gt;&lt;a href=&quot;&lt;?php echo $_item-&gt;getProductUrl() ?&gt;&quot;&gt;&lt;?php echo $this-&gt;htmlEscape($_item-&gt;getName()) ?&gt;&lt;/a&gt;&lt;/p&gt;
                        &lt;?php echo $this-&gt;getPriceHtml($_item, true) ?&gt;
                        &lt;?php if ($this-&gt;helper(&#039;wishlist&#039;)-&gt;isAllow()) : ?&gt;
                        &lt;a href=&quot;&lt;?php echo $this-&gt;getAddToWishlistUrl($_item) ?&gt;&quot; class=&quot;link-wishlist&quot;&gt;&lt;?php echo $this-&gt;__(&#039;Add to Wishlist&#039;) ?&gt;&lt;/a&gt;
                        &lt;?php endif; ?&gt;
                    &lt;/div&gt;
                &lt;/div&gt;
            &lt;/li&gt;
        &lt;?php endforeach; ?&gt;
        &lt;/ol&gt;
    &lt;/div&gt;
&lt;/div&gt;</pre>
<p>The last thing you need to do is to create a CMS page and insert the products list where you want to show it on the page. So, go to Admin-&gt;CMS-&gt;Pages-&gt;Add New Page. Fill the page with the necessary content and connect our products block anywhere in the content by putting the following string:</p>
<pre class="brush: php; gutter: true">{{block type=&quot;atwix_cmsattr/list&quot; name=&quot;cmsattr&quot; template=&quot;atwix/cmsattr/list.phtml&quot; color=&quot;26&quot;}}</pre>
<p>Note, that you need to pass the attribute value for color here. The product list on this page is filtered by this value. If you going to filter product by text attribute, such as &#8220;<em>name</em>&#8221; for example, you can pass the text value directly like <em>name=&#8221;Apple&#8221;</em>. But current example describes how to filter by attribute with type <em>select</em>. There are many ways to get value id from attribute&#8217;s value text. One of the simple (non programmatically) ways to do this is to inspect attributes values form using your browser. The example below describes how to do it within Google Chrome:</p>

<a href='http://www.atwix.com/magento/products-list-cms/attachment/screen-shot-2012-12-12-at-4-02-48-pm/' title='Screen Shot 2012-12-12 at 4.02.48 PM'><img width="150" height="150" src="http://www.atwix.com/wp-content/uploads/2012/12/Screen-Shot-2012-12-12-at-4.02.48-PM-150x150.png" class="attachment-thumbnail" alt="Screen Shot 2012-12-12 at 4.02.48 PM" /></a>
<a href='http://www.atwix.com/magento/products-list-cms/attachment/screen-shot-2012-12-12-at-4-03-01-pm/' title='Screen Shot 2012-12-12 at 4.03.01 PM'><img width="150" height="70" src="http://www.atwix.com/wp-content/uploads/2012/12/Screen-Shot-2012-12-12-at-4.03.01-PM-150x70.png" class="attachment-thumbnail" alt="Screen Shot 2012-12-12 at 4.03.01 PM" /></a>

<p>Also you can extend your model&#8217;s logic with the new method, which would transform attribute value text into it&#8217;s id, but this small article doesn&#8217;t describe how to do it <img src='http://www.atwix.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
In the next articles we are going to tell how to use layered navigation (filters) for products on CMS pages.<br />
Feel free to ask the questions and good luck in your coding experiments.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.atwix.com/magento/products-list-cms/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>Adding new category attribute in Magento</title>
		<link>http://www.atwix.com/magento/add-category-attribute/</link>
		<comments>http://www.atwix.com/magento/add-category-attribute/#comments</comments>
		<pubDate>Wed, 19 Dec 2012 10:27:55 +0000</pubDate>
		<dc:creator>Sergiy Vasyutynsky</dc:creator>
				<category><![CDATA[Magento]]></category>
		<category><![CDATA[attribute]]></category>
		<category><![CDATA[magento]]></category>

		<guid isPermaLink="false">http://www.atwix.com/?p=2089</guid>
		<description><![CDATA[Our new article will help you to find out useful tips on how to add a new custom category attribute.  ]]></description>
				<content:encoded><![CDATA[<p>In this article we would like to show you how to add a new custom category attribute. Let&#8217;s say, this attribute is needed to display some content on the category page.<br />
<span id="more-2089"></span>First of all, we need to create a new module for adding custom category attribute, we will call it &#8220;Custom Category Attribute&#8221;.</p>
<p><strong>Step 1</strong>. Create new module<br />
We should let Magento know about our new module. Initial configuration file is located in &#8216;app/etc/modules/Atwix_CustomCategoryAttribute.xml&#8217;.<br />
<em><strong>Atwix_CustomCategoryAttribute.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_CustomCategoryAttribute&gt;
            &lt;active&gt;true&lt;/active&gt;
            &lt;codePool&gt;community&lt;/codePool&gt;
        &lt;/Atwix_CustomCategoryAttribute&gt;
    &lt;/modules&gt;
&lt;/config&gt;
</pre>
<p>It means that the module is active and it is located in the community code pool.</p>
<p><strong>Step 2</strong>. Configure module<br />
Module configuration file is located in &#8216;app/code/&lt;code pool&gt;/&lt;name space&gt;/&lt;module name&gt;/etc&#8217; and its name is config.xml &#8211; note, that in our case this looks like &#8216; app/code/community/Atwix/CustomCategoryAttribute/etc/config.xml&#8217;.<br />
<em><strong>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_CustomCategoryAttribute&gt;
            &lt;version&gt;0.0.1&lt;/version&gt;
        &lt;/Atwix_CustomCategoryAttribute&gt;
    &lt;/modules&gt;

    &lt;global&gt;
        &lt;resources&gt;
            &lt;add_category_attribute&gt;
                &lt;setup&gt;
                    &lt;module&gt;Atwix_CustomCategoryAttribute&lt;/module&gt;
                    &lt;class&gt;Mage_Catalog_Model_Resource_Eav_Mysql4_Setup&lt;/class&gt;
                &lt;/setup&gt;
                &lt;connection&gt;
                    &lt;use&gt;core_setup&lt;/use&gt;
                &lt;/connection&gt;
            &lt;/add_category_attribute&gt;
            &lt;add_category_attribute_write&gt;
                &lt;connection&gt;
                    &lt;use&gt;core_write&lt;/use&gt;
                &lt;/connection&gt;
            &lt;/add_category_attribute_write&gt;
            &lt;add_category_attribute_read&gt;
                &lt;connection&gt;
                    &lt;use&gt;core_read&lt;/use&gt;
                &lt;/connection&gt;
            &lt;/add_category_attribute_read&gt;
        &lt;/resources&gt;
    &lt;/global&gt;
&lt;/config&gt;
</pre>
<p>As you can see, configuration file is not large, there are two nodes only: module version and resources for install script. Install script helps us to create a new attribute. In the node &lt;resources&gt; we defined the class for our install script which will be used for the extension. Working with methods of this class helps to create, update, remove attribute (etc). And the node &lt;add_category_attribute&gt; says that script must be located in the folder with the same name (in our case path will be &#8216;app/code/community/Atwix/CustomCategoryAttribute/sql/add_category_attribute&#8217;)</p>
<p><strong>Step 3</strong>. Create attribute<br />
Another important thing is to create install script file in the folder &#8216;add_category_attribute&#8217;, and the file name depends on the module version, so it looks like &#8216;mysql4-install-x.x.x.php&#8217;, where x.x.x is the version of the module.<br />
<em><strong>mysql4-install-0.0.1.php</strong></em></p>
<pre class="brush: php; gutter: true">
&lt;?php
$this-&gt;startSetup();
$this-&gt;addAttribute(&#039;catalog_category&#039;, &#039;custom_attribute&#039;, array(
    &#039;group&#039;         =&gt; &#039;General&#039;,
    &#039;input&#039;         =&gt; &#039;textarea&#039;,
    &#039;type&#039;          =&gt; &#039;text&#039;,
    &#039;label&#039;         =&gt; &#039;Custom attribute&#039;,
    &#039;backend&#039;       =&gt; &#039;&#039;,
    &#039;visible&#039;       =&gt; true,
    &#039;required&#039;      =&gt; false,
    &#039;visible_on_front&#039; =&gt; true,
    &#039;global&#039;        =&gt; Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
));

$this-&gt;endSetup();
</pre>
<p>As a result, we created a new attribute in the category with label &#8216;Custom attribute &#8216;. This attribute is a text area and it must be visible on the frontend. Check this out!</p>
<p><strong>Step 4</strong>. Check result<br />
Here, let’s clear cache and after this, go to Manage Categories &#8211; in &#8216;General&#8217; tab you will see a new attribute: &#8216;Custom attribute&#8217;.<br />
<a href="http://www.atwix.com/wp-content/uploads/2012/12/admin_category_attr.png"><img class="aligncenter size-full wp-image-2107" title="admin_category_attr" src="http://www.atwix.com/wp-content/uploads/2012/12/admin_category_attr.png" alt="" width="696" height="62" /></a><br />
Moreover, to see the content from &#8216;Custom attribute&#8217; on the frontend in the category page &#8211; you need to use the helper for reading attribute in the template of this page (&#8216;app/design/frontend/&lt;package&gt; /&lt;theme&gt;/template/catalog/category/view.phtml&#8217;), check please the following code:</p>
<pre class="brush: php; gutter: true">
...
&lt;?php if($_customAttribute = $this-&gt;getCurrentCategory()-&gt;getCustomAttribute()): ?&gt;
    &lt;?php echo $_helper-&gt;categoryAttribute($_category, $_customAttribute, &#039;custom_attribute&#039;) ?&gt;
&lt;?php endif; ?&gt;
...
</pre>
<p>As this code was put before &#8216;product list&#8217; output, so after refreshing a page we will see the result:<br />
<a href="http://www.atwix.com/wp-content/uploads/2012/12/catalog_product.png"><img class="aligncenter size-full wp-image-2106" title="catalog_category" src="http://www.atwix.com/wp-content/uploads/2012/12/catalog_product.png" alt="" width="542" height="435" /></a></p>
<p>Following all these steps, it will be great to enable wysiwyg editor for a new attribute, that will give us an opportunity to place different content with an inline styles in simple way.</p>
<p><strong>Step 5</strong>. Update attribute<br />
For each version of the module, it is also possible to have upgraded scripts which filenames are of the form mysql4-upgrade-0.0.1-0.0.2.php. It seems to be the best solution to update the attribute.<br />
<em><strong>mysql4-upgrade-0.0.1-0.0.2.php</strong></em></p>
<pre class="brush: php; gutter: true">
&lt;?php
$this-&gt;startSetup();
$this-&gt;addAttribute(&#039;catalog_category&#039;, &#039;custom_attribute&#039;, array(
    &#039;group&#039;         =&gt; &#039;General&#039;,
    &#039;input&#039;         =&gt; &#039;textarea&#039;,
    &#039;type&#039;          =&gt; &#039;text&#039;,
    &#039;label&#039;         =&gt; &#039;Custom attribute&#039;,
    &#039;backend&#039;       =&gt; &#039;&#039;,
    &#039;visible&#039;       =&gt; true,
    &#039;required&#039;      =&gt; false,
    &#039;wysiwyg_enabled&#039; =&gt; true,
    &#039;visible_on_front&#039; =&gt; true,
    &#039;is_html_allowed_on_front&#039; =&gt; true,
    &#039;global&#039;        =&gt; Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
));

$this-&gt;endSetup();
</pre>
<p>As well, method addAttribute can not only create, but also update the attribute &#8211; that&#8217;s why, you may also use method updateAttribute.</p>
<p><strong>Step 6</strong>. Check updated result<br />
At last, we clear cache and see the button &#8216; WYSIWYG Editor &#8216; near &#8216;Custom attribute&#8217;. Also, it is necessary to use editor to change the content in this attribute, insert some image, format text and at the end &#8211; to save the changes. After all, you should refresh category page and see the result:<br />
<a href="http://www.atwix.com/wp-content/uploads/2012/12/catalog_product_update.png"><img class="aligncenter size-full wp-image-2105" title="catalog_category_update" src="http://www.atwix.com/wp-content/uploads/2012/12/catalog_product_update.png" alt="" width="553" height="564" /></a></p>
<p>We hope this article will help you to work with Magento development. Thanks for reading!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.atwix.com/magento/add-category-attribute/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
