Magento Forum

Filter Products By Root Category
 
nigelscott
Member
 
Total Posts:  37
Joined:  2009-08-10
 

I have 2 Magento stores. I have Root Catalog 1 for Store 1 and Root Catalog 2 for Store 2. I want to find the categories for each product in Root Catalog 1. The following code appears to pull the information from both Root Catalog 1 and Root Catalog 2. How do I exclude Root Catalog 2 from the data. My current code is:

$collection Mage::getModel('catalog/product')->getCollection();
foreach (
$collection as $currentproduct{
$product 
=  Mage::getModel('catalog/product')->load($currentproduct->getId());
$cats $product->getCategoryIds();
  foreach(
$cats as $category_id){
   $_cat 
Mage::getModel('catalog/category')->load($category_id);
   
$listofcategorynames[] $_cat->getParentCategory()->getName() . "/" $_cat->getName();  
  
}
}
 
Magento Community Magento Community
Magento Community
Magento Community
 
tzyganu
Mentor
 
Avatar
Total Posts:  2205
Joined:  2009-11-18
Bucharest, Romania
 

Hello
Each category has a path attribute. This represents the list of category ids in the tree.
each path looks similar to this: 1/2/45/122.
1 - is the root of the roots (a hidden category needed to generate the tree).
2 - is the root category you see in your back-end
42 - is the parent category
122 - is the current category.
Of course the number of ids in the path depends on the level of each category.
But the firs 2 ids are always there.

You can check in your code if $_cat->getPath() starts with 1/{ROOT 1 id}.

Let me know how it turns out.
Marius.

 
Magento Community Magento Community
Magento Community
Magento Community
 
mcdky
Sr. Member
 
Avatar
Total Posts:  140
Joined:  2011-09-29
 

Try this

Mage::getModel('catalog/category')->getCollection()
    ->
addAttributeToSelect('name')
    ->
setStoreId(<YOUR_STORE_ID>)
    ->
addAttributeToFilter('entity_id', array('in' => $product->getCategoryIds()))
    ->
load();

Hope this helps

 
Magento Community Magento Community
Magento Community
Magento Community
 
squizeers
Member
 
Total Posts:  38
Joined:  2010-06-27
 

I am trying to show only those categories which are active using following code. Right now it gives me all the categories whether active or not. I know I have to use addAttributeToFilter (’is_active’,’1’); . But when i use it, it gives me an error. ANY HELP WILL BE MUCH APPRECIATED.

<?php 
    $groups 
$this->getGroups(); 
    
$cnt count($groups); 
?>
<?php 
if($cnt 1): ?>
    <?php 
foreach ($groups as $_group): ?>            
           <?php 
               $storeId 
$_group->getId();
            
$store_url Mage::app()->getStore($storeId)->getHomeUrl();
            
$root_cat Mage::app()->getStore($storeId)->getRootCategoryId();
            
            
$category_model Mage::getModel('catalog/category');
            
$_category $category_model->load($root_cat); //$categoryid for which the child categories to be found
            
$_img_path Mage::getBaseUrl('media').'catalog/category/';
           
?>
                <?php 
if ($_imgUrl $_category->getThumbnail()): ?>
                
<div class="span2">
                    <
a href="<?php echo $store_url ?>" title="<?php echo $_category->getName(); ?>">
                        <
img class="img-polaroid" src="<?php echo $_img_path.$_imgUrl; ?>" />
                    </
a>
                    <
h6>
                        <
a href="<?php echo $store_url; ?>" title="<?php echo $_category->getName(); ?>">
                            
<?php echo $_category->getName(); ?>
                        
</a>
                    </
h6>
                </
div>
                 
<?php endif; ?>                    
    <?php 
endforeach; ?> 
<?php 
endif; ?>
 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top