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

如何在Prestashop 1.8后台过滤供应商可见的商品列表?

解决Prestashop 1.8供应商后台商品列表过滤问题

一、正确使用actionAdminProductsListingResultsModifier钩子(优先尝试)

你提到的这个钩子是生效的,大概率是注册或实现环节出错,按以下步骤调整:

1. 在模块中注册钩子

在模块的install()方法里添加钩子注册逻辑:

public function install()
{
    return parent::install() &&
        $this->registerHook('actionAdminProductsListingResultsModifier');
}

2. 实现钩子过滤逻辑

在模块主文件中添加钩子处理方法,通过自定义关联表获取当前供应商ID,再过滤商品列表:

public function hookActionAdminProductsListingResultsModifier($params)
{
    // 获取当前登录员工ID
    $id_employee = (int)$this->context->employee->id;
    
    // 从自定义表匹配对应的供应商ID
    $id_supplier = Db::getInstance()->getValue(
        'SELECT id_supplier FROM `'._DB_PREFIX_.'your_custom_table` WHERE id_employee = '.(int)$id_employee
    );
    
    if ($id_supplier) {
        // 筛选出当前供应商关联的商品
        $filtered_products = [];
        foreach ($params['products'] as $product) {
            // 检查商品是否在商品-供应商关联表中匹配当前供应商
            $is_match = Db::getInstance()->getValue(
                'SELECT 1 FROM `'._DB_PREFIX_.'product_supplier` WHERE id_product = '.(int)$product['id_product'].' AND id_supplier = '.(int)$id_supplier
            );
            if ($is_match) {
                $filtered_products[] = $product;
            }
        }
        // 替换原商品列表
        $params['products'] = $filtered_products;
    }
}

二、正确重写AdminProductsController(钩子无效时备选)

Prestashop 1.8的控制器重写有严格的目录和命名要求,按以下步骤操作:

1. 创建重写文件

在模块目录下建立路径override/controllers/admin/AdminProductsController.php,文件内容如下:

class AdminProductsController extends AdminProductsControllerCore
{
    public function __construct()
    {
        parent::__construct();
        
        // 获取当前员工对应的供应商ID
        $id_employee = (int)$this->context->employee->id;
        $id_supplier = Db::getInstance()->getValue(
            'SELECT id_supplier FROM `'._DB_PREFIX_.'your_custom_table` WHERE id_employee = '.(int)$id_employee
        );
        
        if ($id_supplier) {
            // 关联商品-供应商表并添加过滤条件
            $this->_join .= ' JOIN `'._DB_PREFIX_.'product_supplier` ps ON a.id_product = ps.id_product ';
            $this->_where .= ' AND ps.id_supplier = '.(int)$id_supplier;
        }
    }

    // 可选:重写getList方法确保过滤逻辑生效
    public function getList($id_lang, $orderBy = null, $orderWay = null, $start = 0, $limit = null, $id_lang_shop = false)
    {
        $id_employee = (int)$this->context->employee->id;
        $id_supplier = Db::getInstance()->getValue(
            'SELECT id_supplier FROM `'._DB_PREFIX_.'your_custom_table` WHERE id_employee = '.(int)$id_employee
        );
        
        if ($id_supplier) {
            $this->_join .= ' JOIN `'._DB_PREFIX_.'product_supplier` ps ON a.id_product = ps.id_product ';
            $this->_where .= ' AND ps.id_supplier = '.(int)$id_supplier;
        }
        
        return parent::getList($id_lang, $orderBy, $orderWay, $start, $limit, $id_lang_shop);
    }
}

2. 刷新缓存

创建完重写文件后,删除Prestashop缓存目录下的class_index.php(路径:/cache/class_index.php),让系统加载重写后的控制器。

三、额外检查项

  • 确认自定义表中id_employee和id_supplier的关联数据准确无误;
  • 验证商品与供应商的关联已正确写入Prestashop默认的ps_product_supplier表;
  • 确保当前登录员工是纯供应商权限,未被赋予管理员等高级权限(高级权限会绕过过滤逻辑)。

内容的提问来源于stack exchange,提问作者Beto López

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.09 13:32:46