Monday , December 11 2017
Home >> All tutorial >> How To Create a Featured Product

How To Create a Featured Product

Step 1: Create new “Featured attribute”

Create a new attribute by going to Catalog > Attributes > Manage Attributes > Add 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

Save the new attribute and go to Catalog → Attributes → Manage Attributes Sets to add the attribute to the default feature set.

Step 2: Create a new block class that will instantiate the featured product

{code type=php} getStore()->getId();
$categoryId = $this->getRequest()->getParam(‘id’, false);
$resource = Mage::getSingleton(‘core/resource’);
$read = $resource->getConnection(‘catalog_read’);
$categoryProductTable = $resource->getTableName(‘catalog/category_product’);
//$productEntityIntTable = $resource->getTableName(‘catalog/product_entity_int’); // doesn’t work 🙁
$productEntityIntTable = (string)Mage::getConfig()->getTablePrefix() . ‘catalog_product_entity_int’;
$eavAttributeTable = $resource->getTableName(‘eav/attribute’);
// Query database for featured product
if ($categoryId){
$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=?’, $categoryId)
->where(‘pei.value=1’)
->where(‘ea.attribute_code=”featured”‘);}
else {

$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”‘);
}
$featuredProductData = $read->fetchAll($select);
$i=0;
$product=array();
$productid=array();
foreach ($featuredProductData as $row) {

// instantiate the product object
//$productid[$i] = Mage::getModel(‘catalog/product’)->load($row[‘product_id’]);
$productid[$i] = $row[‘product_id’];

// if the product is a featured product, return the object
// if ($product->getData(‘featured’)) {

//}
$i++;
}
$productid=array_unique($productid);
$i=0;
foreach($productid as $id){
$product[$i] = Mage::getModel(‘catalog/product’)->load($id);
$i++;
}
return $product;
}
}
?>
{/code}

Step 4: Extend Mage_Catalog_Block_Category_View

Create a new file, and directories, called app/code/local/MyCompany/Catalog/Block/Category/View.php. We’re extending the core class here so our module will be separate from the core code base. When upgrading, we won’t have to worry about our code not working or having to patch files.
{code type=php} getBlockHtml(‘product_featured’);
}
}
?> {/code}

Step 5: Modify the templates

Edit app/design/frontend/default/default/template/catalog/category/view.phtml and add the following code:
{code type=html}>getFeaturedProductHtml()?>{/code}

ight above this line:
{code type=html}>getProductListHtml()?>{/code}

Create app/design/frontend/default/default/template/catalog/product/featured.phtml and add some product info HTML to show the featured product. Here is an example that simply displays a link to the product:
{code type=php}>getFeaturedProduct() ?>
Check this out:{/code}

Step 5: Add new blocks to the app/etc/local.xml

Add the following inside the config global tag:

{code type=html}>

MyCompany_Catalog_Block_Product_Featured

MyCompany_Catalog_Block_Category_View

{/code}

I hope this helps you add a “Featured Product” feature. It certainly feels thorough, but if I left anything out, please let me know and I’ll be happy to help.

We also contribute great content of Magento Download for FREE (all versions)

DON'T MISS THE CHANCE!

magento download

About Champ

Leave a Reply

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

Heap | Mobile and Web Analytics