我想重寫product_new塊。目的是我想添加category_id字段並只提取一個類別的新產品。下面是代碼Magento塊重寫不正常
應用程序\代碼\本地\富\酒吧\塊\產品\ New.php
類Foo_Bar_Block_Product_New擴展Mage_Catalog_Block_Product_New {
protected function _beforeToHtml()
{
// echo "aaaaaaasdfa";
$todayDate = Mage::app()->getLocale()->date()->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('date' => true, 'to' => $todayDate))
->addAttributeToFilter('news_to_date', array('or'=> array(
0 => array('date' => true, 'from' => $todayDate),
1 => array('is' => new Zend_Db_Expr('null')))
), 'left')
->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);
}
$this->setProductCollection($collection);
return parent::_beforeToHtml();
}
}
中添加塊local.xml使用以下代碼
<block type="foo_bar/product_new" name="new_products_list" template="catalog/product/new.phtml">
<action method="setCategoryId"><category_id>4</category_id></action>
<action method="setColumnCount"><columns>4</columns></action>
</block>
模塊的config.xml是
<global>
<blocks>
<catalog>
<rewrite>
<product_new>Foo_Bar_Block_Product_New</product_new>
</rewrite>
</catalog>
</blocks>
</global>
請指引我在哪裏,我會犯錯
感謝
有在目錄中提到CATEGORY_ID新產品。目前結果是空的塊。 – Noora 2014-10-11 08:26:34