1

I'm trying to get all products of a certain category. Unfortunately I always get not only all products of the category but also all products from its child categories. What am I doing wrong?

For example I'm working with following code snippet:

$catId = 160;
$category = Mage::getModel('catalog/category')->load($catId);
$collection = $category->getProductCollection()->addAttributeToSort('position');
Mage::getModel('catalog/layer')->prepareProductCollection($collection);

foreach ($collection as $product) {
var_dump( $product->getId() );
}

Above category (ID 160) contains 17 products but I get 51 products. If I look these up I see that all these products are assigned to one of the child categories of category ID 160. Sometimes even a sub-sub-category of #160.

Ran a full reindex - didn't help. Caches are all disabled. Magento ver. 1.9.2.0

Thank you so much in advance!

3 Answers 3

3

Figured it out myself.

$category_id = Mage::getModel('catalog/layer')->getCurrentCategory()->getId();
$category = Mage::getModel('catalog/category')->load($category_id);
$products = Mage::getModel('catalog/product')
    ->getCollection()
    ->addAttributeToSelect('*')
    ->addCategoryFilter($category)
    ->setOrder('price', 'ASC')
    ->load();

you can also use for more than one category as bellow:

$categoryIds = array(2,4);//category id

$collection = Mage::getModel('catalog/product')
                             ->getCollection()
                             ->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id = entity_id', null, 'left')
                             ->addAttributeToSelect('*')
                             ->addAttributeToFilter('category_id', array('in' => $categoryIds))

I hope this will help you.

4
  • The first code snippet gives me the same result (all products of child categories). But the second one works for me!!! I'm happy to get it fixed now but I'm also wondering why the first one does not work. It should though! If anyone has any idea why it does not work - please share! @Denish Thank you so much for your post!
    – Stefan
    Dec 30, 2015 at 11:06
  • if you are happy with my answer than accept it so this can help to other our buddies also. and once again i welcomes you buddy. Dec 30, 2015 at 11:08
  • 2
    Figured it out myself. what is this? Dec 30, 2015 at 12:25
  • ok buddy wel done...!!! Dec 30, 2015 at 12:30
0

Try following code, it might help you out:

/**
 * If you want to display products from current category
 */ 
$category = Mage::registry('current_category'); 

/**
 * If you want to display products from any specific category
 */ 
$categoryId = 10;
$category = Mage::getModel('catalog/category')->load($categoryId);

/**
 * Getting product collection for a particular category 
 */
$prodCollection = Mage::getResourceModel('catalog/product_collection')
            ->addCategoryFilter($category)
            ->addAttributeToSelect('*')
            ->addAttributeToFilter('level', 2);

/**
 * Applying status and visibility filter to the product collection
 * i.e. only fetching visible and enabled products
 */
Mage::getSingleton('catalog/product_status')
    ->addVisibleFilterToCollection($prodCollection);    

Mage::getSingleton('catalog/product_visibility')
    ->addVisibleInCatalogFilterToCollection($prodCollection); 

/** 
 * Printing category and products name
 */ 
echo '<strong>'.$category->getName().'</strong><br />';
foreach ($prodCollection as $val) {
    echo $val->getName() . '<br />';
}

This is how level value you need to use:

0 - Root of all roots (not visible in the backend)

1 - Catalog Root

2 - top level categories

3+ - go lower in the category tree

2
  • Thanks. But same result
    – Stefan
    Dec 30, 2015 at 11:07
  • @Phil Please try edited answer Dec 30, 2015 at 11:15
0

make sure is Anchor selected as "Yes" for category #160

make sure is Anchor selected as "Yes" for category #160

$catId = 160;
$category   = Mage::getModel('catalog/category')->load($catId);
$collection = $category->getProductCollection()
    ->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
    ->addMinimalPrice()
    ->addFinalPrice()
    ->addTaxPercents()
    ->addUrlRewrite($category->getId())
    ->addAttributeToSort('position');

Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);

foreach ($collection as $product) {
    var_dump( $product->getId() );
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.