Displaying WooCommerce Prices: Breaking Down Tax vs. Total
By default, WooCommerce handles tax display logic based on global settings or specific product flags, often collapsing the final price into a single number for the consumer. When you configure a store to show prices excluding tax, the frontend typically renders only the base price. However, there are scenariosâcommonly found in B2B catalogs or regions with complex tax structuresâwhere displaying the breakdown is mandatory: the unit price without tax, the calculated tax amount, and the final total including tax.
Since no standard plugin natively offers a three-column display for product prices on the main shop loop while simultaneously maintaining the "exclude tax" store setting, you will likely need to intervene directly in the theme's template hierarchy or use custom code. The most robust solution involves hooking into WooCommerce's price rendering filters and leveraging its built-in price formatting functions.
Understanding the Price Structure
Before modifying templates, it is crucial to understand how WooCommerce calculates these values internally. When a product is set to "Exclude tax" in the product data panel, the WC_Product::get_price() method returns the base cost. However, this method does not automatically calculate the tax amount unless you explicitly request it using get_tax_display_value() or by calculating it manually based on the customer's cart location and shipping settings.
The core of the solution lies in accessing the WC_Product object within a template file (such as content-product.php or single-product.php) and formatting three distinct values:
- Base Price: The price without tax.
- Tax Amount: The calculated tax based on the current user's location and the product's tax class.
- Final Price: The sum of the base price and the calculated tax.
Implementing Custom Price Breakdown
To achieve this display, you should avoid hardcoding prices in your theme files. Instead, override the relevant template part or create a custom short code that renders the specific markup you need. Here is a practical approach using PHP to format these values dynamically:
// Add this to your theme's functions.php or a custom plugin
add_action( 'woocommerce_before_shop_loop_item_title', 'display_breakdown_prices', 20 );
function display_breakdown_prices( $product ) {
// Check if the product actually has tax enabled in settings
if ( ! $product->is_taxable() ) {
return;
}
// Get the base price without tax
$price_without_tax = wc_price( $product->get_price() );
// Calculate tax amount based on customer location
// Note: This requires checking if tax is calculated for this specific context
$tax_amount = '';
$displayed_tax = false;
// Check if display includes tax in the current view logic
if ( get_option( 'woocommerce_prices_include_tax' ) === 'no' ) {
// If prices exclude tax, we need to calculate the tax amount manually for display
// We use WC_Tax to simulate the calculation based on customer location
$customer_location = new WC_Customer( WC()->customer );
$tax_rate = WC_Tax::get_rates();
// Simplified logic: In a real scenario, you'd iterate through rates or use get_tax_total()
// if available in this context. For precise rendering, we often rely on the product's tax object.
// A more direct way to get the formatted tax string for a specific price:
$taxed_price = wc_price( $product->get_price() + WC_Tax::calc_tax( $product->get_price(), $customer_location ) );
// However, getting the exact formatted tax string is slightly nuanced.
// We can use get_formatted_tax_amount if available in newer versions or calculate via get_product_type_tax_rate().
// For a reliable implementation without complex location checks in the loop:
// We often rely on the 'price_html' filter to inject our HTML.
}
}
// Better approach: Filter the price output directly
add_filter( 'woocommerce_get_price_html', 'custom_breakdown_price_html', 10, 2 );
function custom_breakdown_price_html( $price_html, $product ) {
// Only apply if we want to override standard display
if ( get_option( 'woocommerce_prices_include_tax' ) === 'no' && $product->is_taxable() ) {
// Get base price
$base = wc_format_price( $product->get_price(), $product->get_price_includes_tax() );
// Calculate tax amount.
// Note: Getting the exact formatted tax string dynamically in a loop requires
// knowing the customer's location or using the cart context.
// If no customer is logged in, tax might default to 0% unless set globally.
$tax_amount = '';
$final_price = $product->get_price();
if ( WC()->customer && WC_Tax::get_rates() ) {
// Calculate tax based on current location
$tax_total = WC_Tax::calc_tax( $product->get_price(), array_keys( WC_Tax::get_rates() ), 'inclusive' );
// This calculation logic is complex without the specific customer context.
// A simpler alternative for display-only purposes:
// If you need a simplified view where tax is just added:
$tax_rate = 0;
if ( WC()->customer && $product->is_taxable() ) {
$tax_rate = WC_Tax::get_rates(); // Returns array of rates for location
// Logic to extract specific rate and apply would go here.
}
}
// Construct the HTML manually
$display_html = '<span class="product-base-price">' . $base . '</span>';
// If tax is applicable and we have a customer or default rate:
if ( $product->get_tax_percent() > 0 ) {
$tax_value = wc_format_price( $product->get_price() * ( $product->get_tax_percent() / 100 ), false );
$display_html .= '<span class="product-tax-amount">+ ' . $tax_value . '</span>';
}
$final_display = wc_format_price( $product->get_price() + ( $product->get_tax_percent() ? $product->get_price() * ($product->get_tax_percent()/100) : 0 ), true );
$display_html .= '<span class="product-final-price">= ' . $final_display . '</span>';
return $display_html;
}
return $price_html;
}
Handling Location-Based Tax Calculations
A critical pitfall in this implementation is calculating the tax amount dynamically. WooCommerce's calc_tax function requires a specific customer location (billing/shipping) to determine which tax rate applies. In a product loop where no user is logged in, or when shipping settings are ambiguous, the calculated tax might default to zero even if the store setting says "exclude tax."
To handle this gracefully, your code must account for WC_Tax::get_rates() returning an empty array if a customer location isn't set. In such cases, you may need to fall back to a global default tax rate or simply display the base price plus a placeholder indicating that tax will be added at checkout. This prevents displaying incorrect totals that could lead to cart discrepancies later.
Trade-offs and Template Overrides
While custom code offers granular control, it comes with maintenance costs. Every time WooCommerce updates its core pricing logic, your filter hook might need adjustment. Alternatively, you can create a child theme and copy content-product.php or the specific price display block into it. This approach isolates your changes but requires overriding multiple template files if you want this behavior across different product types like variations or grouped products.
If you prefer not to touch code, check for third-party extensions that specifically target "Price Display" in WooCommerce settings, though as noted in your requirements, finding a plugin that strictly separates these three values while keeping the store set to "exclude tax" globally is rare. Custom development remains the most precise path to ensuring the checkout totals match your catalog display exactly.
