我试图通过它的url_键在magento 2.0中获取一个类别。
现在我有:

        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $categoryFactory = $objectManager->create('Magento\Catalog\Model\CategoryFactory');
        $category = $categoryFactory->create()
            ->addAttributeToFilter('url_key','my_category_url_key');

它返回此错误:
筛选模板时出错:方法无效
magento\catalog\model\category\interceptor::addAttributeToFilter(数组
([0]=>url_键[1]=>我的分类_url_键)
谢谢。

最佳答案

/**
 * @var \Magento\Catalog\Model\CategoryFactory
 ****** inject in constructor ******
 */
protected $categoryFactory;

---------
---------
---------
$categories = $this->categoryFactory->create()
            ->getCollection()
            ->addAttributeToFilter('url_key','devops')
            ->addAttributeToSelect(['entity_id']);
echo "<pre>";
print_r($categories->getFirstItem()->getEntityId());

08-25 10:04