Magento按类别名称和类别父名称获取类别ID [英] Magento get category id by category name and category parent name

查看:102
本文介绍了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();

这篇关于Magento按类别名称和类别父名称获取类别ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆