Magento ->addCategoryFilter - 按根类别过滤产品集合

发布于 2024-10-18 17:01:57 字数 1668 浏览 2 评论 0原文

关于在产品集合上使用->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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

无人问我粥可暖 2024-10-25 17:01:57

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

$_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/>"; 
};

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/>"; 
};
噩梦成真你也成魔 2024-10-25 17:01:57

我有一个类似的问题,最后在这里回答我的问题:
https://magento.stackexchange.com/questions/24436/product-collection-for-默认类别

$store_id = 1;
$root_category_id = Mage::app()->getStore(Mage::app()->getStore()->getId())->getRootCategoryId();

$collection = Mage::getResourceModel('catalog/product_collection')->setStoreId($store_id);
$model = Mage::getModel('catalog/product')->setStoreId($store_id);
$category_model = Mage::getModel('catalog/category');
$category = $category_model->load($root_category_id);
$category->setIsAnchor(true);
$collection->addCategoryFilter($category);

echo $collection->getSize(); // example to show you a count of products

I had a similar question and ended up answering my question here:
https://magento.stackexchange.com/questions/24436/product-collection-for-default-category

$store_id = 1;
$root_category_id = Mage::app()->getStore(Mage::app()->getStore()->getId())->getRootCategoryId();

$collection = Mage::getResourceModel('catalog/product_collection')->setStoreId($store_id);
$model = Mage::getModel('catalog/product')->setStoreId($store_id);
$category_model = Mage::getModel('catalog/category');
$category = $category_model->load($root_category_id);
$category->setIsAnchor(true);
$collection->addCategoryFilter($category);

echo $collection->getSize(); // example to show you a count of products
我三岁 2024-10-25 17:01:57

试试这个:

<代码>
$_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('*');

蓝天白云 2024-10-25 17:01:57

它没有尝试过,但我有两个想法:

(1)您可以尝试向类别添加一个额外的商店过滤器:

$collection = Mage::getModel('catalog/category')
     ->getCollection()
     ->addFieldToFilter('store_id', Mage::app()->getStore()->getId())
     ->addIdFilter(35); 

foreach ($collection as $item) {
     ...
}

这不是很干净,因为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:

$collection = Mage::getModel('catalog/category')
     ->getCollection()
     ->addFieldToFilter('store_id', Mage::app()->getStore()->getId())
     ->addIdFilter(35); 

foreach ($collection as $item) {
     ...
}

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.

残月升风 2024-10-25 17:01:57

场景:

  • Magento 版本1.7.0.2
  • 只是
  • 在全局模式下存储编辑的类别
  • 删除了旧的根类别。所以旧产品没有类别。
  • 表“catalog_category_product_index”仍然处于不良状态(即使在重新索引之后)并显示新的根类别!

我构建了一个辅助方法:

    public function getCategoriesId($parent =  -1,$recursionLevel = -1, $asCollection = true)
{
    if($parent === -1){
        $categories = Mage::helper('catalog/category')->getStoreCategories(false, true);
    }else{
        $recursionLevel  = ($recursionLevel<0) ? max(0, (int) Mage::app()->getStore()->getConfig('catalog/navigation/max_depth')) : $recursionLevel;
        $categories = $category->getCategories($parent, $recursionLevel, false, $asCollection, true);
    }

    $ids = array();

    foreach($categories as $category){
        if($category->getId()){
            $ids[] = $category->getId();
        }
    }

    return $ids;
}

我使用结果来过滤类别,如 jazzhand 所写:

    ->addAttributeToFilter('category_id', array('in' => $_rootcatID))

我不太确定,如果它不仅仅需要根类别。

scenario:

  • Magento Version 1.7.0.2
  • just one Store
  • edited categories in the global mode
  • deleted old root-category. So old products had no categories.
  • Table "catalog_category_product_index" remains in a bad condition (even after reindexing) and shows to new root-category!

I build a helper-method:

    public function getCategoriesId($parent =  -1,$recursionLevel = -1, $asCollection = true)
{
    if($parent === -1){
        $categories = Mage::helper('catalog/category')->getStoreCategories(false, true);
    }else{
        $recursionLevel  = ($recursionLevel<0) ? max(0, (int) Mage::app()->getStore()->getConfig('catalog/navigation/max_depth')) : $recursionLevel;
        $categories = $category->getCategories($parent, $recursionLevel, false, $asCollection, true);
    }

    $ids = array();

    foreach($categories as $category){
        if($category->getId()){
            $ids[] = $category->getId();
        }
    }

    return $ids;
}

I used the result to filter the categories like jazzhand written:

    ->addAttributeToFilter('category_id', array('in' => $_rootcatID))

I'm not really sure, if its not just needed for the root-category.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文