PHP Magento Tips

Add Custom Category attribute in Magento

Rate this posting:
{[['']]}

To add Custom category attribute with custom module in Magento follow below steps.

Step 1:

In  app/etc/modules/Mycompany_CategoryDescription.xml add below code:

<?xml version="1.0"?>

<config>

    <modules>

        <Mycompany_CategoryDescription>

            <active>true</active>

            <codePool>local</codePool>

            <depends>

                <Mage_Catalog />

            </depends>

        </Mycompany_CategoryDescription>

    </modules>

</config>

Step 2:

In  app/code/local/Mycompany/CategoryDescription/etc/config.xml add below code:

<?xml version="1.0"?>
<config>
 <modules>
  <Mycompany_CategoryDescription>
   <version>1.0.0</version>
  </Mycompany_CategoryDescription>
 </modules>
 <frontend>
  <layout>
   <updates>
    <mycompany_categorydescription>
     <file>CategoryDesc.xml</file>
    </mycompany_categorydescription>
   </updates>
  </layout>
 </frontend>
 <global>
  <resources>
   <categorydescription_setup>
    <setup>
     <module>Mycompany_CategoryDescription</module>
     <class>Mage_Catalog_Model_Resource_Setup</class>
    </setup>
    <connection>
     <use>default_setup</use>
    </connection>
   </categorydescription_setup>
  </resources>
  <helpers>
   <mycompany_categorydescription>
    <class>Mycompany_CategoryDescription_Helper</class>
   </mycompany_categorydescription>
  </helpers>
 </global>
 <adminhtml>
  <acl>
   <resources>
    <admin>
     <children>
      <system>
       <children>
        <config>
         <children>
          <mycompany_categorydescription translate="title">
           <title>Category Extra Description</title>
          </mycompany_categorydescription>
         </children>
        </config>
       </children>
      </system>
     </children>
    </admin>
   </resources>
  </acl>
 </adminhtml>
 <default>
  <mycompany_categorydescription>
   <general>
    <enabled>1</enabled>
   </general>
  </mycompany_categorydescription>
 </default>
</config>

Step 3: 

In app/code/local/Mycompany/CategoryDescription/etc/system.xml add below code:

<?xml version="1.0"?>

<config>

    <tabs>

        <categortextradescription>

            <label>Category Extra Description</label>

            <sort_order>450</sort_order>

        </categortextradescription>

    </tabs>

    <sections>

        <mycompany_categorydescription translate="label">

            <label>Category Extra Description</label>

            <tab>categortextradescription</tab>

            <sort_order>10</sort_order>

            <show_in_default>1</show_in_default>

            <show_in_website>1</show_in_website>

            <show_in_store>1</show_in_store>

            <groups>

                <general translate="label">

                    <label>Category Extra Description</label>

                    <sort_order>10</sort_order>

                    <show_in_default>1</show_in_default>

                    <show_in_website>1</show_in_website>

                    <show_in_store>1</show_in_store>

                    <fields>

                        <enabled translate="label">

                            <label>Enabled</label>

                            <frontend_type>select</frontend_type>

                            <source_model>adminhtml/system_config_source_yesno</source_model>

                            <sort_order>10</sort_order>

                            <show_in_default>1</show_in_default>

                            <show_in_website>1</show_in_website>

                            <show_in_store>1</show_in_store>

                        </enabled>

                    </fields>

                </general>

            </groups>

        </mycompany_categorydescription>

    </sections>

</config>

Step 4:

In app/code/local/Mycompany/CategoryDescription/sql/categorydescription_setup/install-0.0.1.php add below code:

<?php
$installer = Mage::getResourceModel('catalog/setup','catalog_setup');
$installer->startSetup();

//Categories typically only have one attribute set, this will retrieve its ID
$setId = Mage::getSingleton('eav/config')->getEntityType('catalog_category')->getDefaultAttributeSetId();

//Add group to entity & set
$installer->addAttributeGroup('catalog_category',$setId, 'My Extra Tab');

$installer->endSetup();
?>

This will add new tab in manage categories named with "My Extra Tab". Now we add our new attribute in this tab.

Step 5:
In app/code/local/Mycompany/CategoryDescription/sql/categorydescription_setup/upgrade-0.0.1-1.0.0.php add below code:

<?php
$installer = $this;
$installer->startSetup();
$attribute  = array(
    'type' => 'text',
    'label'=> 'Category Extra Description',
    'input' => 'textarea',
    'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
    'visible' => true,
    'required' => false,
    'user_defined' => true,
    'wysiwyg_enabled' => true,
    'visible_on_front' => true,
    'is_html_allowed_on_front' => true,
    'default' => "",
    'group' => "My Extra Tab"
);
$installer->addAttribute('catalog_category', 'extra_description', $attribute);
$installer->endSetup();
?>


Step 6:

In  app/design/frontend/rwd/default/layout/CategoryDesc.xml add below code:

<?xml version="1.0"?>
<layout version="0.1.0">
 <catalog_category_default>
  <reference name="root">
   <action method="addBodyClass">
    <className helper="mycompany_categorydescription/getCategoryClass" />
   </action>
  </reference>
  <reference name="content">
   <block name="category_extra_desc" type="catalog/category_view" after="category.products" template="catalog/category/extra-desc.phtml" />
  </reference>
 </catalog_category_default>
 <catalog_category_layered>
  <reference name="root">
   <action method="addBodyClass">
    <className helper="mycompany_categorydescription/getCategoryClass" />
   </action>
  </reference>
  <reference name="content">
   <block name="category_extra_desc" type="catalog/category_view" after="category.products" template="catalog/category/extra-desc.phtml" />
  </reference>
 </catalog_category_layered>
</layout>


Step 7: Create Helper Class to add new class to body tag. Create file in app/code/local/Mycompany/CategoryDescription/Helper/Data.php and add below code in it.

<?php

class Mycompany_CategoryDescription_Helper_Data extends Mage_Core_Helper_Abstract

{

public function getCategoryClass() {



$_category  = Mage::registry('current_category');

if( $_category->getExtraDescription() ){

return 'category-with-extradescription';

} else {

return 'category-without-extradescription';

}

}

}


Step 8:

In  app/design/frontend/rwd/default/template/catalog/category/extra-desc.phtml add below code:

<?php

if (Mage::getStoreConfig('mycompany_categorydescription/general/enabled')):

 $_category  = $this->getCurrentCategory();

 if( $_category->getExtraDescription() ):

?>

 <div class="category_description description extra_description">

  <?php echo Mage::helper('cms')->getPageTemplateProcessor()->filter($_category->getExtraDescription()); ?>

 </div>

 <?php endif; ?>

<?php endif; ?>

After adding above code, you will find one additional box in each category where you can add your extra description.


  • To display that extra description above product just change in  app/design/frontend/rwd/default/layout/CategoryDesc.xml from after to before
<catalog_category_default>
  <reference name="content">
   <block name="category_extra_desc" type="catalog/category_view" before="category.products" template="catalog/category/extra-desc.phtml" />
  </reference>
 </catalog_category_default>

<catalog_category_layered>
  <reference name="content">
   <block name="category_extra_desc" type="catalog/category_view" before="category.products" template="catalog/category/extra-desc.phtml" />
  </reference>
</catalog_category_layered>
Download This Module On Github : https://github.com/vijayrami/Category_Extra_Description
0 Comments On "Add Custom Category attribute in Magento"

Back To Top