2 回答

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)之后運行。
如果您想了解有關插件類的更多信息。你可以在這里查看。

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;
}
}
我希望它對我有幫助。
- 2 回答
- 0 關注
- 202 瀏覽
添加回答
舉報