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

WooCommerce场景下PHP代码匹配多个产品ID的正确写法咨询

WooCommerce多产品ID匹配判断实现方案

你的修改写法不生效是因为PHP语法不支持使用逗号直接并列多个值做等于判断,正确实现多ID匹配需要借助in_array()函数。

正确代码如下

add_filter( 'woocommerce_get_price_html', 'bbloomer_price_prefix_suffix', 100, 2 );
   
function bbloomer_price_prefix_suffix( $price, $product ){
    // 可按需修改/增减下方数组内的目标产品ID
    $target_product_ids = [555, 556];
       
    if( in_array( $product->get_id(), $target_product_ids ) ) {
        $price = '' . $price . '';
    } else {
        $price = '' . $price . ' st';
    }
   
    return apply_filters( 'woocommerce_get_price', $price );
}

代码说明

  • 数组$target_product_ids用来存放所有需要匹配的产品ID,多个ID之间用英文逗号分隔即可
  • in_array()函数会自动校验当前产品ID是否在你指定的ID列表内,返回布尔值供条件判断使用

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.29 09:36:03