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

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


- Lamps

-- Hanging lamps

-- Wall lamps

-- Floor lamps

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


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


皈依舞
浏览 208回答 2
2回答

哔哔one

您需要使用自定义插件类。您可以在哪里更改类中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">&nbsp; &nbsp; <type name="Magento\Catalog\Model\Layer\Filter\Item">&nbsp; &nbsp; &nbsp; &nbsp; <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"/>&nbsp; &nbsp; </type></config>app/code/Vendor/Module/Plugin/Magento/Catalog/Model/Layer/Filter/Item.php<?phpnamespace Vendor\Module\Plugin\Magento\Catalog\Model\Layer\Filter;class Item{&nbsp; &nbsp; protected $categoryHelper;&nbsp; &nbsp; protected $categoryRepository;&nbsp; &nbsp; public function __construct(&nbsp; &nbsp; &nbsp; &nbsp; \Magento\Catalog\Helper\Category $categoryHelper,&nbsp; &nbsp; &nbsp; &nbsp; \Magento\Catalog\Model\CategoryRepository $categoryRepository&nbsp; &nbsp; ) {&nbsp; &nbsp; &nbsp; &nbsp; $this->categoryHelper = $categoryHelper;&nbsp; &nbsp; &nbsp; &nbsp; $this->categoryRepository = $categoryRepository;&nbsp; &nbsp; }&nbsp; &nbsp; public function afterGetUrl(&nbsp; &nbsp; &nbsp; &nbsp; \Magento\Catalog\Model\Layer\Filter\Item $subject,&nbsp; &nbsp; &nbsp; &nbsp; $result&nbsp; &nbsp; ) {&nbsp; &nbsp; &nbsp; &nbsp; // custom url for category filter&nbsp; &nbsp; &nbsp; &nbsp; if (strtolower($subject->getName()) == 'category') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $categoryId = $subject->getValue();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $categoryObj = $this->categoryRepository->get($categoryId);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $this->categoryHelper->getCategoryUrl($categoryObj);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return $result;&nbsp; &nbsp; }}这里使用了after插件方法 ( afterGetUrl)。在 main 方法 ( getUrl)之后运行。如果您想了解有关插件类的更多信息。你可以在这里查看。

萧十郎

我正在研究 magento 2.3.3 - 2.3.4,这个解决方案对我不起作用,但它非常有帮助。我需要稍微更改一下代码。这是我的修复:&nbsp; &nbsp; <?php namespace Vendor\Module\Plugin\Magento\Catalog\Model\Layer\Filter;&nbsp; &nbsp; &nbsp; &nbsp;class Item&nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;protected $categoryHelper;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;protected $categoryRepository;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;public function __construct(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\Magento\Catalog\Helper\Category $categoryHelper,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\Magento\Catalog\Model\CategoryRepository $categoryRepository&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$this->categoryHelper = $categoryHelper;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$this->categoryRepository = $categoryRepository;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;public function afterGetUrl(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\Magento\Catalog\Model\Layer\Filter\Item $subject, $result&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// custom url for category filter&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (strtolower($subject->getFilter()->getRequestVar()) === 'cat') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$categoryId = $subject->getValue();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$categoryObj = $this->categoryRepository->get($categoryId);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return $this->categoryHelper->getCategoryUrl($categoryObj);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return $result;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp;}我希望它对我有帮助。
打开App,查看更多内容
随时随地看视频慕课网APP