문제

Currently encountered a problem with catalog/category_collection.

When I'm trying to get category id by it's name it returns wrong category ID.

Anyone else had this problem and know a fix? I tried reindexing data, clearing cache nothing helps.

$category = Mage::getResourceModel('catalog/category_collection')->addFieldToFilter('name', $name);
$cat = $category->getData();
$id = $cat[0][entity_id];

Anyone maybe know an alternative to this code?

도움이 되었습니까?

해결책

The main problem here is that two categories can have the same name and your code does not take that into consideration.

Here's how I would attack this problem:

$collection = Mage::getResourceModel('catalog/category_collection')
    ->addAttributeToSelect('entity_id')
    ->addAttributeToFilter('name', $name);

$ids = $collection->getColumnValues('entity_id');

Thus, your $ids array will contain all the category id with the name you specified.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 magento.stackexchange
scroll top