Search code examples
magento

Add custom property to magento Attributes and display it on the front end


I have started using magento as my ecommerce cms and I know that is an extremely powerful platform. Recently I came across its functionality that helps the developer extending the core and I have managed to add custom category options. Is there any chance to achieve the same results on an attribute? I would like to add a text description on the properties' tab and display it on the front end?


Solution

  • This is possible with writing your own custom module. I did it to add a tooltip-option to an attribute.

    You can use the adminhtml_catalog_product_attribute_edit_prepare_form-event to add a field with your observer:

    $fieldset = $observer->getForm()->getElement('base_fieldset');
    $fieldset->addField('tooltip', 'text', array(
        'name' => 'tooltip',
        'label' => Mage::helper('catalog')->__('Tooltip'),
        'title' => Mage::helper('catalog')->__('Tooltip')
    ));
    

    This adds the extra field to the attribute-edit screen. Next up is to make sure the property gets saved when editing your attribute. This can be done in your installer script with something like:

    $installer->getConnection()->addColumn(
        $installer->getTable('catalog/eav_attribute'),
        'tooltip',
        array(
            'type'      => Varien_Db_Ddl_Table::TYPE_TEXT,
            'nullable'  => true,
            'comment'   => 'Tooltip'
        )
    );
    

    You also have to make sure your Model/Resource/Setup-class extends Mage_Eav_Model_Entity_Setup instead of Mage_Core_Model_Resource_Setup.

    Now, at this point, you can save your custom attribute property. Next step is to display it on the frontend. This can be done fairly easy, just by simple Magento templating 101:

    For example, in catalog/product/view/type/options/configurable.phtml in the foreach()-loop, place something like this, to display the tooltip:

    echo $_attribute->getProductAttribute()->getTooltip();
    

    That's nice...

    Update: Since I got some questions about this subject by e-mail, I decided to write a more detailed blog post about it. You can read it here: http://gielberkers.com/add-custom-properties-magento-attributes/