Question

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?

Was it helpful?

Solution

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

$categoryId = $category->getId();

OTHER TIPS

Please try using the following.

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

$categoryId = $_category->getId();
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top