I've been tasked with modifying an existing B2C WooCommerce store to enable B2B wholesale orders. So far, a combination of User Role Editor and WooCommerce Prices by User Role has allowed me to set the wholesale prices and make the store functional.
The problem I'm facing is that now I've been told that the wholesale prices need to be displayed without VAT until checkout, but as we're based in the UK, consumers can't be shown prices in this fashion, which means I need more granular control than WooCommerce natively allows.
I noticed that there's already a solved question about setting taxes for user roles at Role based taxes in woocommerce, but what I really need is a piece of code or a plugin that will allow me to set the "Display Prices in the Shop" variable in WooCommerce's standard tax panel to Excluding Tax for one user role but default to Including Tax for everyone else.
Is that possible?
Placing the following in my functions.php works for me. I override the WooCommerce option for displaying tax in shop and cart if the role is "retailer".
add_filter('pre_option_woocommerce_tax_display_shop', 'override_tax_display_setting');
add_filter('pre_option_woocommerce_tax_display_cart', 'override_tax_display_setting');
function override_tax_display_setting() {
if ( current_user_can('retailer') ) {
return "excl";
} else {
return "incl";
}
}
I don't have enough points to comment on the answer here, but the answer of @hagbard_2605 is not working. I can't even find the filters he is mentioning. You can find the 'woocommerce_tax_setting' filter however: https://github.com/woothemes/woocommerce/blob/5ef335b169ff4e19a4c5b393963a369446922b0c/includes/admin/settings/views/settings-tax.php#L7. Maybe that will work.
The solution from @hagbard_2605 is working for me, the following custom plugin is working with WordPress 4.6
and WooCommerce 2.2.3
:
<?php
/*
Plugin Name: My WooCommerce Prices Excluding Tax for Distributors
Plugin URI: https://www.pronamic.eu/
Description: Display WooCommerce prices exlcuding tax for distributors.
Author: Pronamic
Version: 1.0.0
Author URI: https://www.pronamic.eu/
*/
/**
* Override WooCommerce tax display option for distributors.
*
* @see http://stackoverflow.com/questions/29649963/displaying-taxes-in-woocommerce-by-user-role
* @see https://github.com/woothemes/woocommerce/blob/v2.2.3/includes/admin/settings/class-wc-settings-tax.php#L147-L158
* @see https://github.com/woothemes/woocommerce/blob/v2.2.3/includes/admin/settings/class-wc-settings-tax.php#L166-L178
* @see https://github.com/WordPress/WordPress/blob/4.6.1/wp-includes/option.php#L37-L52
*/
function my_override_woocommerce_tax_display( $value ) {
if ( current_user_can( 'retailer' ) ) {
return 'excl';
}
return $value;
}
add_filter( 'pre_option_woocommerce_tax_display_shop', 'my_override_woocommerce_tax_display' );
add_filter( 'pre_option_woocommerce_tax_display_cart', 'my_override_woocommerce_tax_display' );
The WordPress pre_option_
filter is used to override the WooCommerce woocommerce_tax_display_shop
and woocommerce_tax_display_cart
options for users with the retailer
role/capability.