Magento按类别名称和类别父名称获取类别ID

Magento按类别名称和类别父名称获取类别ID

问题描述:

我可以通过使用以下代码段中的类别名称来获取类别ID

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];

但是,问题是,如果两个父类别的子类别具有相同的名称,如何使用父名称获取类别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

例如类别Men的子类别为服装,类别Women的子类别也为服装.因此,使用abve代码可能会返回女性的服装ID或男性的服装ID.我想找到一种方法来确保我获得男士的子类别ID服装,反之亦然.

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.

任何指针都将不胜感激

在这种情况下,您想先获取父对象,然后获取合适的孩子:

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();