Ex inc price

<?php


function show_excl_prices() {
  $user = wp_get_current_user();
  $roles_selected = get_field('user_roles', 'option');
  if ( $user && isset($user->roles) && $roles_selected && !empty(array_intersect($user->roles, $roles_selected)) ) {
    return true;
  }
  return false;
}

function price_html_with_tax($product, $checkSale, $prefix = false, $showOther = false) {
  $price_incl_tax = wc_get_price_including_tax($product, ['qty' => 1, 'price' => $product->get_regular_price()]);
  $sale_price_incl_tax = wc_get_price_including_tax($product, ['qty' => 1, 'price' => $product->get_sale_price()]);
  $price_excl_tax = wc_get_price_excluding_tax($product, ['qty' => 1, 'price' => $product->get_regular_price()]);
  $sale_price_excl_tax = wc_get_price_excluding_tax($product, ['qty' => 1, 'price' => $product->get_sale_price()]);
  $display_type = show_excl_prices() ? 'excl' : get_option('woocommerce_tax_display_shop'); // always excl if business client (logged in)


  $price_display_incl = 
    ($product->is_on_sale() && $checkSale ? wc_price($sale_price_incl_tax) . '<del>'.wc_price($price_incl_tax).'</del>' : wc_price($price_incl_tax))
    . ' <small class="woocommerce-price-suffix">incl. BTW</small>';
  $price_display_excl = 
    ($product->is_on_sale() && $checkSale ? wc_price($sale_price_excl_tax) . '<del>'.wc_price($price_excl_tax).'</del>' : wc_price($price_excl_tax)) 
    . ' <small class="woocommerce-price-suffix">excl. BTW</small>';

  if($display_type == 'incl') {
    $display_price = $prefix . $price_display_incl;
  } else {
    $display_price = $prefix . $price_display_excl;
  }

  if(is_singular('product') || $showOther) {
    if($display_type == 'incl') {
      $display_price .= '<br>' . '<span class="price-sub">' . $prefix . $price_display_excl . '</span>';
    } else {
      $display_price .= '<br>' . '<span class="price-sub">' . $prefix . $price_display_incl . '</span>';
    }
  }
  
  return $display_price;
}

// Replace default price_html with custom ex+inc vat
function excl_incl_price_html($price, $product) {
	if($product->is_type('simple') && !is_admin()) {
		$price = wc_get_price_to_display( $product );
		$price = price_html_with_tax($product, true);
  }
  if($product->is_type('variable') && !is_admin()) {
    $prices = $product->get_variation_prices( true );
    if ( !empty( $prices['price'] ) ) {
			$min_price     = current( $prices['price'] );
			$min_reg_price = current( $prices['regular_price'] );
			$max_reg_price = end( $prices['regular_price'] );
			$price = price_html_with_tax($product, $min_reg_price === $max_reg_price, '<small class="woocommerce-price-prefix fs-md mr-05">v.a.</small>');
    }
	}
  if($product->is_type('variation') && !is_admin()) {
    $price = wc_get_price_to_display( $product );
		$price = price_html_with_tax($product, true, false, true);
	}
	return $price;
}
add_filter('woocommerce_get_price_html', __NAMESPACE__ . '\\excl_incl_price_html', 10, 2);

// filter the WooCommerce setting to display price incl or excl, based on if user is logged in
function filter_display_type( $value ) {
  return show_excl_prices() ? 'excl' : $value;
}
add_filter('option_woocommerce_tax_display_shop', __NAMESPACE__ . '\\filter_display_type');
add_filter('option_woocommerce_tax_display_cart', __NAMESPACE__ . '\\filter_display_type');

// Remove the WC setting to set a custom display suffix, since we'll do it manually here.
add_filter('woocommerce_tax_settings', function($settings) {
  $key = array_search('woocommerce_price_display_suffix', array_column($settings, 'id'));
  if($key) {
    unset($settings[$key - 1]); // -1, because it's an array
  }
  return $settings;
});
Last Updated:
Contributors: Niek Vlam, Suite Seven