Skip to content
Advertisement

Get and display the tax rate on Woocommerce single product pages

I’m trying to find a way how I could only display the Tax rate (16% or 7%), which a product has. Basically the idea is that there should be a static tax like.

The price includes 16% Taxes

or

The price includes 7% Taxes

So the percent rate should be dynamic based on which rate the product has.

Any idea how to solve that. All solutions that I found are displaying the complete tax amount, but I need only the rate.

Advertisement

Answer

The taxes depends on your settings which are one or multiple tax classes and for each tax classes on the customer location. Here below you will find the correct way to get and display the tax rate for a product (as it’s set on WooCommerce):

// Get the WC_Product Object
global $product;

if( ! is_a( $product, 'WC_Product' ) ) {
    $product = wc_get_product( get_the_id() );
}

// Get an instance of the WC_Tax object
$tax_obj = new WC_Tax();

// Get the tax data from customer location and product tax class
$tax_rates_data = $tax_obj->find_rates( array(
    'country'   => WC()->customer->get_shipping_country() ? WC()->customer->get_shipping_country() : WC()->customer->get_billing_country(),
    'state'     => WC()->customer->get_shipping_state() ? WC()->customer->get_shipping_state() : WC()->customer->get_billing_state(),
    'city'      => WC()->customer->get_shipping_city() ? WC()->customer->get_shipping_city() : WC()->customer->get_billing_city(),
    'postcode'  => WC()->customer->get_shipping_city() ? WC()->customer->get_shipping_city() : WC()->customer->get_billing_city(),
    'tax_class' => $product->get_tax_class()
) );

// Finally we get the tax rate (percentage number) and display it:
if( ! empty($tax_rates_data) ) {
    $tax_rate = reset($tax_rates_data)['rate'];

    // The display
    printf( '<span class="tax-rate">' . __("The price includes %s Taxes", "woocommerce") . '</span>',  $tax_rate . '%' );
}

Tested and works. You can embed that code on a function that you can reuse.


Usage example:

To display the tax rate on single products below the price (using a hooked function):

add_action( 'woocommerce_single_product_summary', 'display_tax_rate_on_single_product', 15 );
function display_tax_rate_on_single_product() {
    global $product; // The current WC_Product Object instance

    // Get an instance of the WC_Tax object
    $tax_obj = new WC_Tax();
    
    // Get the tax data from customer location and product tax class
    $tax_rates_data = $tax_obj->find_rates( array(
        'country'   => WC()->customer->get_shipping_country() ? WC()->customer->get_shipping_country() : WC()->customer->get_billing_country(),
        'state'     => WC()->customer->get_shipping_state() ? WC()->customer->get_shipping_state() : WC()->customer->get_billing_state(),
        'city'      => WC()->customer->get_shipping_city() ? WC()->customer->get_shipping_city() : WC()->customer->get_billing_city(),
        'postcode'  => WC()->customer->get_shipping_city() ? WC()->customer->get_shipping_city() : WC()->customer->get_billing_city(),
        'tax_class' => $product->get_tax_class()
    ) );
    
    // Finally we get the tax rate (percentage number) and display it:
    if( ! empty($tax_rates_data) ) {
        $tax_rate = reset($tax_rates_data)['rate'];
    
        // The display
        printf( '<span class="tax-rate">' . __("The price includes %s Taxes", "woocommerce") . '</span>',  $tax_rate . '%' );
    }
}

Code goes in functions.php file of your active child theme (or active theme). Tested and works.

Related: Get tax rate separately for every cart and order items in Woocommerce

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement