This tutorial will show you how to implement a Featured Product in magento.
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
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”
view plaincopy to clipboardprint?
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(!emptyempty($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(emptyempty($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”
view plaincopy to clipboardprint?
<?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>
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
-
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
-
Default: Featured Product
-
English: 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”
view plaincopy to clipboardprint?
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(!emptyempty($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(emptyempty($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”
view plaincopy to clipboardprint?
<?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>