Looking to find the category ID using the Name attribute programmatically. My script returns a 1 when it should be a 19. Here is my code:

$collection = Mage::getModel('catalog/category')->getCollection()
    ->addAttributeToFilter('is_active', 1) 
    ->addAttributeToFilter('parent_id', $currentCategoryId) 
    ->addAttributeToFilter('name', $categoryName);

which should return the category right? Then the script utilizes $catid = $collection->getFirstItem()->getId(); to get the ID, however it is returning the wrong ID. Any ideas?

2 Answers 2

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

$categoryId = $category->getId();

Please try using the following.

$_category = Mage::getResourceModel('catalog/category_collection')
        ->addFieldToFilter('name', $categoryName)

$categoryId = $_category->getId();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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