phpwordpresswoocommerce

How to get the available tax rates in WooCommerce


Based on Using WC_Tax::get_tax_classes() to get all WooCommerce tax-classes answer to my previous question, I am trying to get the available tax rates from the defined tax-classes in WooCommerce.

I tried the following code:

$tax_classes = wc_get_product_tax_class_options();

foreach ( $tax_classes as $tax_class ) {
    $taxes = WC_Tax::get_rates( $tax_class );
    var_dump($taxes);
}

The var_dump exports are empty, but it looks like it interacts with the available classes.

/home/public_html/...etc
array (size=0)
  empty
/home/public_html/...etc
array (size=0)
  empty
/home/public_html/...etc
array (size=0)
  empty
/home/public_html/...etc
array (size=0)
  empty
/home/public_html/...etc
array (size=0)
  empty

How do i get a array (in the backend admin-panel) with the available tax details: name, tax_rate etc?


Solution

  • Each tax class rates are defined in backend by location settingsā€¦ so they depend on customer location. The tax rates can be applied differently to products and shipping.

    For each tax class, you need to set tax rates by country or by shipping zone, depending on allowed purchase locations or zones (see the example below).

    enter image description here

    Use the following to display the available tax rates for the customer (based on its location):

    $location = 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_postcode() ? WC()->customer->get_shipping_postcode() : WC()->customer->get_billing_postcode(),
    );
    
    $output = array(); // Initialiizing (for display)
    
    // Loop through tax classes
    foreach ( wc_get_product_tax_class_options() as $tax_class => $tax_class_label ) {
    
        // Get the tax data from customer location and product tax class
        $tax_rates = WC_Tax::find_rates( array_merge(  $location, array( 'tax_class' => $tax_class ) ) );
    
        // Finally we get the tax rate (percentage number) and display it:
        if( ! empty($tax_rates) ) {
            $rate_id      = array_keys($tax_rates);
            $rate_data    = reset($tax_rates);
    
            $rate_id      = reset($rate_id);        // Get the tax rate Id
            $rate         = $rate_data['rate'];     // Get the tax rate
            $rate_label   = $rate_data['label'];    // Get the tax label
            $is_compound  = $rate_data['compound']; // Is tax rate compound
            $for_shipping = $rate_data['shipping']; // Is tax rate used for shipping
    
            // set for display
            $output[] = '<li class="tax-rate '.$tax_class.'">'.$tax_class_label.': <strong>'.$rate.'</strong> <small>(label: '.$rate_label.' | id: ' . $rate_id . ')</small></li>';
        }
    }
    
    // Display
    if ( ! empty($output) ) {
        echo '<div><h4>' . __("Available Tax rates") . '</h4><ul>' . implode('', $output) . '</ul></div>';
    }
    

    You will get something like:

    enter image description here

    Related: Using WC_Tax::get_tax_classes() to get all WooCommerce tax-classes


    Usage specifying a location

    As it seems that you want to use it in backend as are asking in your comment, you need to specify a location to be able to get tax rates for that specific location.

    So for example to get the tax rates set for France country ("FR" country code) you will replace the $location array by:

    $location = array( 'country' => 'FR', 'state' => '', 'city' => '', 'postcode' => '' );
    

    If there is no country specified, you will use an array with empty values like:

    $location = array( 'country' => '', 'state' => '', 'city' => '', 'postcode' => '' );