Magento get category id by category name and category parent name

magento

I'm able to get category id by using the category name in the snippet below

 $category = Mage::getResourceModel('catalog/category_collection')->addFieldToFilter('name', 'clothing');   
 $cat= $category->getData();
 $categoryid = $cat[0][entity_id];

The problem, however, is if two parent categories have subcategories with the same name, how can I get the category id using the parent name

For instance category Men has subcategory Clothing and category Women also has subcategory Clothing. So using the abve code could return the id for clothing in women or clothing in men. I want to find a way to ensure that I get the subcategory id clothing for men or vice versa.

Any pointers would be deeply appreciated

Best Answer

In this case you want to get the parent first, then get the appropriate child:

$category = Mage::getResourceModel('catalog/category_collection')
    ->addFieldToFilter('name', 'Men')
    ->getFirstItem() // The parent category
        ->getChildrenCategories()
        ->addFieldToFilter('name', 'Clothing')
        ->getFirstItem(); // The child category

$categoryId = $category->getId();
Related Topic