Add custom attribute dropdown (yes/no) magento

For adding a new custom attribute to the category page we need to create a module. In this tutorial I am explaining how to add a featured category custom attribute field to magento backend.
This field allows the client to select which category they want to display in the home page.

The main steps are :

1) Create the file structure
2) Enable the module from the magento backend
3) Add the dropdown attribute
4) Retrieve the value in the template

Create the file structure

We need to create the following files in the app/local and app/etc folders as shown below.
enable-module
file-structure

Enable the module from the magento backend

As the next step we need to write the code for adding a new module. For that add the following code in your app/etc/Addpeople_CustomFeaturedAttribute.xml file.

<?xml version="1.0"?>
<config>
    <modules>
      <Addpeople_CustomFeaturedAttribute>
         <active>true</active>
         <codePool>local</codePool>
      </Addpeople_CustomFeaturedAttribute>
    </modules>
</config>

Then please login to your magento admin and navigate to System->Configuration->Advanced there you will find the module as shown below.

Enable magento module

Add the dropdown attribute

This step we need to add the attribute and set up the config.xml file as shown below.Add the following code to the Addpeople/CustomFeaturedAttribute/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Addpeople_CustomFeaturedAttribute>
            <version>0.0.1</version>
        </Addpeople_CustomFeaturedAttribute>
    </modules>
 
    <global>
        <resources>
            <add_category_attribute>
                <setup>
                    <module>Addpeople_CustomFeaturedAttribute</module>
                    <class>Mage_Catalog_Model_Resource_Setup</class>
                </setup>
                <connection>
                    <use>core_setup</use>
                </connection>
            </add_category_attribute>
            <add_category_attribute_write>
                <connection>
                    <use>core_write</use>
                </connection>
            </add_category_attribute_write>
            <add_category_attribute_read>
                <connection>
                    <use>core_read</use>
                </connection>
            </add_category_attribute_read>
        </resources>
    </global>
</config>

And add the custom attribute to the Addpeople/CustomFeaturedAttribute/sql/add_category_attribute/mysql4-install-0.0.1.php file as shown below.

<?php
$this->startSetup();
$this->addAttribute(Mage_Catalog_Model_Category::ENTITY, 'featured_product', array(
    'group'         => 'General Information',
    'input'         => 'select',
    'type'          => 'text',
    'label'         => 'Featured Product',
    'backend'       => '',
    'visible'       => true,
    'required'      => false,
    'visible_on_front' => true,
    'global'        => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
    'source' => 'eav/entity_attribute_source_boolean',
));
 
$this->endSetup();

Now please login to the magento backend and navigate to the category page and refresh the page and the clear the cache. You will get the custom attribute as shown below.
featured-category

The module reference is also added to the mg_core_resource table of your database as shown below.
magento-database

Retrieve the value in the template

For the retrieving the value of the custom attribute in the frontend use the method $cur_category->getFeaturedProduct() as shown below

<?php

  foreach ($this->getStoreCategories() as $_category): ?>
    $cur_category = Mage::getModel('catalog/category')->load($_category->getId());
    if($cur_category->getFeaturedProduct()) : // Check if the featured category is enabled
?>
    /* Code for displaying featured category goes here */
    
<?php endif; ?>
<?php endforeach; ?>

If you have any further queries please leave a reply. Thanks

Share this Tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *

*