我想在 Magento 1.7 中将类别过滤器添加到 product_new 小部件。我尝试了以下方法。
1.模块声明。
<config>
<modules>
<module_name>
<active>true</active>
<codePool>local</codePool>
</module_name>
</modules> </config>
2.在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.覆盖产品新类
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. 之后,当我在 HOME CMS 页面中调用新产品小部件时,它会为我提供所有类别中的所有新产品,简而言之,我的新课程无效,但仍会调用核心课程。下面是小部件块。
{{widget type="catalog/product_widget_new" products_count="9" category_id="124" template="catalog/product/widget/new/content/new_grid.phtml"}}
任何人都可以指导我应该做哪些改变以使我的课程有效。