Custom Magento EAV Attributes

Magento has an Entity Attribute Value system in the db, neaning you can easily add custom stuff by adding rows and not columns! But that’s all done for us, so here’s how we set it up. In this example I’m wanting a home page image radio button.

In the config.xml you’ll see something like

<version>0.2.0</version>

So in this example we would bump that up to 0.3.0. Also, check you have a setup key in the xml:

<resources>
    <madskull_homepage_setup>
        <setup>
            <module>Madskull_HomePage</module>
            <class>Mage_Catalog_Model_Resource_Setup</class>
        </setup>
    </madskull_homepage_setup>
</resources>

Next, in the sql folder of your module, create a file called madskull_homepage_setup/upgrade-0.2.0-0.3.0.php (thats upgrading from v0.2.0 to v0.3.0) and I put the following:

<?php

/* @var $installer Mage_Catalog_Model_Resource_Setup */
$installer = $this;

$installer->startSetup();

$installer->addAttribute(
    Mage_Catalog_Model_Product::ENTITY,
    'homepage_image',
    array(
        'frontend'                      => 'catalog/product_attribute_frontend_image',
        'global'                        => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
        'visible'                       => true,
        'used_in_product_listing'       => true,
        'filterable'                    => false,
        'filterable_in_search'          => false,
        'used_for_price_rules'          => false,
        'searchable'                    => false,
        'comparable'                    => false,
        'visible_on_front'              => true,
        'used_for_sort_by'              => false,
        'is_visible_in_advanced_search' => false,
        'type'                          => 'varchar',
        'input'                         => 'media_image',
        'group'                         => 'Images',
        'label'                         => 'Homepage Image',
        'user_defined'                  => true,
        'is_configurable'               => false,
        'required'                      => false
    )
);

$installer->endSetup();

When you next visit any page, Magento will see the version has changed, and attempt to upgrade it. If successful, it will appear in the eav_attribute table. Clear your caches,Ā  and reindex. Now if you go into your products in the admin panel, and select any product to edit, click on the Images option, and you should see our new attribute in there as a radio button! šŸ™‚

Now you can use your variable by doing things like $collection->addAttributeToSelect([‘name’, ‘price’, ‘special_price’, ‘homepage_image’]);Ā  and so on! šŸ˜€

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s