亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 關注
  • 202 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號