Create custom attribute source type magento

This tutorial explains how to create custom attribute source type in magento. Here I am creating an attribute and set the source type as categories in the product page.

This module creates a dropdown attribute and listed all the categories in the category section as the options.This dropdown options change dynamically based on the categories in the category section.

Please refer the images below.
Image 1
magento custom attribute source type
Image 2
Set category as attribute source type

Step by step explanation

  1. Create the File structure
  2. Create the module
  3. Enable that from the magento backend
  4. Create the config.xml and the sql install file
  5. Create the Model for retrieving the categories
  6. Retrieve the value in the frontend
  7. Upgrade the plugin

Create the File structure

As an initial step create the files as shown below.
Set custom attribute source type magento
dynamically add attribute options magento

Create the module

After that we need to enable the module for that create the file in app/etc/module/Addpeople_Categoryattr.xml folder and enter the code as shown below.

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

Enable module from the magento backend

For enabling the module please login to the magento backend and navigate to the System/Configuration/Advanced/ and enable the module as shown and save the settings.
custom attribute source type magento

Create config and sql file

As the next step add the config and the sql files

<?xml version="1.0"?>
<config>
    <modules>
        <Addpeople_Categoryattr>
            <version>0.1.0</version>
        </Addpeople_Categoryattr>
    </modules>
    <global>
        <models>
            <categoryattr>
                <class>Addpeople_Categoryattr_Model</class>
            </categoryattr>
        </models>
        <resources>
            <addpeople_categoryattr_setup>
                <setup>
                    <module>Addpeople_Categoryattr</module>
                    <class>Mage_Eav_Model_Entity_Setup</class>
                </setup>
            </addpeople_categoryattr_setup>
        </resources>
    </global>
</config>

$installer = $this;

$installer->startSetup();

$installer->addAttribute('catalog_product', 'category_type', array(
    'group'             => 'General',
    'label'             => 'Category',
    'note'              => '',
    'type'              => 'int',    //backend_type
    'input'             => 'select',    //frontend_input
    'frontend_class'    => '',
    'source'        => 'categoryattr/attribute_source_type',
    'backend'           => '',
    'frontend'          => '',
    'global'            => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_WEBSITE,
    'required'          => true,
    'visible_on_front'  => false,
    'apply_to'          => 'simple',
    'is_configurable'   => false,
    'used_in_product_listing'    => false,
    'sort_order'        => 5,
));

$installer->endSetup();

Create the Model for custom options

Inside the sql file we mentioned the source as categoryattr/attribute_source_type so we need create the Attribute/Source/Type.php file and declare the option in the model file as shown.

<?php
class Addpeople_Categoryattr_Model_Attribute_Source_Type extends Mage_Eav_Model_Entity_Attribute_Source_Abstract
{


    public function getAllOptions()
    {
        $this->_options = array();
        $categories = Mage::getModel('catalog/category')
        ->getCollection()
        ->addAttributeToSelect('id')
        ->addAttributeToSelect('name')
        ->addAttributeToFilter('level',2) 
        ->addAttributeToFilter('entity_id', array('nin' => 148)) //exclude manufacturers
        ->addIsActiveFilter();
        
        $this->_options[] = array (
                'label' => 'Select Category',
                'value' => '0'
            );
        
        foreach($categories as $category) {
            $this->_options[] = array (
                'label' => $category->getName(),
                'value' => $category->getId()
            );
        }
        usort($this->_options, array( $this,'sortByValue'));
        return $this->_options;
    }
    
    function sortByValue($a, $b) {
        return strnatcmp ( $a['label'],$b['label']);
    }
}

Then clear the cache and login to the product section we get the custom attribute dropdown .
Set category as source type in the products page

Upgrade the plugin

For upgrading the plugin please create the upgrade file (mysql4-upgrade-0.1.0-0.2.0.php) and make the necessary changes. I have changed the label.

<?php
$updater = $this;

$updater->startSetup();

$updater->addAttribute('catalog_product', 'category_type', array(
    'group'             => 'General',
    'label'             => 'Category Label',
    'note'              => '',
    'type'              => 'int',    //backend_type
    'input'             => 'select',    //frontend_input
    'frontend_class'    => '',
    'source'        => 'categoryattr/attribute_source_type',
    'backend'           => '',
    'frontend'          => '',
    'global'            => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_WEBSITE,
    'required'          => true,
    'visible_on_front'  => true,
    'apply_to'          => 'simple,configurable',
    'is_configurable'   => false,
    'used_in_product_listing'    => true,
    'sort_order'        => 5,
));

$updater->endSetup();

Also change the version number in the config.xml file and clear the cache and refresh the category page.
Update config file magento for upgrading the plugin

If you have any further queries please leave a comment.

Thanks

Share this Tutorial

Leave a Reply

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

*