Displaying price with and without TAX depending on user role

My shop base price includes TAX. What I’m trying to do is to show for some user roles price with and without TAX, and for other user roles I want to show only prices without TAX.

I tried using “price display suffix” field and hide unwanted elements with display: none, but it can’t be done because of html structure I get.

Now I’m trying to use woocommerce_get_price_html where I’m checking for user role and then turning off taxes for specified roles though I don’t know if it’s safe and it still doesn’t work how I want.

function tax_price_based_on_role( $price ) {
$current_user = wp_get_current_user();
$allowed_roles = array( ‘user_role1’, ‘user_role2’ );
if ( is_user_logged_in() && array_intersect( $current_user->roles, $allowed_roles ) ) {
WC()->customer->set_is_vat_exempt( true );
//need to also show price with TAX
return $price .= ‘ ‘ . __(‘without TAX’);
}
else{
return $price .= ‘ ‘ . __(‘with TAX’); //show only price with TAX
}
}
add_filter( ‘woocommerce_get_price_html’, ‘tax_price_based_on_role’ );

I only managed to show price with TAX to one role an price without tax to other user role. I tried adding tax to $price, but it failed because it contains price and currency string. Maybe there’s better way to do this?

Read more here:: Displaying price with and without TAX depending on user role

Leave a Reply

Your email address will not be published. Required fields are marked *