Magento 如何按一个集合显示子类别的所有产品


Magento How to show all products of subcatgories by one collection

您好,我有一个特定类别的子类别的类别ID,所以我想获取该特定父类别的所有子类别的所有产品。 那么如何获得该产品。

Suppose I have parent category 4 and it's having 5,6,7,8,9,10,11 as a child category so i Then I want all the products of 5,6,7,8,9,10,11's category using single query.
$categories = Mage::getModel('catalog/category')->load('4')->getChildrenCategories();
foreach ($categories as $category) {
    $collection = $category->getProductCollection();
    foreach ($collection as $product) {
        $result[] = $product;
    }
}

即使您不知道子类别的 id,这也应该有效。

您应该使用catalog_category_product_index表加入产品集合。请参见Mage_Catalog_Model_Resource_Product_Collection方法_applyProductLimitations()。

现成的解决方案将是这样的(只是一个想法,可能需要一些修改或过滤器):

$productCollection = Mage::getModel('catalog/product')->getCollection();
$productCollection
    ->getSelect()->join(
            array('cat_index' => $this->getTable('catalog/category_product_index')),
            'cat_index.product_id=e.entity_id',
            array()
        )
    ->where('cat_index.category_id in (?)', $categoryIds);