Create a new featured product in admin panel with below properties
Attribute Identifier: featuredproduct
Scope: Store View
Catalog Input Type for Store Owner: Yes/No
Unique Value: No
Values Required: No
Apply To: All Product Types
Use in quick search: No
Use in advanced search: Yes
Comparable on Front-end: No
Visible on Catalog Pages on Front-end: Yes
Save the attribute and set it to default Attributes Sets.
Then,Create new block class that will instantiate the featured product
Create a new file, and directories: app/code/local/Companyname/Catalog/Block/Product/Featured.php
<?php
class Companyname_Catalog_Block_Product_Featured extends Mage_Catalog_Block_Product_Abstract
{
public function getFeaturedProduct()
{
// instantiate database connection object
$storeId = Mage::app()->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="featuredproduct"');
}
$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('featuredproduct')) {
//}
$i++;
}
$productid=array_unique($productid);
$i=0;
foreach($productid as $id){
$product[$i] = Mage::getModel('catalog/product')->load($id);
$i++;
}
return $product;
}
}
?>
Extend Mage_Catalog_Block_Category_View
Create a new file, and directories, called app/code/local/MyCompany/Catalog/Block/Category/View.php.
<?php
class MyCompany_Catalog_Block_Category_View extends Mage_Catalog_Block_Category_View
{
public function getFeaturedProductHtml()
{
return $this->getBlockHtml('product_featured');
}
}
?>
then edit app/design/frontend/default/default/template/catalog/category/view.phtml.
<?=$this->getFeaturedProductHtml()?>
right above this line:
<?=$this->getProductListHtml()?>
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:
<?php $_product=$this->getFeaturedProduct() ?>
Check this out: <a href="<?php echo $_product->getProductUrl() ?>"><?php echo $this->htmlEscape($_product->getName()) ?></a>
Add new blocks to the app/etc/local.xml
Add the following inside the config global tag:
<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>
Attribute Identifier: featuredproduct
Scope: Store View
Catalog Input Type for Store Owner: Yes/No
Unique Value: No
Values Required: No
Apply To: All Product Types
Use in quick search: No
Use in advanced search: Yes
Comparable on Front-end: No
Visible on Catalog Pages on Front-end: Yes
Save the attribute and set it to default Attributes Sets.
Then,Create new block class that will instantiate the featured product
Create a new file, and directories: app/code/local/Companyname/Catalog/Block/Product/Featured.php
<?php
class Companyname_Catalog_Block_Product_Featured extends Mage_Catalog_Block_Product_Abstract
{
public function getFeaturedProduct()
{
// instantiate database connection object
$storeId = Mage::app()->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="featuredproduct"');
}
$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('featuredproduct')) {
//}
$i++;
}
$productid=array_unique($productid);
$i=0;
foreach($productid as $id){
$product[$i] = Mage::getModel('catalog/product')->load($id);
$i++;
}
return $product;
}
}
?>
Extend Mage_Catalog_Block_Category_View
Create a new file, and directories, called app/code/local/MyCompany/Catalog/Block/Category/View.php.
<?php
class MyCompany_Catalog_Block_Category_View extends Mage_Catalog_Block_Category_View
{
public function getFeaturedProductHtml()
{
return $this->getBlockHtml('product_featured');
}
}
?>
then edit app/design/frontend/default/default/template/catalog/category/view.phtml.
<?=$this->getFeaturedProductHtml()?>
right above this line:
<?=$this->getProductListHtml()?>
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:
<?php $_product=$this->getFeaturedProduct() ?>
Check this out: <a href="<?php echo $_product->getProductUrl() ?>"><?php echo $this->htmlEscape($_product->getName()) ?></a>
Add new blocks to the app/etc/local.xml
Add the following inside the config global tag:
<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>
Comments
Post a Comment
Feel Free To Ask...