1

I am using the code from here: Conditionally alter specific product price in Woocommerce to increase the price of a specific product by $10 except on product pages from a certain category and if the cart contains anything from that category.

However now I have more than one product that I need to increase the price of. I tried altering line 7:

if( $product->get_id() != 87 ) return $price_html;

to something like

if( $product->get_id() != 87 || $product->get_id() != 2799 ) return $price_html;

to target product 87 or 2799 but it just breaks the code and even product 87 no longer displays as $10 more. I have tried variations on || and or but nothing I do works.

Help much appreciated :)

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Rose Thorn
  • 179
  • 3
  • 16
  • Above code snippet looks good. Maybe something else is affecting it. Can you share the entire code block so we can debug it? – Dhaval Shah Sep 13 '18 at 05:56

2 Answers2

2

For multiple product IDs, instead of using something like:

add_filter( 'some_hook', 'some_function' );
function some_function( $price_html, $product ){
    if( $product->get_id() != 87 || $product->get_id() != 2799 ) return $price_html;

    // The function code 

    return $price_html; // at the end
}

You will use something like:

add_filter( 'some_hook', 'some_function' );
function some_function( $price_html, $product ){
    if( in_array( $product->get_id(), array( 87, 2799 ) ) ){

        // The function code 

    }

    return $price_html; // at the end
}

And it will work for an array of multiple products

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Thanks! Changing line 7 to `if( ! in_array( $product->get_id(), array( 87, 2799 ) ) ) return $price_html; // Exit` works great! – Rose Thorn Sep 13 '18 at 23:00
0

Your if condition doesn't makes sense because it will always return true. Try to replace or with and:

if( $product->get_id() != 87 && $product->get_id() != 2799 ) return $price_html;
Ivnhal
  • 1,099
  • 1
  • 12
  • 20
  • Thanks @IvnH My coding fortes lie outside PHP so it's been quite the learning curve for me to understand logic. I get the differences now :) – Rose Thorn Sep 13 '18 at 23:02