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

Prestashop分类商品总数优化:修改MySQL查询排除库存为0商品

解决Prestashop分类商品统计中排除无库存商品的问题

嗨,如果你需要在Prestashop的分类商品总数统计里,不把库存为0的商品算进去,直接修改对应的MySQL查询语句就能搞定。

找到负责拉取分类商品列表的SQL代码段,它大概长这样:

$sql = 'SELECT p.*, product_shop.*, stock.out_of_stock, IFNULL(p.quantity, 0) as quantity, MAX(product_attribute_shop.id_product_attribute) id_product_attribute, product_attribute_shop.minimal_quantity AS product_attribute_minimal_quantity, pl.`description`, pl.`description_short`, pl.`available_now`, pl.`available_later`, pl.`link_rewrite`, pl.`meta_description`, pl.`meta_keywords`, ...';

在这个查询的WHERE条件部分(或者合适的过滤位置)加上and p.quantity > 0这个条件,修改后的代码就变成:

$sql = 'SELECT p.*, product_shop.*, stock.out_of_stock, IFNULL(p.quantity, 0) as quantity, MAX(product_attribute_shop.id_product_attribute) id_product_attribute, product_attribute_shop.minimal_quantity AS product_attribute_minimal_quantity, pl.`description`, pl.`description_short`, pl.`available_now`, pl.`available_later`, pl.`link_rewrite`, pl.`meta_description`, pl.`meta_keywords`, ... WHERE ... and p.quantity > 0';

小提示

  • 这里的p是商品主表(通常是ps_product,前缀可能根据你的配置有所不同)的别名,要确保你的查询里这个别名对应正确的表,不然过滤条件会失效。
  • 添加p.quantity > 0后,查询会直接排除库存数量为0的商品,分类统计的总数就只会包含有库存的商品了。

内容的提问来源于stack exchange,提问作者Daniel Koczuła

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 03:59:37