I wanted to add category filter to product_new widget in Magento 1.7. I have tried the following.
1.Module declaration.
<config>
<modules>
<module_name>
<active>true</active>
<codePool>local</codePool>
</module_name>
</modules> </config>
2.Added following to config.xml
<config>
<modules>
<module_name>
<version>0.1.0</version>
</module_name>
</modules>
<global>
<blocks>
<catalog>
<rewrite>
<product_new>Module_Name_Block_Product_New</product_new>
</rewrite>
</catalog>
</blocks>
</global>
</config>
3. Override product new class
class Module_Name_Block_Product_New extends Mage_Catalog_Block_Product_New
{
/**
* Prepare collection with new products and applied page limits.
*
* return Mage_Catalog_Block_Product_New
*/
protected function _beforeToHtml()
{
$todayStartOfDayDate = Mage::app()->getLocale()->date()
->setTime('00:00:00')
->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
$todayEndOfDayDate = Mage::app()->getLocale()->date()
->setTime('23:59:59')
->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
$collection = Mage::getResourceModel('catalog/product_collection');
$collection->setVisibility(Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds());
$collection = $this->_addProductAttributesAndPrices($collection)
->addStoreFilter()
->addAttributeToFilter('news_from_date', array('or'=> array(
0 => array('date' => true, 'to' => $todayEndOfDayDate),
1 => array('is' => new Zend_Db_Expr('null')))
), 'left')
->addAttributeToFilter('news_to_date', array('or'=> array(
0 => array('date' => true, 'from' => $todayStartOfDayDate),
1 => array('is' => new Zend_Db_Expr('null')))
), 'left')
->addAttributeToFilter(
array(
array('attribute' => 'news_from_date', 'is'=>new Zend_Db_Expr('not null')),
array('attribute' => 'news_to_date', 'is'=>new Zend_Db_Expr('not null'))
)
)
//->addAttributeToSort('news_from_date', 'desc')
->setPageSize($this->getProductsCount())
->setCurPage(1);
if($categoryId=$this->getData('category_id')){
$category = Mage::getModel('catalog/category')->load($categoryId);
$collection->addCategoryFilter($category)->addAttributeToSort('position','asc');
}
$collection->addAttributeToSort('news_from_date', 'desc');
$this->setProductCollection($collection);
return parent::_beforeToHtml();
}
}
4. After this, When i call new product widget in HOME CMS page its give me all new product from all the category, in short my new class in not in effect and still the core classes are called. Below is the widget block.
{{widget type="catalog/product_widget_new" products_count="9" category_id="124" template="catalog/product/widget/new/content/new_grid.phtml"}}
Can anyone please guide me what changes i should do in order to make my class effective.