2013-10-21 99 views
0

我嘗試過不同的論壇,並找到以下代碼對我有幫助,但它不會爲我返回任何內容。Magento:來自某個特定類別的產品收集及其子類別

$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始終爲零長度。

回答

0

那麼,我們需要設置storeId獲取集合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); 
相關問題