Magento ->addCategoryFilter - 按根类别过滤产品集合
关于在产品集合上使用->addCategoryFilter。为什么不能按根类别过滤?我有 2 家商店,都有不同的根类别。我想在我的主页上显示最畅销产品的列表。但我无法按根类别过滤产品,只能按根类别过滤产品。
因此,在我的主页上,两家商店的所有产品都会显示。我可以按任何子类别过滤。例如,我的第二个根类别的 ID 为 35。如果我尝试按此进行过滤,我会从两个根中获取所有产品。但该根下的第一个子类别是 ID 36,并且按此进行过滤可以正常工作,仅显示这些产品。我的电话如下(简化):
$_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/>";
};
有人知道为什么这不起作用吗?或者还有其他方法可以按根类别进行过滤吗?
更新: 我在这方面仍然没有任何运气。似乎非常错误 - 使用根类别添加类别过滤器有时仅有效,您需要将所有产品添加到根目录,然后保存,然后删除任何不应该在该根目录中的产品,然后重新保存。但如果您重新索引,所有产品都会再次显示。如果我从上面的集合调用中输出 sql 查询,我会得到以下信息:
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
如您所见,类别列在那里,那么为什么过滤器不起作用?
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.
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?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
好吧,我认为这可行,没有测试太多,但似乎已经成功了。您需要首先获取商店根类别 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:
我有一个类似的问题,最后在这里回答我的问题:
https://magento.stackexchange.com/questions/24436/product-collection-for-默认类别
I had a similar question and ended up answering my question here:
https://magento.stackexchange.com/questions/24436/product-collection-for-default-category
试试这个:
<代码>
$_testproductCollection = Mage::getResourceModel('catalog/product_collection')
->setVisibility(数组(self::VISIBILITY_IN_CATALOG, self::VISIBILITY_BOTH))
->addCategoryFilter($_category)
->addAttributeToSelect('*');
Try this:
$_testproductCollection = Mage::getResourceModel('catalog/product_collection')
->setVisibility(array(self::VISIBILITY_IN_CATALOG, self::VISIBILITY_BOTH))
->addCategoryFilter($_category)
->addAttributeToSelect('*');
它没有尝试过,但我有两个想法:
(1)您可以尝试向类别添加一个额外的商店过滤器:
这不是很干净,因为category_id是硬编码的。
(2) 您还可以在集合上尝试
addPathFilter()
(我不知道根类别路径,也许只是“/” - 但您可以在数据库中查找它或使用getPath() 加载的根类别)。也许您还必须使用商店过滤器。It didn't try it out, but I have two ideas:
(1) You can try to add an additional store filter to the category:
Which is not very clean because the category_id is hardcoded.
(2) You could also try
addPathFilter()
on the collection (i don't know the root category path by heart, maybe simply "/" - but you can look it up in the database or use getPath() on the loaded root category). Maybe you have to use a store filter, too.场景:
我构建了一个辅助方法:
我使用结果来过滤类别,如 jazzhand 所写:
我不太确定,如果它不仅仅需要根类别。
scenario:
I build a helper-method:
I used the result to filter the categories like jazzhand written:
I'm not really sure, if its not just needed for the root-category.