【问题标题】:Magento 2 - Making categories in Layered Navigation link, instead of filterMagento 2 - 在分层导航链接中制作类别,而不是过滤器
【发布时间】:2019-09-12 11:03:08
【问题描述】:

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

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

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

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

【问题讨论】:

  • 产品未被过滤。它显示您从后端添加到类别中的产品。
  • 为什么您希望所有不是吊灯的产品(假设您已经导航到它)都显示在该类别下?
  • 嗨 Shabbir,感谢您的回复。我认为这里有一点误解。单击分层导航中的类别时,产品列表实际上正在被过滤。例如,在类别“Meubels”(家具)中,我有一个子类别“Stoelen”(椅子)。在分层导航中单击“Stoelen”时,会发生这种情况:i.imgur.com/J44gJzS.png URL 看起来像:/meubels?cat=51 我不希望它“过滤”页面上的列表,我希望它链接到实际的“Stoelen”类别。它应该链接到 /meubels/stoelen/ 。

标签: php magento magento2


【解决方案1】:

您需要使用自定义插件类。您可以在哪里更改 Magento\Catalog\Model\Layer\Filter\Item 类中 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)。在 main 方法 (getUrl) 之后运行。

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

【讨论】:

  • 我很抱歉延迟回答。再次米兰,我不知道该怎么感谢你!这对我很有帮助。我的目光落在了一个可以为我做到这一点的插件上,但成本是 300 美元以上,但对于这么小的项目来说,这绝对不值得。你是个传奇。
  • 虽然在技术上可行,但这种解决方案效率非常低,并且会单独加载每个子类别,这意味着为每个子类别读取一个新的数据库,这会很慢。更好的解决方案是在渲染的早期获取所有过滤器值并一次性获取所有类别 - 尽管我不知道最好的插件点
  • 虽然在技术上可行,但这种解决方案效率非常低,并且会单独加载每个子类别,这意味着为每个子类别读取一个新的数据库,这会很慢。更好的解决方案是在渲染的早期获取所有过滤器值并一次性获取所有类别 - 尽管我不知道最好的插件点
【解决方案2】:

我正在开发 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;
           }
       }

我希望它对我有所帮助。

【讨论】:

    猜你喜欢
    • 2014-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多