cancel
Showing results for 
Search instead for 
Did you mean: 

How to display products subcategories in parent categories?

   Did you know you can see the translated content as per your choice?

Translation is in progress. Please check again after few minutes.

How to display products subcategories in parent categories?

Hello,

 

I would like show products subcategories in parent category but I don't check a parent category.

 

Because the parent category is empty.

 

Do you understand me?

 

Regards

3 REPLIES 3

Re: How to display products subcategories in parent categories?

Hi Em,

 

Please check below code:-

 

<?php
    $currentCat = Mage::registry('current_category');

    if ( $currentCat->getParentId() == Mage::app()->getStore()->getRootCategoryId() )
    {
        // current category is a toplevel category
        $loadCategory = $currentCat;
    }
    else
    {
        // current category is a sub-(or subsub-, etc...)category of a toplevel category
        // load the parent category of the current category
        $loadCategory = Mage::getModel('catalog/category')->load($currentCat->getParentId());
    }
    $subCategories = explode(',', $loadCategory->getChildren());

    foreach ( $subCategories as $subCategoryId )
    { 

	$cat = Mage::getModel('catalog/category')->load($subCategoryId);
	$category_id =$cat->getId(); // if you know static category then enter number
	$catagory_model = Mage::getModel('catalog/category')->load($subCategoryId); 
	$collection = Mage::getResourceModel('catalog/product_collection');
	$collection->addCategoryFilter($catagory_model); //category filter
	$collection->addAttributeToFilter('status',1); //only enabled product
	$collection->addAttributeToSelect(array('name','url','small_image')); //add product attribute to be fetched
	$collection->getSelect()->order('rand()'); //uncomment to get products in random order     
	$collection->addStoreFilter();          

	if(!empty($collection)){

	    foreach ($collection as $_product):?>

	      <a href="<?php echo $_product->getProductUrl();?>"><?php echo $_product->getName();?>   </a>  

	<?php   endforeach;

	  }
     }
?>

Re: How to display products subcategories in parent categories?

You can also see this 

http://www.blog.plazathemes.com/archives/2429

 

OpenSource Expert | OpenSource Technologies | www.opensourcetechnologies.com
Magento plugins released: http://www.opensourcetechnologies.com/product/product-category/magento-extensions

Re: How to display products subcategories in parent categories?

Thanks you,

 

I see the code but where should I put to prove it?

 

Regards,

Juan