Skip to content
Advertisement

Multiple IDs rule in php code WooCommerce

I’m simply trying to make this php code work with multiple IDs

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 );
}

Trying this doesn’t work –

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 );
}

What’s the correct formatting?

Advertisement

Answer

Try this.

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 );
}

Learn about in_array here : https://www.w3schools.com/php/func_array_in_array.asp

Keep adding items in $ids as required.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement