检索某个类别下的所有产品

时间:2013-08-27 07:51:06

标签: php magento

尝试将所有产品置于类别问题之下是我的代码出了问题。非常感谢我在做错的任何帮助。这是我的代码。

$cattegoryId= Mage::registry('current_category');

 $category = Mage::getModel('catalog/category')->load($categoryId);
 $_productCollection = Mage::getModel('catalog/category')
 ->getCollection()
 ->addAttributeToSelect('*')
 ->addAttributeToFilter('category_id',$category)
 ->setOrder('entity_id', 'DESC')
 ->load()
 ;

3 个答案:

答案 0 :(得分:2)

$category = Mage::registry('current_category'); // object of class Mage_Catalog_Model_Category, not id
$_productCollection = $category->getProductCollection(); // you have this method in the class

答案 1 :(得分:1)

在您的代码中,类别ID未获取。请尝试以下代码: -

$_helper = $this->helper('catalog/output');
$_category_detail=Mage::registry('current_category');
$categoryId= $_category_detail->getId();
$_productCollection = Mage::getModel('catalog/category')->load($categoryId)
      ->getProductCollection()
      ->addAttributeToSelect('*')
      ->addAttributeToFilter('status', 1)
      ->setOrder('entity_id', 'DESC');

    foreach($_productCollection as $product)
       {
             echo $product->getName();
        }

答案 2 :(得分:0)

如果您尝试在category / view.phtml上获取所有产品

比下面是代码

$category = $this->getCurrentCategory();

$categoryId = $category->getId();

$_productCollection = Mage::getModel('catalog/category')->load($categoryId)
->getProductCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('status', 1)
->setOrder('entity_id', 'DESC');

foreach($_productCollection as $product)
{
    echo $product->getName();
}