【问题标题】:Magento 2 How to get category by url_keyMagento 2 如何通过 url_key 获取类别
【发布时间】:2016-01-28 10:01:00
【问题描述】:

我尝试通过它的 url_key 在 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(Array ( [0] => url_key [1] => my_category_url_key ) )

谢谢。

【问题讨论】:

    标签: php magento2


    【解决方案1】:
    /**
     * @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());
    

    【讨论】:

      【解决方案2】:

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

      这里的所有答案都使用 ObjectManager。这是不好的做法。正确的实现方式如下:

      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.
      }
      

      在本例中,category 将包含 URL 键为“my_category_key”的类别的对象。

      【讨论】:

        【解决方案3】:

        addAttributeToFilter 是一种集合方法。
        您应该在类别集合上执行,而不是在类别实例上执行。

        【讨论】:

        • 是的,我明白了。现在如何按 url_key 过滤?
        【解决方案4】:

        试试下面的代码,我希望你能得到你的结果。

        <?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();
        }
        ?>
        

        【讨论】:

        • 虽然代码有问题,但思路是正确的。
        【解决方案5】:

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2014-08-10
          • 2015-05-04
          • 2020-03-05
          • 2017-05-04
          • 1970-01-01
          • 2019-08-09
          • 1970-01-01
          相关资源
          最近更新 更多