5

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

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

$categoryId = $category->getId();
0
3

Please try using the following.

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

$categoryId = $_category->getId();

Your Answer

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

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