[英]Magento 2 - Making categories in Layered Navigation link, instead of filter
I have a category with some subcategories:我有一个包含一些子类别的类别:
- Lamps
-- Hanging lamps
-- Wall lamps
-- Floor lamps
When clicking on one of the three subcategories in the Layered Navigation, the product listing is filtered to the products of that specific category.单击分层导航中的三个子类别之一时,产品列表将过滤为该特定类别的产品。 I don't want it to filter, but I want the subcategories in the Layered Navigation to actually link to the category.
我不希望它过滤,但我希望分层导航中的子类别实际链接到该类别。
Is this a Magento 2 setting, or does this require custom changes?这是 Magento 2 设置,还是需要自定义更改? If so, can anybody help me get started?
如果是这样,有人可以帮助我开始吗? I've done some searching, but was only able to find a similar question for Magento 1.
我已经做了一些搜索,但只能找到 Magento 1 的类似问题。
You need to use a custom plugin class.您需要使用自定义插件类。 Where you can change the core behavior of
getUrl
method in Magento\\Catalog\\Model\\Layer\\Filter\\Item
class.您可以在哪里更改
Magento\\Catalog\\Model\\Layer\\Filter\\Item
类中getUrl
方法的核心行为。 Because this getUrl
method is responsible for generating all filter URLs.因为这个
getUrl
方法负责生成所有过滤器 URL。
app/code/Vendor/Module/etc/di.xml
应用程序/代码/供应商/模块/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
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;
}
}
Here after
plugin method ( afterGetUrl
) is used.这里使用
after
插件方法( afterGetUrl
)。 Which runs after the main method ( getUrl
).在 main 方法 (
getUrl
) 之后运行。
If you want to know more about plugin class.如果您想了解更多关于插件类的信息。 You can check here .
你可以在这里查看。
I am working on magento 2.3.3 - 2.3.4 and this solution does not work for me, but it was very helpful.我正在研究 magento 2.3.3 - 2.3.4,这个解决方案对我不起作用,但它非常有帮助。 I need to change the code little bit.
我需要稍微更改一下代码。
here is my fix:这是我的修复:
<?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;
}
}
I hope it will be helpful as it was for me.我希望它对我有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.