2013-08-27 75 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(); 
}