Magento 1.7 Filter products by multiple categories

后端 未结 2 668
甜味超标
甜味超标 2021-01-16 08:09

I am looking for a way to filter the products being returned on a category page by the current category AND an optional sub-category. Every solution I have seen so far has b

2条回答
  •  迷失自我
    2021-01-16 08:46

    About error Item (Mage_Catalog_Model_Product) with the same id "30674" already exist' in /magento/lib/Varien/Data/Collection.php:373, I found the solution:

    $conditions = array();
    foreach ($categoryIds as $categoryId) {
        if (is_numeric($categoryId)) {
            $conditions[] = "{{table}}.category_id = $categoryId";
        }
    }
    $collection->distinct(true)
        ->joinField('category_id', 'catalog/category_product', /* 'category_id' */null, 
             'product_id = entity_id', implode(" OR ", $conditions), 'inner');
    
    1. set distinct
    2. don't include the category_id field in select clause

提交回复
热议问题