Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Problem concerning Product Collection
 
blockhead
Jr. Member
 
Total Posts:  20
Joined:  2008-05-12
 

This script i made will show the collection of products with in a set of categories. i just have problems with getProductCollection. It seems that it doesnt give me the product collection for the current category in the loop.

$_main_categories=$this->helper('catalog/category')->getStoreCategories();
if(
$_main_categories->count()):
    foreach (
$_main_categories as $_main_category):
        if(
$_main_category->getIsActive()):
                   foreach (
$_main_category->getChildren() as $_main_category_sub):
                           echo 
"Category:" $_main_category_sub->getName() . "<br />";
                           if(
$_main_category_sub->getProductCollection()){
                           
foreach ($_main_category_sub->getProductCollection() as $_main_category_sub_products):
                               echo 
"Product:" $_main_category_sub_products->getName() . "<br />";
                            endforeach;
                           
}
                
endforeach;
        endif;
    endforeach;
endif;
 
Magento Community Magento Community
Magento Community
Magento Community
 
Roderik
Member
 
Avatar
Total Posts:  35
Joined:  2007-09-15
Belgium
 

you have to call ->load() on the collection to fill it

 
Magento Community Magento Community
Magento Community
Magento Community
 
blockhead
Jr. Member
 
Total Posts:  20
Joined:  2008-05-12
 

Thanks roderick but can you give me a script on how to implement the load? tired it a couple of times but i cant seem to get the solution on load.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Roderik
Member
 
Avatar
Total Posts:  35
Joined:  2007-09-15
Belgium
 

$products $category->getProductCollection()->addCategoryFilter($category)->addAttributeToSelect('*');
               
$products->load();
               foreach ( 
$products->toArray() as $product {
                   var_dump
($product);
       
        
}
 
Magento Community Magento Community
Magento Community
Magento Community
 
mzentrale1
Guru
 
Total Posts:  731
Joined:  2007-12-06
Stuttgart, Germany
 

Hi,

it should be $_category->load();

cheers

 
Magento Community Magento Community
Magento Community
Magento Community
 
miked2004
Sr. Member
 
Avatar
Total Posts:  214
Joined:  2007-12-13
Atlanta, Georgia
 

Thanks for the Post. I was seaching for hours on how to do this.

 
Magento Community Magento Community
Magento Community
Magento Community
 
leetaylordoes
Sr. Member
 
Total Posts:  227
Joined:  2008-02-05
San Diego, CA
 

You could also try this for a specific category…

Thanks to @Lee Saferite.

In your .phtml file, you’ll read a line of code similar to this:

$_productCollection=$this->getLoadedProductCollection();

Go underneath and ADD the following (where $cat_id corresponds to the id of the product category you want to display):

$cat_id 6// category_id for "specific category"
$_productCollection Mage::getResourceModel('catalog/product_collection')
->
addCategoryFilter(Mage::getModel('catalog/category')->load($cat_id));

Thanks,
Lee

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top