4
votes

I am trying to display a suffix after the Price ONLY on the Product Page. I do not want to display this suffix anywhere else.

I am NOT using the Tax settings as my prices are all inclusive and I do not want to complicate the settings using the Tax options.

Using the first route from code snippet on this answer https://stackoverflow.com/a/57218980/8044005

My code is:

## Add suffix to price on Product Page
add_filter( 'woocommerce_get_price_html', 'custom_price_suffix', 100, 2 );
function custom_price_suffix( $price, $product ) {
    if(is_singular('product')) {
        $price = $price . ' <span class="make-me-small"> Inclusive of all taxes</span>';
    }
    return apply_filters( 'woocommerce_get_price', $price );
}
##-End of above code - Start new code below

However, this code snippet shows the suffix in the Related Products.

What changes should I do to prevent displaying the suffix in Related Products

1

1 Answers

5
votes

https://businessbloomer.com/woocommerce-conditional-logic-ultimate-php-guide/

Related products are generated by a “loop”. Sometimes you might want to use your PHP on the single product page only (and excluding the related ones) or viceversa.

function custom_price_suffix( $price, $product ) {
    global $woocommerce_loop;

    if( is_product() && !$woocommerce_loop['name'] == 'related' ) {
        $price = $price . ' <span class="make-me-small"> Inclusive of all taxes</span>';
    }
    //return $price;
    return apply_filters( 'woocommerce_get_price', $price );
}
add_filter( 'woocommerce_get_price_html', 'custom_price_suffix', 100, 2 );