We can't add this item to your shopping cart right now 是 Magento 2 把底层异常转换后的通用提示。真正原因可能是必选项缺失、Quote 不可用、库存校验失败或扩展在 addProduct 流程中抛错。继续点击只会制造更多噪声,先保留一次请求的完整上下文。

从浏览器请求取证

打开开发者工具 Network,清空记录后只点击一次加入购物车。记录 POST URL、HTTP 状态、form_key、product、qty、selected_configurable_option、super_attribute 和 custom options。响应若是 302,继续看跳转后的消息;若 500,按请求时间查服务端日志。

tail -n 150 var/log/exception.log
tail -n 150 var/log/system.log
grep -n -B15 -A50 'shopping cart right now' var/log/*.log

使用生产模式时前台消息通常被隐藏,异常日志中的第一条业务异常比最后一条通用 LocalizedException 更有价值。

做四个对照,快速缩小范围

  1. 同一客户加入一个普通简单商品。
  2. 访客加入问题商品。
  3. 问题商品数量改为 1。
  4. 在新无痕会话中重新选择全部必选项。

只有登录客户失败,查 customer group、shared catalog 或旧 quote;只有问题商品失败,查商品类型、选项和库存;数量 1 成功则查最小/最大销售数量与增量。

核对 Quote 是否仍然活动

SELECT entity_id, is_active, customer_id, store_id, items_count,
       reserved_order_id, updated_at
FROM quote
WHERE entity_id = 12345;

SELECT item_id, sku, qty, parent_item_id, product_type
FROM quote_item
WHERE quote_id = 12345;

不要把已下单 quote 的 is_active 直接改回 1。若 Session 指向旧 quote,应修会话恢复或自定义 checkout 逻辑,让 Magento 创建新购物车。

库存必须按当前 Stock 判断

php bin/magento inventory:reservation:list-inconsistencies -r
php bin/magento indexer:status inventory
SELECT stock_id, sales_channel_type, code
FROM inventory_stock_sales_channel;

可配置商品要检查实际选中的子 SKU;Bundle、Grouped 商品还要检查每个必选子项。后台显示 Source Quantity 大于 0,不等于目标网站的 Salable Quantity 大于 0。

定位拦截 addProduct 的自定义代码

grep -R --line-number --include='di.xml' \
  'Magento\\Checkout\\Model\\Cart\|Magento\\Quote\\Model\\Quote' app/code vendor 2>/dev/null
grep -R --line-number --include='events.xml' \
  'checkout_cart_product_add\|sales_quote_product_add' app/code vendor 2>/dev/null

在预发布环境按模块做二分禁用,重新编译后复现。若某插件捕获异常后只返回 false,应改为保留原异常上下文;若插件再次调用 cart save,检查是否造成递归或事务冲突。

修复后测试简单、可配置、Bundle 商品,各执行访客与登录客户两组;再确认迷你购物车、购物车页数量和库存预留一致。加入成功但 Quote 数据不完整,同样不能上线。