[英]Magento 2 - Making categories in Layered Navigation link, instead of filter
我有一個包含一些子類別的類別:
- Lamps
-- Hanging lamps
-- Wall lamps
-- Floor lamps
單擊分層導航中的三個子類別之一時,產品列表將過濾為該特定類別的產品。 我不希望它過濾,但我希望分層導航中的子類別實際鏈接到該類別。
這是 Magento 2 設置,還是需要自定義更改? 如果是這樣,有人可以幫助我開始嗎? 我已經做了一些搜索,但只能找到 Magento 1 的類似問題。
您需要使用自定義插件類。 您可以在哪里更改Magento\\Catalog\\Model\\Layer\\Filter\\Item
類中getUrl
方法的核心行為。 因為這個getUrl
方法負責生成所有過濾器 URL。
應用程序/代碼/供應商/模塊/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
) 之后運行。
如果您想了解更多關於插件類的信息。 你可以在這里查看。
我正在研究 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;
}
}
我希望它對我有幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.