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 keyword “currentTheme” 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>
Good to see a tanlet at work. I can’t match that.
Kinda, this is waht i was looking out for.
I simply want to mention I am new to blogs and absolutely savored you’re web-site. Probably I’m planning to bookmark your website . You really have really good article content. Thanks a bunch for sharing your blog.
I simply want to tell you that I’m beginner to blogging and site-building and seriously liked you’re blog. Most likely I’m going to bookmark your blog post . You absolutely come with tremendous posts. Many thanks for sharing with us your web-site.
This is a new information for me, thanks for sharing|
I hate my life but at least this makes it baerable.
I found myself nodding my nggoin all the way through.
You get a lot of resecpt from me for writing these helpful articles.
I love reading these articles because they’re short but informative.