How to show featured products on home page in magento?
If you want to show featured products on home page in magento so here is code for you...
Attribute Properties
<?php
class Company_Catalog_Block_Product_Featured extends Mage_Catalog_Block_Product_Abstract
{
/**
* Default toolbar block name
*
* @var string
*/
protected $_defaultToolbarBlock = 'catalog/product_list_toolbar';
/**
* Product Collection
*
* @var Mage_Eav_Model_Entity_Collection_Abstract
*/
protected $_productCollection;
/**
* Retrieve loaded category collection
*
* @return Mage_Eav_Model_Entity_Collection_Abstract
*/
protected function _getProductCollection()
{
if (is_null($this->_productCollection)) {
$layer = $this->getLayer();
/* @var $layer Mage_Catalog_Model_Layer */
if ($this->getShowRootCategory()) {
$this->setCategoryId(Mage::app()->getStore()->getRootCategoryId());
}
// if this is a product view page
if (Mage::registry('product')) {
// get collection of categories this product is associated with
$categories = Mage::registry('product')->getCategoryCollection()
->setPage(1, 1)
->load();
// if the product is associated with any category
if ($categories->count()) {
// show products from this category
$this->setCategoryId(current($categories->getIterator()));
}
}
$origCategory = null;
if ($this->getCategoryId()) {
$category = Mage::getModel('catalog/category')->load($this->getCategoryId());
if ($category->getId()) {
$origCategory = $layer->getCurrentCategory();
$layer->setCurrentCategory($category);
}
}
$this->_productCollection = $layer->getProductCollection()->addFieldToFilter(array(array('attribute' => 'featured', 'eq' => true),));
$this->prepareSortableFieldsByCategory($layer->getCurrentCategory());
if ($origCategory) {
$layer->setCurrentCategory($origCategory);
}
}
return $this->_productCollection;
}
/**
* Get catalog layer model
*
* @return Mage_Catalog_Model_Layer
*/
public function getLayer()
{
$layer = Mage::registry('current_layer');
if ($layer) {
return $layer;
}
return Mage::getSingleton('catalog/layer');
}
/**
* Retrieve loaded category collection
*
* @return Mage_Eav_Model_Entity_Collection_Abstract
*/
public function getLoadedProductCollection()
{
$this->_getProductCollection();
}
/**
* Retrieve current view mode
*
* @return string
*/
public function getMode()
{
return $this->getChild('toolbar')->getCurrentMode();
}
/**
* Need use as _prepareLayout - but problem in declaring collection from
* another block (was problem with search result)
*/
protected function _beforeToHtml()
{
$toolbar = $this->getToolbarBlock();
// called prepare sortable parameters
$collection = $this->_getProductCollection();
// use sortable parameters
if ($orders = $this->getAvailableOrders()) {
$toolbar->setAvailableOrders($orders);
}
if ($sort = $this->getSortBy()) {
$toolbar->setDefaultOrder($sort);
}
if ($dir = $this->getDefaultDirection()) {
$toolbar->setDefaultDirection($dir);
}
if ($modes = $this->getModes()) {
$toolbar->setModes($modes);
}
// set collection to toolbar and apply sort
$toolbar->setCollection($collection);
$this->setChild('toolbar', $toolbar);
Mage::dispatchEvent('catalog_block_product_list_collection', array(
'collection' => $this->_getProductCollection()
));
$this->_getProductCollection()->load();
return parent::_beforeToHtml();
}
/**
* Retrieve Toolbar block
*
* @return Mage_Catalog_Block_Product_List_Toolbar
*/
public function getToolbarBlock()
{
if ($blockName = $this->getToolbarBlockName()) {
if ($block = $this->getLayout()->getBlock($blockName)) {
return $block;
}
}
$block = $this->getLayout()->createBlock($this->_defaultToolbarBlock, microtime());
return $block;
}
/**
* Retrieve additional blocks html
*
* @return string
*/
public function getAdditionalHtml()
{
return $this->getChildHtml('additional');
}
/**
* Retrieve list toolbar HTML
*
* @return string
*/
public function getToolbarHtml()
{
return $this->getChildHtml('toolbar');
}
public function setCollection($collection)
{
$this->_productCollection = $collection;
return $this;
}
public function addAttribute($code)
{
$this->_getProductCollection()->addAttributeToSelect($code);
return $this;
}
public function getPriceBlockTemplate()
{
return $this->_getData('price_block_template');
}
/**
* Retrieve Catalog Config object
*
* @return Mage_Catalog_Model_Config
*/
protected function _getConfig()
{
return Mage::getSingleton('catalog/config');
}
/**
* Prepare Sort By fields from Category Data
*
* @param Mage_Catalog_Model_Category $category
* @return Mage_Catalog_Block_Product_List
*/
public function prepareSortableFieldsByCategory($category) {
if (!$this->getAvailableOrders()) {
$this->setAvailableOrders($category->getAvailableSortByOptions());
}
$availableOrders = $this->getAvailableOrders();
if (!$this->getSortBy()) {
if ($categorySortBy = $category->getDefaultSortBy()) {
if (!$availableOrders) {
$availableOrders = $this->_getConfig()->getAttributeUsedForSortByArray();
}
if (isset($availableOrders[$categorySortBy])) {
$this->setSortBy($categorySortBy);
}
}
}
return $this;
}
}
<?php
class Company_Mage_Page_Block_Html extends Mage_Page_Block_Html
{
public function getFeaturedProductHtml()
{
return $this->getBlockHtml('product_featured');
}
}
?>
<style>
.view-mode {display:none;}
</style>
<div style="float:left; width:100%; padding-left:0px; min-height:500px;"><div style="width:100%; float:left;">
<p><?php
$_productCollection=$this->_getProductCollection();
$_helper = $this->helper('catalog/output');
?>
<?php // Product Image ?>
<?php echo $this->getToolbarHtml() ?>
</p>
<?php foreach($_productCollection as $_product) {
$sortdesc = strlen($this->htmlEscape($_product->getShortDescription()));
$title = strlen($this->htmlEscape($_product->getName()));
?>
<div style="width:200px; margin:15px; float:left;">
<div style="float: left; width:200px;"> <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" class="product-image"><img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(200); ?>" width="200" height="200" alt="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" /></a>
</div>
<div style="float: left; width:100%;">
<div style="float: left;"><p style="margin-bottom:1px;"> <a href="<?php echo $_product->getProductUrl() ?>">
<?php echo substr($this->htmlEscape($_product->getName()),0,12); ?><?php if($title>12){ echo "..."; }?>
</a></p>
<p style="font-size:14px;"><?php echo substr($this->htmlEscape($_product->getShortDescription()),0,15); ?><?php if($sortdesc>15){ echo "..."; }?></p></div>
<div style="float: right;">
<p> <a href="<?php echo $_product->getProductUrl() ?>"><img alt="view Details" src="<?php echo $this->getSkinUrl('images/view.jpg') ?>"></a></p></div>
</div></div>
<?php } ?>
</div> </div>
<?php // Product Image ?>
<?php echo $this->getToolbarHtml() ?>
</p>
IF you still have not add featured attribute in magento so first of all you have to add arrtibute in Manage Attributes
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
- 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
Save the new attribute and go to Catalog ? Attributes ? Manage Attributes Sets to add the attribute to the default feature set. I mean to say put the attribute from right side and place it in left side where you want to show this attribute during the add prduct
Step
2: Create new block class that will instantiate the featured product
Create a new file, and directories:
app/code/local/Company/Catalog/Block/Product/Featured.php
<?php
class Company_Catalog_Block_Product_Featured extends Mage_Catalog_Block_Product_Abstract
{
/**
* Default toolbar block name
*
* @var string
*/
protected $_defaultToolbarBlock = 'catalog/product_list_toolbar';
/**
* Product Collection
*
* @var Mage_Eav_Model_Entity_Collection_Abstract
*/
protected $_productCollection;
/**
* Retrieve loaded category collection
*
* @return Mage_Eav_Model_Entity_Collection_Abstract
*/
protected function _getProductCollection()
{
if (is_null($this->_productCollection)) {
$layer = $this->getLayer();
/* @var $layer Mage_Catalog_Model_Layer */
if ($this->getShowRootCategory()) {
$this->setCategoryId(Mage::app()->getStore()->getRootCategoryId());
}
// if this is a product view page
if (Mage::registry('product')) {
// get collection of categories this product is associated with
$categories = Mage::registry('product')->getCategoryCollection()
->setPage(1, 1)
->load();
// if the product is associated with any category
if ($categories->count()) {
// show products from this category
$this->setCategoryId(current($categories->getIterator()));
}
}
$origCategory = null;
if ($this->getCategoryId()) {
$category = Mage::getModel('catalog/category')->load($this->getCategoryId());
if ($category->getId()) {
$origCategory = $layer->getCurrentCategory();
$layer->setCurrentCategory($category);
}
}
$this->_productCollection = $layer->getProductCollection()->addFieldToFilter(array(array('attribute' => 'featured', 'eq' => true),));
$this->prepareSortableFieldsByCategory($layer->getCurrentCategory());
if ($origCategory) {
$layer->setCurrentCategory($origCategory);
}
}
return $this->_productCollection;
}
/**
* Get catalog layer model
*
* @return Mage_Catalog_Model_Layer
*/
public function getLayer()
{
$layer = Mage::registry('current_layer');
if ($layer) {
return $layer;
}
return Mage::getSingleton('catalog/layer');
}
/**
* Retrieve loaded category collection
*
* @return Mage_Eav_Model_Entity_Collection_Abstract
*/
public function getLoadedProductCollection()
{
$this->_getProductCollection();
}
/**
* Retrieve current view mode
*
* @return string
*/
public function getMode()
{
return $this->getChild('toolbar')->getCurrentMode();
}
/**
* Need use as _prepareLayout - but problem in declaring collection from
* another block (was problem with search result)
*/
protected function _beforeToHtml()
{
$toolbar = $this->getToolbarBlock();
// called prepare sortable parameters
$collection = $this->_getProductCollection();
// use sortable parameters
if ($orders = $this->getAvailableOrders()) {
$toolbar->setAvailableOrders($orders);
}
if ($sort = $this->getSortBy()) {
$toolbar->setDefaultOrder($sort);
}
if ($dir = $this->getDefaultDirection()) {
$toolbar->setDefaultDirection($dir);
}
if ($modes = $this->getModes()) {
$toolbar->setModes($modes);
}
// set collection to toolbar and apply sort
$toolbar->setCollection($collection);
$this->setChild('toolbar', $toolbar);
Mage::dispatchEvent('catalog_block_product_list_collection', array(
'collection' => $this->_getProductCollection()
));
$this->_getProductCollection()->load();
return parent::_beforeToHtml();
}
/**
* Retrieve Toolbar block
*
* @return Mage_Catalog_Block_Product_List_Toolbar
*/
public function getToolbarBlock()
{
if ($blockName = $this->getToolbarBlockName()) {
if ($block = $this->getLayout()->getBlock($blockName)) {
return $block;
}
}
$block = $this->getLayout()->createBlock($this->_defaultToolbarBlock, microtime());
return $block;
}
/**
* Retrieve additional blocks html
*
* @return string
*/
public function getAdditionalHtml()
{
return $this->getChildHtml('additional');
}
/**
* Retrieve list toolbar HTML
*
* @return string
*/
public function getToolbarHtml()
{
return $this->getChildHtml('toolbar');
}
public function setCollection($collection)
{
$this->_productCollection = $collection;
return $this;
}
public function addAttribute($code)
{
$this->_getProductCollection()->addAttributeToSelect($code);
return $this;
}
public function getPriceBlockTemplate()
{
return $this->_getData('price_block_template');
}
/**
* Retrieve Catalog Config object
*
* @return Mage_Catalog_Model_Config
*/
protected function _getConfig()
{
return Mage::getSingleton('catalog/config');
}
/**
* Prepare Sort By fields from Category Data
*
* @param Mage_Catalog_Model_Category $category
* @return Mage_Catalog_Block_Product_List
*/
public function prepareSortableFieldsByCategory($category) {
if (!$this->getAvailableOrders()) {
$this->setAvailableOrders($category->getAvailableSortByOptions());
}
$availableOrders = $this->getAvailableOrders();
if (!$this->getSortBy()) {
if ($categorySortBy = $category->getDefaultSortBy()) {
if (!$availableOrders) {
$availableOrders = $this->_getConfig()->getAttributeUsedForSortByArray();
}
if (isset($availableOrders[$categorySortBy])) {
$this->setSortBy($categorySortBy);
}
}
}
return $this;
}
}
?>
Step 3: Extend Mage_Page_Block_Html
Create a new file, and directories: app/code/local/Company/Page/Block/Html.php<?php
class Company_Mage_Page_Block_Html extends Mage_Page_Block_Html
{
public function getFeaturedProductHtml()
{
return $this->getBlockHtml('product_featured');
}
}
?>
Step 4: Add This blocks to the app/etc/local.xml
<blocks>
<catalog>
<rewrite>
<product_featured>Company_Catalog_Block_Product_Featured</product_featured>
</rewrite>
</catalog>
</blocks>
after the <config><global> block.
Step 5: echo featured products HTML
Place the following code to the file: app/design/frontend/default/default/template/catalog/product/featured.phtml<style>
.view-mode {display:none;}
</style>
<div style="float:left; width:100%; padding-left:0px; min-height:500px;"><div style="width:100%; float:left;">
<p><?php
$_productCollection=$this->_getProductCollection();
$_helper = $this->helper('catalog/output');
?>
<?php // Product Image ?>
<?php echo $this->getToolbarHtml() ?>
</p>
<?php foreach($_productCollection as $_product) {
$sortdesc = strlen($this->htmlEscape($_product->getShortDescription()));
$title = strlen($this->htmlEscape($_product->getName()));
?>
<div style="width:200px; margin:15px; float:left;">
<div style="float: left; width:200px;"> <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" class="product-image"><img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(200); ?>" width="200" height="200" alt="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" /></a>
</div>
<div style="float: left; width:100%;">
<div style="float: left;"><p style="margin-bottom:1px;"> <a href="<?php echo $_product->getProductUrl() ?>">
<?php echo substr($this->htmlEscape($_product->getName()),0,12); ?><?php if($title>12){ echo "..."; }?>
</a></p>
<p style="font-size:14px;"><?php echo substr($this->htmlEscape($_product->getShortDescription()),0,15); ?><?php if($sortdesc>15){ echo "..."; }?></p></div>
<div style="float: right;">
<p> <a href="<?php echo $_product->getProductUrl() ?>"><img alt="view Details" src="<?php echo $this->getSkinUrl('images/view.jpg') ?>"></a></p></div>
</div></div>
<?php } ?>
</div> </div>
<?php // Product Image ?>
<?php echo $this->getToolbarHtml() ?>
</p>
Step 6: Add Featured Products block to the frontpage
As the last step, you have to place featured product box to the
frontpage. So, go to Magento administration to CMS> Manage Pages and
select home page (or any other if you wish to place featured products
in separate page)
Place the following line in Content area:
Place the following line in Content area:
{{block type="catalog/product_featured" name="product_featured" as="product_featured" template="catalog/product/featured.phtml"}}
After this delete your cache and flush all type of cache from admin and ftp , delete session and refresh the page hope this will work fine.
its really works i used it many times...... :)
I have followed all these steps very carefully but it's not working.Please do something helpful.Thanks in advance.
ReplyDeleteWhat problem you are facing?
ReplyDelete