php - 在 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
即使在添加语句之后,仍有一些事情仍然不起作用:
- 我永远停留在第 1 页 - 即使直接在 URL 中设置页码,我也无法导航到更远的地方。
- 最后页码在寻呼机中计算不正确。
谢谢
解决方案
推荐阅读
- nginx - 如何使用 NGINX 进行 DNS 映射
- next.js - 为什么 next/image 会生成重置我的 CSS 的内联样式?
- r - 如何捕获位于 HTML 页面的 h2 节点之间的特定值?
- spring-boot - 使用 Flyway 迁移执行复杂的价值分配
- javascript - 在 Google Sheets 中使用数组公式并为 Google App Script 提取值
- ibm-cloud - 如何暂停 OpenWhisk 触发器
- xml - 我想重新格式化 xml 中的信息,这可以使用 xslt 实现吗?
- latex - 将自定义单词添加到 LaTex 中的交叉引用
- vba - 使用 VBA PowerPoint 删除单元格边框?
- routes - Discover / Autosuggest 和 calculateroute API 不会返回从同一点 a 到同一点 b 的相同距离