2013-08-27 178 views
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() 
; 
+0

你会得到什么错误信息? –

+0

SQLSTATE [42000]:语法错误或访问冲突:1064您的SQL语法错误;检查相应的手册;和一段代码错误的东西 – Tonzkie

回答

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

,如果你试图让所有的产品类别上/ 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(); 
}