Magento 2 - 在分层导航 link 中制作类别,而不是过滤器

Magento 2 - Making categories in Layered Navigation link, instead of filter

我有一个包含一些子类别的类别:

- Lamps
-- Hanging lamps
-- Wall lamps
-- Floor lamps

单击分层导航中的三个子类别之一时,产品列表将过滤为该特定类别的产品。我不希望它进行过滤,但我希望分层导航中的子类别实际上 link 到类别。

这是 Magento 2 设置,还是需要自定义更改?如果是这样,有人可以帮助我开始吗?我进行了一些搜索,但只能找到与 Magento 1 类似的问题。

您需要使用自定义插件class。您可以在 Magento\Catalog\Model\Layer\Filter\Item class 中更改 getUrl 方法的核心行为。因为这个 getUrl 方法负责生成所有过滤器 URL。

app/code/Vendor/Module/etc/di.xml

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Catalog\Model\Layer\Filter\Item">
        <plugin disabled="false" name="Vendor_Module_Plugin_Magento_Catalog_Model_Layer_Filter_Item" sortOrder="10" type="Vendor\Module\Plugin\Magento\Catalog\Model\Layer\Filter\Item"/>
    </type>
</config>

app/code/Vendor/Module/Plugin/Magento/Catalog/Model/Layer/Filter/Item.php

<?php
namespace Vendor\Module\Plugin\Magento\Catalog\Model\Layer\Filter;

class Item
{
    protected $categoryHelper;
    protected $categoryRepository;

    public function __construct(
        \Magento\Catalog\Helper\Category $categoryHelper,
        \Magento\Catalog\Model\CategoryRepository $categoryRepository
    ) {
        $this->categoryHelper = $categoryHelper;
        $this->categoryRepository = $categoryRepository;
    }

    public function afterGetUrl(
        \Magento\Catalog\Model\Layer\Filter\Item $subject,
        $result
    ) {
        // custom url for category filter
        if (strtolower($subject->getName()) == 'category') {
            $categoryId = $subject->getValue();
            $categoryObj = $this->categoryRepository->get($categoryId);
            return $this->categoryHelper->getCategoryUrl($categoryObj);
        }

        return $result;
    }
}

这里使用了after插件方法(afterGetUrl)。它在主要方法 (getUrl) 之后运行。

如果您想了解更多关于插件的信息class。您可以查看 here.

我正在使用 magento 2.3.3 - 2.3.4,这个解决方案对我不起作用, 但这很有帮助。我需要稍微更改一下代码。

这是我的解决方法:

    <?php namespace Vendor\Module\Plugin\Magento\Catalog\Model\Layer\Filter;

       class Item
       {
           protected $categoryHelper;
           protected $categoryRepository;

           public function __construct(
               \Magento\Catalog\Helper\Category $categoryHelper,
               \Magento\Catalog\Model\CategoryRepository $categoryRepository
           ) {
               $this->categoryHelper = $categoryHelper;
               $this->categoryRepository = $categoryRepository;
           }

           public function afterGetUrl(
               \Magento\Catalog\Model\Layer\Filter\Item $subject, $result
           ) {
               // custom url for category filter
               if (strtolower($subject->getFilter()->getRequestVar()) === 'cat') {
                   $categoryId = $subject->getValue();
                   $categoryObj = $this->categoryRepository->get($categoryId);
                   return $this->categoryHelper->getCategoryUrl($categoryObj);
               }

               return $result;
           }
       }

我希望它对我有所帮助。