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

查看:96
本文介绍了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-&gt; addCategoryFilter-按根类别过滤产品集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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