Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add price suffix only on WooCommerce single product page without linked products

I am adding a price suffix on the WooCommerce single product page (and only there, not in the loop!).

I use the following:

add_filter( 'woocommerce_get_price_html', 'custom_price_suffix', 100, 2 );
function custom_price_suffix( $price, $product ) {
    if( is_product() ) {   
        $price = $price . ' <small>incl. tax</small>';
    }

    return apply_filters( 'woocommerce_get_price', $price );
}

However, this also adds the price suffix to the Up-Sells on the product page (I am not talking about the related products, but the Up-Sells).

How can I exclude the price suffix for the Up-Sells?

I tried:

if( is_product() && !$woocommerce_loop['name'] == 'up-sells' )

But the suffix is still displayed for the Up-Sells.


1 Answers

In your code $woocommerce_loop is not defined

Instead of the compare, do the reverse and only apply it to an empty value

So you get:

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

    if ( is_product() && $woocommerce_loop['name'] == '' ) {
        $price .= ' <small> incl. tax</small>';
    }
    
    //return $price;
    return apply_filters( 'woocommerce_get_price', $price );
}
add_filter( 'woocommerce_get_price_html', 'filter_woocommerce_get_price_html', 10, 2 );

OR use

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

    if ( is_product() && $woocommerce_loop['name'] !== 'related' && $woocommerce_loop['name'] !== 'up-sells' ) {
        $price .= ' <small> incl. tax</small>';
    }
    
    //return $price;
    return apply_filters( 'woocommerce_get_price', $price );
}
add_filter( 'woocommerce_get_price_html', 'filter_woocommerce_get_price_html', 10, 2 );
like image 93
7uc1f3r Avatar answered Nov 26 '25 02:11

7uc1f3r