Display Featured Products on Homepage in Magento

Display Featured Products on Homepage in Magento.


The following are the steps to add featured products on homepage.

STEP: 1. To make featured product attribute.
Goto Catalog–>Attribute –> Manage Attribute–>Add New Attribute.

Step 1
Attribute Properties to be set

  • Attribute Identifier: featured
  • Scope: Store View
  • Catalog Input Type : Yes/No
  • Unique Value : No
  • Values Required: No
  • Input Validation : None
  • Apply To: All Product Types

Front End Properties to be set

  • Use In quick Search: No
  • Use In Advance Search: Yes
  • Comparable on Front-end: No
  • Visible on Catalog Pages On Front-end: Yes

Manage label/options to be set

Default: Featured Product

Now add featured product attribute in attribute set:
Goto Catalog –>Attributes–>Manage Attribute sets.

Our featured product attribute will be displayed in “Unassigned attribute” in right end side of the screen. Now drag and drop our “featured product” attribute in ‘General’ tab of “Groups” displayed in left end side of screen. Now click on “save attribute set” button. See the screen shot for reference.

Step 1

STEP: 2. Create featured product block file.
Go to Local directory app/code/local/ and create directory. Create directory level “FeaturedProduct/Catalog/Block/Product/Featured.php”

Copy and paste this code in Featured.php file.
<?php
class FeaturedProduct_Catalog_Block_Product_Featured extends Mage_Catalog_Block_Product_Abstract
{
public function getFeaturedProduct()
{
//database connection object
$storeId = Mage::app()->getStore()->getId();
$resource = Mage::getSingleton( 'core/resource' );
$read = $resource->getConnection( 'catalog_read' );
$categoryProductTable = $resource->getTableName( 'catalog/category_product' );
$productEntityIntTable = (string) Mage::getConfig()->getTablePrefix() . 'catalog_product_entity_int';
$eavAttributeTable = $resource->getTableName( 'eav/attribute' );
// Query for featured product
$select = $read->select()
->from( array( 'cp' => $categoryProductTable ) )
->join( array( 'pei' => $productEntityIntTable ), 'pei.entity_id=cp.product_id', array() )
->joinNatural( array( 'ea' => $eavAttributeTable ) )
->where( 'pei.value=1' )
->where( 'ea.attribute_code="featured"' );
$row = $read->fetchRow( $select );
return Mage::getModel( 'catalog/product' )->setStoreId( $storeId )->load( $row['product_id'] );
}
}

STEP: 3. Create template file for featured product.

Go to directory “app/design/frontend/my_theme/default/template/catalog/product/” and make featured.phtml file.
Replace my_theme with your current theme name.

Copy and paste this code in featured.phtml file.
<?php
$totalPerPage = ( $this->show_total ) ? $this->show_total : 6;
$visibility = array(
Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH,
Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG
);
$storeId = Mage::app()->getStore()->getId();
$_productCollection = Mage::getResourceModel( 'reports/product_collection' )
->addAttributeToSelect( '*' )
->setStoreId( $storeId )
->addStoreFilter( $storeId )
->addAttributeToFilter( 'visibility', $visibility )
->addAttributeToFilter( 'featured', true )
->setOrder( 'created_at', 'desc' )
->addAttributeToSelect( 'status' )
->setPageSize( $totalPerPage );

Mage::getSingleton( ‘catalog/product_status’ )->addVisibleFilterToCollection( $_productCollection );
Mage::getSingleton( ‘cataloginventory/stock’ )->addInStockFilterToCollection( $_productCollection );
Mage::getSingleton( ‘catalog/product_visibility’ )->addVisibleInSearchFilterToCollection( $_productCollection );
?>
<h1>Featured products</h1><br><br>
<div>
<?php $_collectionSize = $_productCollection->count() ?>
<table cellspacing=”0″ class=”generic-product-grid” id=”product-list-table” style=”margin-top:-50px; width:960px;”>
<?php $i = 0;
foreach ( $_productCollection as $_product ): ?><?php if ( $i ++ % 3 == 0 ): ?>
<tr style=”margin-top:20px;”><?php endif ?>
<td><p class=”product-image” style=”width:330px; margin-top:20px;”>
<a href=”<?php echo $_product->getProductUrl() ?>”
title=”<?php echo $this->htmlEscape( $this->getImageLabel( $_product, ‘small_image’ ) ) ?>”>
<img style=”height:190px; width:300px;”
src=”<?php echo $this->helper( ‘catalog/image’ )->init( $_product, ‘small_image’ )->resize( 170, 170 ); ?>”
alt=”<?php echo $this->htmlEscape( $this->getImageLabel( $_product, ‘small_image’ ) ) ?>”
title=”<?php echo $this->htmlEscape( $this->getImageLabel( $_product, ‘small_image’ ) ) ?>”/></a>
</p>
<?php /*?><h5><a href=”<?php echo $_product->getProductUrl() ?>” title=”<?php echo $this->htmlEscape($_product->getName()) ?>”><?php echo $this->htmlEscape($_product->getName()) ?></a></h5><?php */ ?>
<div>
<?php echo $_product->getShortDescription() ?></div>
<div>
<?php if ( $_product->getSpecialPrice() > 0 ): ?>
<span
class=”old”><?php echo $this->helper( ‘checkout’ )->formatPrice( $_product->getPrice() ) ?></span>&nbsp;
<span
class=”new”><?php echo $this->helper( ‘checkout’ )->formatPrice( $_product->getSpecialPrice() ) ?></span><?php else: ?>
<span
class=”new”><?php echo $this->helper( ‘checkout’ )->formatPrice( $_product->getPrice() ) ?></span>
<?php endif; ?><?php if ( $_product->isSaleable() ): ?>
<button type=”button” title=”<?php echo $this->__( ‘Add to Cart’ ) ?>” class=”button btn-cart”
onclick=”setLocation(‘<?php echo $this->getAddToCartUrl( $_product ) ?>’)”>
<span><span><?php echo $this->__( ‘Add to Cart’ ) ?></span></span></button><?php else: ?>
<p><span><?php echo $this->__( ‘Out of stock’ ) ?></span></p>
<?php endif; ?></div>
<div class=”clear”></div>
</td>
<?php if ( $i % 3 == 0 && $i != $_collectionSize ): ?></tr><?php endif ?><?php endforeach ?>
<?php for ( $i

;
$i % 3 != 0;
$i ++ ): ?>
<td class=”empty-product”>&nbsp;</td>
<?php endfor ?><?php if ( $i % 3 == 0 ): ?></tr><?php endif ?></table>
<script type=”text/javascript”>decorateTable(‘product-list-table’)</script>
</div>

STEP: 4. Add new block to the app/etc/local.xml

Add the following code after the global tag ends:
<blocks>
<catalog>
<rewrite>
<product_featured>MyCompany_Catalog_Block_Product_Featured</product_featured>
</rewrite>
</catalog>
</blocks>


STEP: 5. Add featured product block in home page.

Now we need place the featured product box to the frontend homepage. To do this:

Go to CMS > Manage Pages > Homepage
step 5

Copy and paste this featured product block on home page

{{block type="core/template" name="product_featured" as="product_featured" template="catalog/product/featured.phtml"}}

You should be able to see featured products on your homepage.

If you have any query or suggestions, feel free to ask me via the comment section below.

Leave a Reply