好的,这个问题是从我最近在这里提出的另一个问题引申而来。基本上,我想扩展 Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection 类,以便我可以添加一些额外的筛选器,用于可以在整个商店中重复使用的产品集合(例如畅销产品)。这意味着要替换我当前在 template.phtml 文件中使用的以下代码:
$_bs_productCollection = Mage::getResourceModel('reports/product_collection')
->addAttributeToSelect('name')
->addAttributeToFilter('visibility', $visibility)
->addOrderedQty()
->setOrder('ordered_qty', 'desc')
->setPageSize(6);
$_bs_productCollection->load();
我已经设置好了我的模块,它正在加载(在管理/系统/配置/高级选项中显示)。文件夹的结构如下:
etc/modules/Samsmodule.xml
local/Samsmodule
local/Samsmodule/Catalog
local/Samsmodule/Catalog/etc
local/Samsmodule/Catalog/etc/config.xml
local/Samsmodule/Catalog/Model
local/Samsmodule/Catalog/Model/Resource/Eav/Mysql4/Product/Collection.php
local/Samsmodule/Catalog/Helper (not sure if this is needed or not)
我的Samsmodule.xml文件内容如下:
<config>
<modules>
<Samsmodule_Catalog>
<active>true</active>
<codePool>local</codePool>
</Samsmodule_Catalog>
</modules>
</config>
我的config.xml是:
<config>
<modules>
<Samsmodule_Catalog>
<version>0.1.0</version>
</Samsmodule_Catalog>
</modules>
<global>
<models>
<catalog_resource_eav_mysql4>
<rewrite>
<product_collection>Samsmodule_Catalog_Model_Resource_Eav_Mysql4_Product_Collection</product_collection>
</rewrite>
</catalog_resource_eav_mysql4>
</models>
</global>
</config>
我的Collection.php文件如下:
<?php
class Samsmodule_Catalog_Model_Resource_Eav_Mysql4_Product_Collection extends Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection
{
public function filterbyBestSelling($attribute,$visibility,$_category,$no_of_items)
{
$this->addAttributeToSelect($attribute)->addOrderedQty()->setOrder('ordered_qty', 'desc')->addAttributeToFilter('visibility', $visibility)->addCategoryFilter($_category)->setPageSize($no_of_items);
return $this;
}
}
然后在我的template.phtml文件中这样调用它:
$_bs_productCollection = Mage::getResourceModel('reports/product_collection')
->filterbyBestSelling('name',$visibility,$_category,6);
但是它没有起作用-我错过了什么?如果我只是将我的Collection.php文件中的代码添加到我的核心Collection.php文件的底部,并使用相同的调用,那么它可以正常工作。