0

I need to find a solution to get product current subcategory from specific parent category.

For example: I have a product that has like 2 parent categories like authors and publishers. And in those i have like 500-800 subcategories, and this product has one of subcategories in each parent categories selected. I need to get those subcategory names. My solution:

function getSubsName($_product,$cat_id)
    {
        $categories = $_product->getCategoryIds(); 
        $cat = Mage::getModel('catalog/category')->load($cat_id); 
        $subcats = $cat->getChildren(); 
        $content = "";
        foreach(explode(',',$subcats) as $subCatid):
           $_category = Mage::getModel('catalog/category')->load($subCatid);
           foreach($categories as $cat_id):
              if($cat_id == $_category->getId()):
                  $content = '<a href="' . $_category->getUrl() .'">' .      $_category->getName() . '</a>';
              endif;
           endforeach;
        endforeach;
        return $content;
    }

But there is a problem because it loads very long. Like 8-10 seconds or more. I can't afford that. Anyone know any more simple and fast solution for this?

  • Is this in detail view or list view for products? and why do you do if($cat_id == $_category->getId()):? You'll only get the category for which you gave the ID as an argument in the method – Sander Mangel Sep 2 '15 at 11:59
  • @SanderMangel - This is only called in product view page. And you missed the foreach($categories as $cat_id): Sorry for using same name sometimes my programming can be little bit confusing :) – Žygimantas Baranauskas Sep 2 '15 at 12:13
1

One reason for it going slow is the number of loads you are doing in a loop. Also you are loading the initial category when you do not need to.

I have taken the liberty of refactoring your function. Hope you don't mind :)

function getSubsName($product, $parentId)
{
    $content = "";

    // get the product categories
    $categories = $product->getCategoryIds();

    // get all children of the parent $parentId
    $children = Mage::getModel('catalog/category')->getCategories($parentId, 0, false, true);
    foreach ($children as $category) {

        // if the child is in the product categories,
        // include it
        if (in_array($category->getId(), $categories)) {
            $content .= '<a href="'.$category->getUrl().'">'.$category->getName().'</a>';
        }
    }

    return $content;
}
1

You're doing a load of loads which means heavy database queries that aren't cached. Since you basically need a group of categories with limited data I would recommend a collection

function getSubsName($_product,$cat_id)
{
    $categories = $_product->getCategoryIds(); 
    $categoryCollection = Mage::getModel('catalog/category')->getCollection()
        ->addAttributeToSelect(array('name', 'url'))
        ->addAttributeToFilter('parent_id', array('in' => $categories));

    $content = "";
    foreach ($categoryCollection as $_category) {
        $content = '<a href="' . $_category->getUrl() .'">' .      $_category->getName() . '</a>';
    }

    return $content;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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