为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
皈依舞 2022-01-08 16:52:08
我有一个包含一些子类别的类别:- Lamps-- Hanging lamps-- Wall lamps-- Floor lamps单击分层导航中的三个子类别之一时,产品列表将被过滤到该特定类别的产品。我不希望它过滤,但我希望分层导航中的子类别实际链接到该类别。这是 Magento 2 设置,还是需要自定义更改?如果是这样,有人可以帮我开始吗?我做了一些搜索,但只能找到 Magento 1 的类似问题。
查看完整描述

2 回答

?
哔哔one

TA贡献1854条经验 获得超8个赞

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


如果您想了解有关插件类的更多信息。你可以在这里查看。


查看完整回答
反对 回复 2022-01-08
?
萧十郎

TA贡献1815条经验 获得超13个赞

我正在研究 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;

           }

       }

我希望它对我有帮助。


查看完整回答
反对 回复 2022-01-08
  • 2 回答
  • 0 关注
  • 207 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号