Change price suffix by customer type

Tuesday, 12 April 2022

WooCommerce By default allow to set a suffix text to append to product prices to indicate, for example, “tax included” or “tax excluded” and so on.
You can find this option, name “Price display suffix”, in:

WooCommerce -> Settings -> Tax tab

As described in option help:

Define text to show after your product prices. This could be, for example, “inc. Vat” to explain your pricing. You can also have prices substituted here using one of the following: {price_including_tax}, {price_excluding_tax}.

But what happens if the WooCommerce B2B “Tax split” option is activated?

Unfortunately, this is a fixed text, there are no conditions to manage or change it by settings and being a custom text that each user can set as they want, there is no automatic way to understand how to behave.

Fortunately, WooCommerce by default has a dedicated hook to manage this string, that you can use to customize behaviour.
If you want to change suffix price text according to customer type (B2B or B2C), edit functions.php file that you find in your site’s FTP space in:

 wp-content/themes/{YOUR_ACTIVE_THEME}/functions.php

and insert this code:

add_filter( 'woocommerce_get_price_suffix', 'wcb2b_price_suffix', 10, 4 );
function wcb2b_price_suffix( $html, $product, $price, $qty ) {
    if ( get_option( 'wcb2b_split_taxes' ) === 'yes' ) {
        if ( is_user_logged_in() ) {
            $group_id = get_the_author_meta( 'wcb2b_group', get_current_user_id() );
            if ( $group_id && $group_id !== get_option( 'wcb2b_guest_group', 0 ) ) {
                return __( 'Tax excluded', 'woocommerce-b2b' );
            }
        }
    }
    return $html;
}

So, if WooCommerce B2B “Tax split” option is enabled and user is logged in and user group is not default group GUEST, text suffix returned will be “Tax excluded”, otherwise text suffix returned will be the default one set in WooCommerce settings.

WARNING! Before making any changes, make sure you have made a complete backup of site files and database.
It is advisable not to edit the theme files directly, but to use a child theme.
For more information on child themes, please read: WordPress Official guide on Child Themes