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

WooCommerce自定义优惠券排除多商品失效:仅单个商品生效问题

WooCommerce优惠券排除多商品ID失效的解决办法

问题出在set_excluded_product_ids方法的参数传递上——这个方法要求传入数组类型的参数,你直接传了两个单独的数值,导致只有第一个ID被识别,第二个被忽略。

修正后的代码如下:

$permitted_chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';  
$bemail= 'email@gmail.com'; 
$coupon = new WC_Coupon();
$coupon->set_code( substr(str_shuffle($permitted_chars), 0, 16) . '5489' );
$coupon->set_amount(5489);
// 将多个商品ID放入数组中传递
$coupon->set_excluded_product_ids( array(1393,1324) );
$coupon->set_email_restrictions($bemail);
$coupon->save();

另外需要注意,WooCommerce中类似的批量设置方法(比如set_included_product_ids、set_excluded_product_categories等),都需要传入数组作为参数,不能直接传递多个独立值。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 23:20:36