首页 > 解决方案 > 在 Magento 2.4 中结合分层导航和按名称搜索

问题描述

我正在尝试在我的类别页面上添加一个搜索框,我的用户可以使用它来过滤(按名称)他们正在查看的类别中的产品。它还应该与分层导航已经支持的现有过滤器一起使用。

我尝试使用插件并连接到afterGetProductCollection,但它不起作用。这是我尝试过的:

di.xml:

<type name="Magento\Catalog\Model\Layer">
    <plugin name="LayerPlugin" type="Vendor\Module\Model\Plugin\Layer"/>
</type>

层.php

public function afterGetProductCollection($subject, $collection)
{
    $search = $this->request->getParam('q');
    if ($search) {
        $collection->addAttributeToFilter('name', ['like' => '%' . $search . '%']);
        $collection->getSize();
    }

    return $collection;
}

如果我不包括$collection->getSize()它似乎只在第一页上过滤。

getSize即使在添加语句之后,仍有一些事情仍然不起作用:

谢谢

标签: phpmagento2layered-navigation

解决方案


推荐阅读