Php Magento 2如何通过url_键获取类别

Php Magento 2如何通过url_键获取类别,php,magento2,Php,Magento2,我试图通过它的url_键在Magento 2.0中获得一个类别 现在我有: $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $categoryFactory = $objectManager->create('Magento\Catalog\Model\CategoryFactory'); $category = $categoryFactory-&g

我试图通过它的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\U键[1]=>my\U category\U url\U键))


谢谢。

addAttributeToFilter
是一种收集方法。

您应该在类别集合上执行,而不是在类别实例上执行。

尝试下面的代码,我希望您能得到结果

<?php
$objectManagerr = \Magento\Framework\App\ObjectManager::getInstance();
$categoryFactory = $objectManagerr->create('Magento\Catalog\Model\ResourceModel\Category\CollectionFactory');
$categoryy = $categoryFactory->create()
            ->addAttributeToFilter('url_key','your_category_url_key')
            ->addAttributeToSelect('*');

foreach ($categoryy as $productt){

    echo $productt->getName().'<br>';
    echo $productt->getId();
}
?>

/**
*@var\Magento\Catalog\Model\CategoryFactory
******注入构造函数******
*/
受保护的$categoryFactory;
---------
---------
---------
$categories=$this->categorifFactory->create()
->getCollection()
->addAttributeToFilter('url\u key','devops')
->addAttributeToSelect(['entity_id']);
回声“;
打印($categories->getFirstItem()->getEntityId());

根据您的代码,您错过了通过url\u键获取类别的正确方法。 现在我们可以使用方法loadByAttribute,所以您的代码应该是这样的:

namespace Vendor\Module\Model;

use Magento\Catalog\Model\CategoryFactory;

class MyClass {

private $categoryFactory;

public function __construct(
    CategoryFactory $categoryFactory
} {
    $this->categoryFactory = $categoryFactory;
}

public function MyFunction() {
    $categoryFactory = $this->categoryFactory->create();
    $category = $categoryFactory->loadByAttribute('url_key', 'my_category_key');
    $categoryId = $category->getId(); // E.g. if you want the ID.
}

我知道这是个老问题,但万一有人想知道

这里的所有答案都使用ObjectManager。那是个坏习惯。正确的实施方法如下:


这将返回URL键为“my_category_key”的类别对象。

是,我看到了。现在我如何通过url_键进行过滤呢?虽然代码有缺陷,但想法是正确的。
$objectManager   = \Magento\Framework\App\ObjectManager::getInstance();
$categoryFactory = $objectManager->create('Magento\Catalog\Model\CategoryFactory');
$category        = $categoryFactory->create()->loadByAttribute('url_key','my_category_url_key');
namespace Vendor\Module\Model;

use Magento\Catalog\Model\CategoryFactory;

class MyClass {

private $categoryFactory;

public function __construct(
    CategoryFactory $categoryFactory
} {
    $this->categoryFactory = $categoryFactory;
}

public function MyFunction() {
    $categoryFactory = $this->categoryFactory->create();
    $category = $categoryFactory->loadByAttribute('url_key', 'my_category_key');
    $categoryId = $category->getId(); // E.g. if you want the ID.
}