Home > Software engineering >  Multiple IDs rule in php code WooCommerce
Multiple IDs rule in php code WooCommerce

Time:10-22

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?

CodePudding user response:

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.

  • Related