Magento - How to add featured product on Magento Store homepage
STEP: 1. To make featured product attribute.
Login to Magento admin area.
Now add attribute from tab
Catalog–>Attribute –> Manage Attribute–>Add New Attribute.
Attribute Properties to be set
Attribute Identifier: featured
Scope: Store View
Catalog Input Type For Store Owner: Yes/No
Unique Value (not shared with other product): No
Values Required: No
Input Validation for Store Owner: 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
Use In Layered navigation (Can be used only with catalog input type ‘Dropdown‘): No
Visible on Catalog Pages On Front-end: Yes
Manage label/options to be set
Default: Featured Product
English: Featured Product
Now add featured product attribute in attribute set:
Go toCatalog –>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.
Screenshot: https://www.milople.com/blogs/wp-content/uploads/2015/02/i1_edit-attribute-set.jpg
STEP: 2. Create featured product block file.
Go to Local directory app/code/local/ and create directory.
Create directory level
“Featured Product/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/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>
<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"> </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:
<product_featured>FeaturedProduct_Catalog_Block_Product_Featured
STEP: 5. Add featured product block in home page.
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"}}
Now the featured product on your home page can be seen!