如何使用 Magento 1.9 中的名称获取 CategoryId?
-
12-12-2019 - |
题
寻找 category ID
使用 Name
以编程方式属性。我的脚本返回 1,而它应该是 19。这是我的代码:
$collection = Mage::getModel('catalog/category')->getCollection()
->addAttributeToFilter('is_active', 1)
->addAttributeToFilter('parent_id', $currentCategoryId)
->addAttributeToFilter('name', $categoryName);
哪个应该返回正确的类别?然后脚本使用 $catid = $collection->getFirstItem()->getId();
获取 ID,但返回错误的 ID。有任何想法吗?
解决方案
$category = Mage::getResourceModel('catalog/category_collection')
->addFieldToFilter('name', 'Men')
->getFirstItem() // The parent category
->getChildrenCategories()
->addFieldToFilter('name', 'Clothing')
->getFirstItem(); // The child category
$categoryId = $category->getId();
其他提示
请尝试使用以下内容。
$_category = Mage::getResourceModel('catalog/category_collection')
->addFieldToFilter('name', $categoryName)
->getFirstItem();
$categoryId = $_category->getId();