我想在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"}}
任何人都可以指导我为了让我的课程有效而应该做些什么改变。
答案 0 :(得分:0)
我认为现在是对的:
<blocks>
<catalog>
<rewrite>
<product_new>Module_Name_Block_Product_New</product_new>
</rewrite>
</catalog>
</blocks>
应该是
<blocks>
<catalog>
<rewrite>
<product_widget_new>Module_Name_Block_Product_New</product_widget_new>
</rewrite>
</catalog>
</blocks>
你的班级宣言应该是:
class Module_Name_Block_Product_New extends Mage_Catalog_Block_Product_Widget_New
您在代码中执行的操作是重写Mage_Catalog_Block_Product_New
阻止,但您在主页中使用此Mage_Catalog_Block_Product_Widget_New
。
我知道你会说Mage_Catalog_Block_Product_Widget_New
已经扩展Mage_Catalog_Block_Product_New
但是即使你已经覆盖了最后一个,小部件块仍然扩展了原始部分。