多个id规则在php代码WooCommerce



我只是想让这个php代码工作与多个id

add_filter( 'woocommerce_get_price_html', 'bbloomer_price_prefix_suffix', 100, 2 );

function bbloomer_price_prefix_suffix( $price, $product ){

// example product ID = 555

if( $product->get_id() == 555 ) {
$price = '' . $price . '';
} else {
$price = '' . $price . ' st';
}

return apply_filters( 'woocommerce_get_price', $price );
}

尝试这个不工作-

add_filter( 'woocommerce_get_price_html', 'bbloomer_price_prefix_suffix', 100, 2 );

function bbloomer_price_prefix_suffix( $price, $product ){

// example product ID = 555

if( $product->get_id() == 555, 556 ) {
$price = '' . $price . '';
} else {
$price = '' . $price . ' st';
}

return apply_filters( 'woocommerce_get_price', $price );
}

正确的格式是什么?

试试这个

add_filter( 'woocommerce_get_price_html', 'bbloomer_price_prefix_suffix', 100, 2 );

function bbloomer_price_prefix_suffix( $price, $product ){

// example product ID = 555
$ids = array(555, 556);

if( in_array($product->get_id(), $ids)) {
$price = '' . $price . '';
} else {
$price = '' . $price . ' st';
}

return apply_filters( 'woocommerce_get_price', $price );
}

在这里了解in_array: https://www.w3schools.com/php/func_array_in_array.asp

继续在$ids中添加项目。

最新更新