简体   繁体   中英

Retrieve all products under a category

Trying to get all products under a category problem is something is wrong with my code. Would appreciate any help on what I'm doing wrong. Here is my code.

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

in your code, category id is not getting. Please try this code:-

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

if you are trying to get all product on category/view.phtml

than bellow is the code

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM