How To Create a Featured Product module in magento

Being new with magento and adding feature like “Featured Product”.  Most of the popular website has featured section/ featured product in magento for online shops;  to have a list of featured products on the homepage. I’ve set up quite a few eCommerce sites with Magento so far and always had to implement it myself since Magento doesn’t support it out of the box. Of course I could use a plugin, but the more independent solution is to do it myself.  Fortunately it’s quite easy to implement. This example also supports the category and sub category product listing in featured product block.

The following tutorial aims at people who know how to use Magento and who know how to create and modify themes (Magento CE 1.4.1.0).

Step 1: Add a new attribute: (Login to magento Admin area)
Now to add new attribute option from  “Catalog (Top Menu) > Attributes > Manage Attributes > Add New Attribute
Add appropriate values for property mentioned bellow and Save the new attribute.

Attribute Properties

  • Attribute Identifier: featured
  • Scope: Store View
  • Catalog Input Type for Store Owner: Yes/No
  • Unique Value (not shared with other products): No
  • Values Required: No
  • Input Validation for Store Owner: None
  • Apply To: All Product Types

Front End Properties

  • Use in quick search: No
  • Use in advanced 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

  • Default: Featured Product
  • English: Featured Product

we have added new attribute for featured product.

Step 2: Now we have to add newly created attribute to the attribute set.
Lets  go to  Catalog  > Attributes > Manage Attributes Sets  to add the attribute to the default feature set.  Newly created attribute is visible under the  “Unassigned Attributes”  in right hand side on the screen.  Now select featured attribute and perform Drag and Drop Operation in order to assign the property under “Group” section under  “General” tab. and click on “save attribute set” button.

Step 3:  Lets create the module
Lets say we have installed magento under folder “magento”.  go to the local directory  “app/code/local/” and create directory

Create directory level “FeaturedProduct/Catalog/Block/Product/Featured.php”

class FeaturedProduct_Catalog_Block_Product_Featured extends Mage_Catalog_Block_Product_Abstract
{
public function getSubCategory($category_id) {
$idList = "";
$categories = '';
$layer = Mage::getSingleton('catalog/layer');
$category = Mage::getModel('catalog/category')->load($category_id);
$categories = $category->getChildren();
$idList = $categories;
$list = explode(",", $categories);
foreach($list as $ccid) {
$cat = Mage::getModel('catalog/category')->load($ccid);
if($cat->getChildren()) $idList .= ",".$cat->getChildren();
}
return $idList;
}
public function getFeaturedProduct()
{
$storeId = Mage::app()->getStore()->getId();
$ccontroller = Mage::app()->getFrontController()->getRequest()->getControllerName();
$categoryId = $this->getRequest()->getParam('id', false);
if($ccontroller == "product") {
$productId = $this->getRequest()->getParam('id', false);
$cproduct = Mage::getModel('catalog/product')->load($productId);
$categoryArr = $cproduct->getCategoryIds();
$categoryId = $categoryArr[0];
/*Setting the bundle flag here*/
if($cproduct->getTypeId() == 'bundle'){
$bundleFlag = 1;
$bundleCategoryId = $categoryArr[0];
}
}
$resource = Mage::getSingleton('core/resource');
$product    = Mage::getModel('catalog/product');
$currentCategory = Mage::getModel('catalog/category')->load($categoryId);
$isTopCat = 0;
$categoryLevel = $currentCategory->getLevel();
if($categoryLevel == 2) $isTopCat = 1;
$csubcats = $currentCategory->getChildren();
if(!empty($csubcats) || $ccontroller == "product" ) {
$subcats = 0;
if($categoryLevel >= 2) {
$pathArr = explode("/", $currentCategory->getPath());
$subcats = $this->getSubCategory($pathArr[2]);
}
$categoryId = $this->getRequest()->getParam('id', false);
$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');
if(empty($subcats)) $subcats = '0';
//checking for Bundle Product flag here
if($bundleFlag == 1){
$subcats = $bundleCategoryId;
}
$select = $read->select()
->from(array('cp'=>$categoryProductTable))
->join(array('pei'=>$productEntityIntTable), 'pei.entity_id=cp.product_id', array())
->joinNatural(array('ea'=>$eavAttributeTable))
->where('cp.category_id IN ('.$subcats.')')
->where('pei.value=1')
->where('ea.attribute_code="featured"');
$featuredProductData = $read->fetchAll($select);
$i=0;
$product=array();
$productid=array();
foreach ($featuredProductData as $row) {
$productid[$i] = $row['product_id'];
$i++;
}
$productid = array_unique($productid);
$i=0;
$prodObj = '';
foreach($productid as $id){
if($id != '') {
$prodObj = Mage::getModel('catalog/product')->load($id);
if($prodObj->getVisibility() == 4 && $prodObj->isSaleable() == 1) {
$product[$i] = $prodObj;
$i++;
}
}
}
} else {
$product = '';
}
return $product;
}
}

Step 4:  Lets create the template file for featured product listing.
Replace keywordcurrentTheme” with actual theme bellow.

Go to the directory and create “/app/design/frontend/currentTheme/template/catalog/product/featured.phtml” 

<?php
echo $this->__('Featured products');
// Now fetch featured product.
$product = $this->getFeaturedProduct();
if($product) {
foreach($product as $_product) {
echo "Product Name". $_product->getName() . "<BR>";
echo "Product Price".  $this->getPriceHtml($_product) .  "<BR>";
echo "Product short Description" . $_product->getShortDescription() .  "<BR>";
echo "Product Image" .  $this->helper('catalog/image')->init($_product, 'small_image')->resize(200);   . "<BR>";
echo "Product URL" .  $_product->getProductUrl() . "<BR>";
}
?>

Step 5:  Now add entry to the local.xml file located at app/etc/.  Add following set of lines inside the  global tags.

<blocks>
<catalog>
<rewrite>
<product_featured>FeaturedProduct_Catalog_Block_Product_Featured</product_featured>
</rewrite>
</catalog>
</blocks>
Share this nice post:

35 thoughts on “How To Create a Featured Product module in magento

  1. I’m honored to obtain a call from my friend as he identified the important tips shared in your site. Browsing your blog write-up is a real excellent experience. Thanks again for considering readers much like me, and I hope for you the best of achievements like a professional in this field.

  2. Excellent web site. A lot of useful info here. I am sending it to a few friends ans additionally sharing in delicious. And of course, thanks in your effort!

  3. I discovered your web site on yahoo and check the number of your earlier blogposts. Keep up the wonderful work. Thank you.

  4. This could be one certain of the most useful sites We have actually arrive across on this matter. Genuinely Magnificent. I am also a professional in this topic so I can appreciate your exhausting work.

  5. I have been absent for some time, but now I remember why I used to love this blog. Thank you, I will try and check back more frequently.

  6. Also, weblog often and with fascinating material to maintain individuals interested in coming back and checking for updates.

  7. I think other web-site proprietors should take this website as an model, very clean and wonderful user friendly style and design, as well as the content. You’re an expert in this topic!

  8. The give an impression of a woman ought to stick with a person. The actual smell of men ought to come your way as you go in order to your pet leave anyone with just a memory, not just a frustration.

  9. I simply want to mention I am just very new to magento and seriously liked you’re blog. More than likely I’m likely to bookmark your blog post . You definitely have impressive articles. Regards for sharing your web-site.

  10. Attractive section of content. I just stumbled upon your web site and in accession capital to assert that I get actually enjoyed account your blog posts. Any way I will be subscribing to your augment and even I achievement you access consistently quickly.

  11. Great post. I was checking continuously this blog and I’m impressed! Very useful information specifically the last part :) I care for such info a lot. I was looking for this certain info for a very long time. Thank you and good luck.

  12. Thanks , I have recently been searching for information about this subject for a long time and yours is the greatest I have discovered till now.

  13. Valuable info. Lucky me I found your site by accident, and I’m shocked why this accident did not happened earlier! I bookmarked it.

  14. Hiya! I just want to give an enormous thumbs up for the nice data you’ve got here on this post. I will likely be coming again to your weblog for more soon.

  15. Whats up very cool site!! Man .. Excellent .. I’m happy to find a lot of helpful info here in the put up, we want work out extra techniques in this regard, thanks for sharing.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>