How to enable both prices with tax and without tax
Monday, 02 September 2019
In some cases, due to commercial needs, you may need to view the prices of the products in your catalog differently depending on customer type.
In this example, we will ensure that for guests the price is displayed including taxes, while for customers logged in the price is displayed excluding taxes.
To do this, 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:
// Consider price tax included for B2C customers and tax excluded for B2B customers. add_filter( 'woocommerce_product_is_taxable', function( $taxable, $product ) { return ! ( is_user_logged_in() ); }, 10, 2 );
You can also extend this behaviour, for example, displaying prices with taxes not only if customer is logged in but also if he belong a WooCommerce B2B group:
// Consider price tax included for B2C customers and tax excluded for B2B customers. add_filter( 'woocommerce_product_is_taxable', function( $taxable, $product ) { return ! ( is_user_logged_in() && get_the_author_meta( 'wcb2b_group', get_current_user_id() ) ); }, 10, 2 );
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