You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Magento2前端移除商品后,分层导航与分页未同步问题求助

解决Magento2商品过滤后分层导航与分页不同步的问题

问题根源

你当前仅在aroundGetSelectCountSql中添加过滤逻辑,只能修正商品列表的显示,但分层导航的筛选计数、分页总数计算依赖Layer层的商品集合以及集合加载阶段的统一过滤规则,仅修改计数查询无法覆盖所有场景。

解决方案

需要从两个核心环节添加过滤逻辑,确保商品集合、计数查询、分层导航集合都应用统一规则:


1. 修正商品集合插件:同步集合加载与计数查询的过滤规则

修改ProductCollectionPlugin,在集合加载前(beforeLoad)添加过滤逻辑,替代仅在计数方法中处理的方式:

namespace Vendor\ProductDisplay\Plugin\Product\Collection;

use Magento\Catalog\Model\ResourceModel\Product\Collection;

class ProductCollectionPlugin
{
    /**
     * 在商品集合加载前添加过滤规则
     */
    public function beforeLoad(Collection $subject, $printQuery = false, $logQuery = false)
    {
        $productIdsToRemove = [/* 替换为你的目标商品ID数组 */];
        if (!empty($productIdsToRemove)) {
            // 使用Magento原生方法添加过滤,自动处理表别名等问题
            $subject->addFieldToFilter('entity_id', ['nin' => $productIdsToRemove]);
        }
        return [$printQuery, $logQuery];
    }

    /**
     * 确保计数查询同步过滤规则(可选,若beforeLoad已生效可省略)
     */
    public function aroundGetSelectCountSql(Collection $subject, \Closure $proceed)
    {
        $select = $proceed();
        $productIdsToRemove = [/* 与beforeLoad中一致的ID数组 */];
        if (!empty($productIdsToRemove)) {
            $select->where('e.entity_id NOT IN (?)', $productIdsToRemove);
        }
        return $select;
    }
}

2. 给Layer层添加插件:同步分层导航的集合过滤

分层导航的筛选计数依赖Layer类准备的商品集合,需在prepareProductCollection方法后追加过滤:

首先在di.xml中注册插件:

<type name="Magento\Catalog\Model\Layer">
    <plugin name="vendor_productdisplay_layer_collection_filter"
            type="Vendor\ProductDisplay\Plugin\Catalog\Model\LayerPlugin"
            sortOrder="-99"/>
</type>

然后编写LayerPlugin类:

namespace Vendor\ProductDisplay\Plugin\Catalog\Model;

use Magento\Catalog\Model\Layer;
use Magento\Catalog\Model\ResourceModel\Product\Collection;

class LayerPlugin
{
    /**
     * 在Layer准备商品集合后追加过滤规则
     */
    public function afterPrepareProductCollection(Layer $subject, Collection $collection)
    {
        $productIdsToRemove = [/* 与之前一致的ID数组 */];
        if (!empty($productIdsToRemove)) {
            $collection->addFieldToFilter('entity_id', ['nin' => $productIdsToRemove]);
        }
        return $collection;
    }
}

3. 收尾操作

执行以下命令清除缓存并刷新索引:

bin/magento cache:flush
bin/magento indexer:reindex

关键说明

  • 优先使用addFieldToFilter而非直接操作getSelect()->where(),避免表别名、字段映射等SQL错误;
  • 确保两处插件中$productIdsToRemove的获取逻辑完全一致(比如基于当前用户、店铺的动态规则);
  • sortOrder="-99"确保你的过滤逻辑在其他插件之后执行,避免被后续规则覆盖。

内容的提问来源于stack exchange,提问作者user2217288

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 23:17:05