Add custom product attributes Magento

In this tutorial explains how to add a featured product custom attribute field to the product section using the install script.Please also refer the tutorial which explains how to add custom options to the dropdown.

You can also add the attribute to product page using the magento backend Manage Attribute section.But if you add using the setup script it easy to move files from the development database to live without copy the database changes manually.

Add custom product attributes Magento

This field allows the client to select which product they want to display in the home page.

The main steps are :

  1. 1) Create the file structure
  2. 2) Enable the module from the magento backend
  3. 3) Add the dropdown attribute
  4. 4) Retrieve the value in the template
  5. 5) Add the phtml file in the local.xml
  6. Create the file structure

    We need to create the following files in the app/local and app/etc folders as shown below.

    Enable product attribute module
    Custom product attribute 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_FeaturedProduct.xml file.

    <?xml version="1.0"?>
    <config>
        <modules>
            <Addpeople_FeaturedProduct>
                <active>true</active>
                <codePool>local</codePool>
            </Addpeople_FeaturedProduct>
        </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 custom product attribute script

    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/FeaturedProduct/etc/config.xml

    <?xml version="1.0"?>
    <config>
        <modules>
            <Addpeople_FeaturedProduct>
                <version>0.0.1</version>
            </Addpeople_FeaturedProduct>
        </modules>
        <global>       
            <resources>
                <add_product_attribute>
                    <setup>
                        <module>Addpeople_FeaturedProduct</module>
                        <class>Mage_Catalog_Model_Resource_Setup</class>
                    </setup>
                    <connection>
                        <use>core_setup</use>
                    </connection>
                </add_product_attribute>
            </resources>
        </global>
    </config>

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

    <?php
    $installer = $this;
    
    $installer->startSetup();
    
    $installer->addAttribute('catalog_product', 'new_products', array(
        'group'         => 'General',
        'input'         => 'select',
        'type'          => 'text',
        'label'         => 'New 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',
        'sort_order'        => 8,
        'is_configurable' => 0,
        'used_in_product_listing' => 1
    ));
    
    $installer->endSetup();

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

    Featured Product Attribute

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

    featured product attribute database entry

    Retrieve the value in the template

    Add a file named custom/featured_product.phtml in app/design/frontend/yourpackage_name/your_themefolder/template and enter the following code.

    <?php
    $featuredProducts = Mage::getModel('catalog/product')->getCollection()
        ->addAttributeToSelect('*')
        ->addAttributeToFilter('status',1)
        ->addAttributeToFilter('visibility',4)
        ->addAttributeToFilter('new_products', 1);
    ?>
    <section class="greyBg">
        <div class="main-container col1-layout">
            <h2 class="align-center page-title">Feature Items</h2>
            <ul class="products-grid products-grid--max-4-col ">
                <?php foreach($featuredProducts as $featuredProduct) : ?>
                    <li class="item">
                        <a href="<?php echo $featuredProduct->getProductUrl();?>" class="displayBlock">
                            <img src="<?php echo Mage::helper('catalog/image')->init($featuredProduct, 'thumbnail')->resize(280,280)?>" alt="<?php echo $featuredProduct->getName(); ?>"/>
                        </a>
                        <p><?php echo $featuredProduct->getName(); ?>
                        <span class="displayBlock fontBold"><?php echo Mage::helper('core')->currency($featuredProduct->getPrice(),true,false); ?></span></p>
                        <?php if($featuredProduct->isSaleable() && ($featuredProduct->getTypeId() == 'simple')): ?>
                            <button type="button" title="" class="button btn-cart" onclick="setLocation('<?php echo Mage::helper('checkout/cart')->getAddUrl($featuredProduct); ?>')">
                                <span><span><?php echo 'Add to Cart'; ?></span></span>
                            </button>
                        <?php elseif ($featuredProduct->isSaleable() && ($featuredProduct->getTypeId() == 'configurable')): ?>
                            <button type="button" title="" class="button btn-cart" onclick="setLocation('<?php echo $featuredProduct->getProductUrl(); ?>')">
                                <span><span><?php echo 'View Details'; ?></span></span>
                            </button>
                        <?php else: ?>
                            <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
                        <?php endif; ?>
                    </li>
                <?php endforeach; ?>
            </ul>
        </div>
    </section>

    Include phtml file in local.xml

    As the next step we need to include the featured_product.phtml file in the local.xml file.

    <cms_index_index>
        <reference name="content">
            <block type="core/template" name="home.featured.product" template="custom/featuredproduct.phtml"/>
        </reference>
    </cms_index_index>

    Please also refer the tutorial which explains how to add custom options to the dropdown.

    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 *

*