相关疑难解决方法(0)

Magento - 扩展Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection类

好的,所以这是我最近在这里问到的另一个问题.基本上,我想扩展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();
Run Code Online (Sandbox Code Playgroud)

所以,我设置了我的模块,它正在加载(它显示在admin/system/config/advanced中).文件夹结构如下:

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)
Run Code Online (Sandbox Code Playgroud)

我的Samsmodule.xml是:

<config>
<modules>
    <Samsmodule_Catalog>
        <active>true</active>
        <codePool>local</codePool>
    </Samsmodule_Catalog>
</modules>
</config>
Run Code Online (Sandbox Code Playgroud)

我的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>
Run Code Online (Sandbox Code Playgroud)

我的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;
} …
Run Code Online (Sandbox Code Playgroud)

magento

11
推荐指数
1
解决办法
4731
查看次数

标签 统计

magento ×1