0

试图将所有产品归入一个类别问题是我的代码有问题。希望对我做错的事情有任何帮助。这是我的代码。

$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()
 ;
4

3 回答 3

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
于 2013-08-27T07:55:06.077 回答
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();
        }
于 2013-08-27T08:51:40.157 回答
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();
}
于 2013-08-27T08:45:07.330 回答