Magento:来自单独类别及其子类别的产品集合

Magento:来自单独类别及其子类别的产品集合,magento,magento-1.7,Magento,Magento 1.7,我在不同的论坛上试过,发现下面的代码对我很有帮助,但它没有给我任何回报 $limit = ($limit == 0) ? 50 : $limit; $category = Mage::getModel('catalog/category')->load($cat_id); $category->setIsAnchor(1); $products = Mage::getResourceModel('catalog/product_collection

我在不同的论坛上试过,发现下面的代码对我很有帮助,但它没有给我任何回报

    $limit = ($limit == 0) ? 50 : $limit;
    $category = Mage::getModel('catalog/category')->load($cat_id);

    $category->setIsAnchor(1);

    $products = Mage::getResourceModel('catalog/product_collection')
            ->addAttributeToSelect('*')
            ->addAttributeToFilter("status", array("eq" => 1))
            ->addAttributeToFilter("visibility", array("eq" => 1))
            ->setPageSize($limit)
            ->addCategoryFilter($category)
            ->load();

$products
的长度始终为零。

好吧,我们需要设置
storeId
来获取collection w.r.t类别。我使用了以下代码

    $category = Mage::getModel('catalog/category')->load($cat_id);

    $products = Mage::getResourceModel('catalog/product_collection')
            ->setStoreId(1)
            ->setPageSize($limit)
            ->addAttributeToFilter(
                    'status',
                    array('eq' => Mage_Catalog_Model_Product_Status::STATUS_ENABLED)
            )
            ->addAttributeToFilter('visibility',
                    Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)
            ->addCategoryFilter($category);