E commerce
- April 2, 2009
Product specials on home page
This tutorial explains briefly how to display one product on sale on the right column of thehome page, but that could be any page in fact.The code below is inspired from the front page featured product tutorial.
- Step 1 : create the product special block class/ Note that the getSpecialProducts function is inspired from the sales catalog list extension
create the file app\code\local\PoleOuest\Catalog\Block\Product\Special.php-
<?
-
class PoleOuest_Catalog_Block_Product_Special extends Mage_Catalog_Block_Product_Abstract
-
{
-
protected function _getStoreId()
-
{
-
$storeId = null;
-
if($storeId == null) {
-
$storeId = Mage::app()->getStore()->getId();
-
}
-
return $storeId
-
}
-
protected function _getCustomerGroupId()
-
{
-
$custGroupID = null;
-
if($custGroupID == null) {
-
$custGroupID = Mage::getSingleton(‘customer/session’)->getCustomerGroupId();
-
}
-
return $custGroupID;
-
}
-
-
// POLEOUEST Method from salescatalog
-
public function getSpecialProducts()
-
{
-
$storeId = $this->_getStoreId();
-
$websiteId = Mage::app()->getStore($storeId)->getWebsiteId();
-
$custGroup = $this->_getCustomerGroupId();
-
$product = Mage::getModel(‘catalog/product’);
-
$rulePriceWhere = "({{table}}.rule_date is null) or ({{table}}.rule_date=’$todayDate’ and {{table}}.website_id=’$websiteId’ and {{table}}.customer_group_id=’$custGroup’)";
-
$specials = $product->setStoreId($storeId)->getResourceCollection()
-
), ”, ‘left’)
-
->addAttributeToSort(‘special_from_date’, ‘desc’)
-
->addAttributeToSelect($productAttributes, ‘inner’)
-
->joinTable(‘catalogrule/rule_product_price’, ‘product_id=entity_id’, array(‘rule_price’=>‘rule_price’, ‘rule_start_date’=>‘latest_start_date’, ‘rule_date’=>‘rule_date’), $rulePriceWhere, ‘left’)
-
;
-
-
$specials->getSelect()->order(new Zend_Db_Expr(‘RAND()’))->limit(1); ;
-
$rulePriceCollection = Mage::getResourceModel(‘catalogrule/rule_product_price_collection’)
-
->addFieldToFilter(‘website_id’, $websiteId)
-
->addFieldToFilter(‘customer_group_id’, $custGroup)
-
->addFieldToFilter(‘rule_date’, $todayDate)
-
;
-
-
$productIds = $rulePriceCollection->getProductIds();
-
}
-
-
Mage::getSingleton(‘catalog/product_status’)->addVisibleFilterToCollection($specials);
-
Mage::getSingleton(‘catalog/product_visibility’)->addVisibleInCatalogFilterToCollection($specials);
-
$this->_productCollection = $specials;
-
}
-
return $this->_productCollection;
-
}
-
}
-
?>
-
- Step 2 : add new block to your etc/config.xml in global/blocks/catalog
<rewrite>
<product_special>PoleOuest_Catalog_Block_Product_Special</product_special>
</rewrite> - Step 3 : create your template catalog/product/PoleOuestSpecial.phtml with the loop inspired from the catalog/product/list.phtml
- Step 4 : declare your block in your layout/catalog.xml in the right.permanent.callout block
<block type=”catalog/product_special” name=”product_special” as=”product_special” template=”catalog/product/PoleOuestSpecial.phtml” /> - Step 5 : call your child HTML where you want it in \template\callouts\right_col.phtml :
<?php echo $this->getChildHtml(‘product_special’) ?>
and let us know how it went for you




One Response to “Product specials on home page”
May 13, 2009 - Anton Maryanov
Hey buddy, thanks for your article. It helped me a lot
also it will be better if you do not use these ugly quotes when publish your code 
also you can add these
Mage::getSingleton(‘catalog/product_status’)->addSaleableFilterToCollection($specials); Mage::getSingleton(‘cataloginventory/stock’)->addInStockFilterToCollection($specials);
for showing only available and salable products.
And the last:
->addAttributeToSelect($productAttributes, ‘inner’) – this does not work for me(I have empty results), only ‘left’ join works well.