php snippet for woocommerce price suffix error

44 Views Asked by At

I am new to coding so plz excuse my idiocy! I have added a php snippet to display a suffix after specific woocommerce products based on product ID. I found this snippet and modified it for my needs.

add_filter( 'woocommerce_get_price_html', 'nordra_text_after_price' );

function nordra_text_after_price($price){
    
    global $post;
        
    $product_id = $post->ID;
    
    $product_array = array( 11489,11485,11478,11473,11469,11465,11244 );//add in the product IDs to add text after price
    
    if ( in_array( $product_id, $product_array )) {
        
        $text_to_add_after_price  = ' / m2 '; //change text in bracket to your preferred text 
          
          return $price .   $text_to_add_after_price;
          
    }else{
        
        return $price; 
    }
          
}

It works for a while but then gets deactivated over and over again due to an error on this line: $product_id = $post->ID;

I can't figure out what is wrong. Can you brains plz help me.

Googled what is wrong with line $product_id = $post->ID; but can't find anything

1

There are 1 best solutions below

0
amiad On

I not understood what you try to do and what the error but maybe it help you: replace the lines:

global $post;    
$product_id = $post->ID;

to line:

$product_id = get_the_ID();