Magento ->addCategoryFilter - 按根类别过滤产品集合 [英] Magento ->addCategoryFilter - filtering product collection by root category

查看:18
本文介绍了Magento ->addCategoryFilter - 按根类别过滤产品集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于在产品集合上使用 ->addCategoryFilter.为什么不能按根类别过滤?我有 2 个商店,它们都有不同的根类别.我想在我的主页上显示最畅销产品的列表.但我无法按根类别过滤产品,只能按其下的子类别过滤.

Concerning the use of the ->addCategoryFilter on a product collection. Why is it not possible to filter by the root category? I have 2 stores, both with different root categories. I want to show a list of best-selling products on my homepage. But I can't filter the products by the root category, only sub-categories under that.

所以,在我的主页上,两家商店的所有产品都显示出来.我可以按任何子类别过滤确定.例如,我的第二个根类别的 ID 为 35.如果我尝试按此过滤,我会从两个根中获得所有产品.但是该根目录下的第一个子类别是 ID 36,按此过滤可以正常工作,仅显示那些产品.我的调用如下(简体):

So, on my homepage, all products from both stores show up. I can filter ok by any sub-categories. For example, my second root category has an ID of 35. If I try to filter by this, I get every product from both roots. But the first sub-category under that root is ID 36, and filtering by this works correctly, showing only those products. My call is as follows (simplified):

$_category = Mage::getModel('catalog/category')->load(35);

$_testproductCollection = Mage::getResourceModel('catalog/product_collection')
->addCategoryFilter($_category)
->addAttributeToSelect('*');
$_testproductCollection->load();

foreach($_testproductCollection as $_testproduct){ 
echo $this->htmlEscape($_testproduct->getName())."<br/>"; 
};

有人知道为什么这不起作用吗?还是有其他方法可以按根类别过滤?

Anyone know why this isn't working? Or is there some other way to filter by the root category?

更新:我仍然没有任何运气.看起来很麻烦 - 添加使用根类别的类别过滤器仅在有时有效,您需要将所有产品添加到根目录,然后保存,然后删除不应该在该根目录中的任何产品,然后重新保存.但是,如果您重新编制索引,则会再次显示所有产品.如果我从上面的集合调用中输出 sql 查询,我会得到以下信息:

UPDATE: I still haven't had any luck with this. Seems very buggy - adding a category filter using the root category only works sometimes, you need to add all products to the root, then save, then remove any that shouldn't be in that root cat, then re-save. But if you re-index you get all products showing again. If I output the sql query from my above collection call I get the following:

SELECT `e`.*, `cat_index`.`position` AS `cat_index_position`, `price_index`.`price`, `price_index`.`tax_class_id`, `price_index`.`final_price`, IF(`price_index`.`tier_price`, LEAST(`price_index`.`min_price`, `price_index`.`tier_price`), `price_index`.`min_price`) AS `minimal_price`, `price_index`.`min_price`, `price_index`.`max_price`, `price_index`.`tier_price` FROM `catalog_product_entity` AS `e` INNER JOIN `catalog_category_product_index` AS `cat_index` ON cat_index.product_id=e.entity_id AND cat_index.store_id='2' AND cat_index.category_id='35' INNER JOIN `catalog_product_index_price` AS `price_index` ON price_index.entity_id = e.entity_id AND price_index.website_id = '1' AND price_index.customer_group_id = 0

如您所见,类别已在此处列出,那么为什么过滤器不起作用?

As you can see, the category is listed there, so why doesn't the filter work?

推荐答案

好的,我认为这有效,还没有测试太多,但似乎已经成功了.您需要首先获取您的商店根类别 ID,然后加入一些字段以便您可以访问产品category_id",然后使用它进行过滤:

OK, I think this works, haven't tested too much but seems to have done the trick. You need to first get your stores root category id, then join some fields so you have access to the products "category_id", then filter using that:

$_rootcatID = Mage::app()->getStore()->getRootCategoryId();

$_testproductCollection = Mage::getResourceModel('catalog/product_collection')
->joinField('category_id','catalog/category_product','category_id','product_id=entity_id',null,'left')
->addAttributeToFilter('category_id', array('in' => $_rootcatID))
->addAttributeToSelect('*');
$_testproductCollection->load();

foreach($_testproductCollection as $_testproduct){ 
    echo $this->htmlEscape($_testproduct->getName())."<br/>"; 
};

这篇关于Magento ->addCategoryFilter - 按根类别过滤产品集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆