I have this code which shows prices with Taxes Included for the ‘customer’ user role and prices with Taxes Excluded for ‘customer_2’ user role.
It works great for “Simple Products”. But it does not work at all for “Variable Products”. What is the change I need to do to this code to make sure it will works for both types of products (simple and variable)
function cowo_edit_price_display($price) { global $product; $user = wp_get_current_user(); $price_excl = $product->get_price_excluding_tax(); // price without VAT $price_incl = $product->get_price_including_tax(); // price included VAT if ( in_array( 'customer', (array) $user->roles ) ) { //The user has the "customer" role $price = wc_price($price_excl); return $price; } if ( in_array( 'customer_2', (array) $user->roles ) ) { //The user has the "customer_2" role $price = wc_price($price_excl); return $price; } return $price; } add_filter('woocommerce_get_price_html', 'cowo_edit_price_display', 30, 2);
Advertisement
Answer
Your code is a bit outdated since WooCommerce 3… The code handle handle simple products, variable products and products variations prices. There is 2 cases:
1). Displayed prices excluding taxes for specific user roles:
Your Woocommerce setting for “Display prices in the shop” is “Including taxes”.
add_filter('woocommerce_get_price_html', 'display_prices_excl_taxes_user_role', 100, 2 ); function display_prices_excl_taxes_user_role( $price_html, $product ) { // Here define your user roles in the array $targeted_user_roles = array('customer_2', 'administrator'); $user = wp_get_current_user(); // The WP_User Object // For specific user roles (price excluding taxes) if ( array_intersect( $user->roles, $targeted_user_roles ) ) { // Simple products and products variation if( $product->is_type('simple') || $product->is_type('variation') ) { if( $product->is_on_sale() ) { $regular_price = wc_get_price_excluding_tax( $product, array( 'price' => $product->get_regular_price() ) ); $price_html = wc_format_sale_price( $regular_price, wc_get_price_excluding_tax( $product ) ); } else { $price_html = wc_price( wc_get_price_excluding_tax( $product ) ); } $price_html .= $product->get_price_suffix(); } // variable pproducts elseif( $product->is_type('variable') ) { $prices = $product->get_variation_prices( true ); if ( ! empty( $prices['price'] ) ) { $act_keys = array_keys($prices['price']); $reg_keys = array_keys($prices['regular_price']); $min_price = wc_get_price_excluding_tax( wc_get_product(current($act_keys))); $max_price = wc_get_price_excluding_tax( wc_get_product(end($act_keys))); $min_reg_price = wc_get_price_excluding_tax( wc_get_product(current($reg_keys))); $max_reg_price = wc_get_price_excluding_tax( wc_get_product(end($reg_keys))); if ( $min_price !== $max_price ) { $price_html = wc_format_price_range( $min_price, $max_price ); } elseif ( $product->is_on_sale() && $min_reg_price === $max_reg_price ) { $price_html = wc_format_sale_price( wc_price( $max_reg_price ), wc_price( $min_price ) ); } else { $price_html = wc_price( $min_price ); } $price_html .= $product->get_price_suffix(); } } } return $price_html; }
2). Displayed prices including taxes for specific user roles:
Your Woocommerce setting for “Display prices in the shop” is “Excluding taxes”.
add_filter('woocommerce_get_price_html', 'display_prices_incl_taxes_user_role', 100, 2 ); function display_prices_incl_taxes_user_role( $price_html, $product ) { // Here define your user roles in the array $targeted_user_roles = array('customer_2', 'administrator'); $user = wp_get_current_user(); // The WP_User Object // For specific user roles (price excluding taxes) if ( array_intersect( $user->roles, $targeted_user_roles ) ) { // Simple products and products variation if( $product->is_type('simple') || $product->is_type('variation') ) { if( $product->is_on_sale() ) { $regular_price = wc_get_price_including_tax( $product, array( 'price' => $product->get_regular_price() ) ); $price_html = wc_format_sale_price( $regular_price, wc_get_price_including_tax( $product ) ); } else { $price_html = wc_price( wc_get_price_including_tax( $product ) ); } $price_html .= $product->get_price_suffix(); } // variable pproducts elseif( $product->is_type('variable') ) { $prices = $product->get_variation_prices( true ); if ( ! empty( $prices['price'] ) ) { $act_keys = array_keys($prices['price']); $reg_keys = array_keys($prices['regular_price']); $min_price = wc_get_price_including_tax( wc_get_product(current($act_keys))); $max_price = wc_get_price_including_tax( wc_get_product(end($act_keys))); $min_reg_price = wc_get_price_including_tax( wc_get_product(current($reg_keys))); $max_reg_price = wc_get_price_including_tax( wc_get_product(end($reg_keys))); if ( $min_price !== $max_price ) { $price_html = wc_format_price_range( $min_price, $max_price ); } elseif ( $product->is_on_sale() && $min_reg_price === $max_reg_price ) { $price_html = wc_format_sale_price( wc_price( $max_reg_price ), wc_price( $min_price ) ); } else { $price_html = wc_price( $min_price ); } $price_html .= $product->get_price_suffix(); } } } return $price_html; }
Code goes in functions.php file of your active child theme (or active theme). Tested and works.