The Featured Product is a product with an attribute added from the administrative UI. When the administrator selects "Yes" in the "Featured" attribute, that product will be displayed in a content block on the category page.
Step to follow to achieve this
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.
2) Add Block configuration to catalog.xml
Open MyCompany/app/design/frontend/default/default/layout/catalog.xml. We want to add a new
Insert the block configuration on line 73 (default catalog.xml).
<block type="catalog/product_featured" name="product_featured" as="product_featured" template="catalog/product/featured.phtml"></block>
<php
class MyCompany_Catalog_Block_Product_Featured extends Mage_Catalog_Block_Product_Abstract
{
public function getFeaturedProduct()
{
// instantiate database connection object
$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
$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"');
$row = $read->fetchRow($select);
return Mage::getModel('catalog/product')->load($row['product_id']);
}
}
?>
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.
<php
class MyCompany_Catalog_Block_Category_View extends Mage_Catalog_Block_Category_View
{
public function getFeaturedProductHtml()
{
return $this->getBlockHtml('product_featured');
}
}
?>
Edit app/design/frontend/default/default/template/catalog/category/view.phtml and add the following code:
<?=$this->getFeaturedProductHtml()?>
<?=$this->getProductListHtml()?>
<?php $_product=$this->getFeaturedProduct() ?>
Check this out: <a href="<?php echo $_product->getProductUrl() ?>"><?php echo $this->htmlEscape($_product->getName()) ?></a>>
<blocks>
<catalog>
<rewrite> <product_featured>MyCompany_Catalog_Block_Product_Featured</product_featured>
</rewrite>
<rewrite>
<category_view>MyCompany_Catalog_Block_Category_View</category_view>
</rewrite>
</catalog>
</blocks>
Source: http://www.magentocommerce.com/wiki/how_to_create_a_featured_product
No comments:
Post a Comment