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

Viewed 872

I try to get the active tax-classes in Woocommerce in my custom plugin. When I use the WC_Tax::get_tax_classes(), I get a empty array.

How does WC_tax class work without order details and run by itself?

1 Answers

WC_Tax::get_tax_classes() returns an empty array when there is only the default tax class set in WooCommerce > Tax (tab) settings.

It's used for example in wc_get_product_tax_class_options() function that displays the available tax classes in admin product pages settings:

/**
 * Get product tax class options.
 *
 * @since 3.0.0
 * @return array
 */
function wc_get_product_tax_class_options() {
    $tax_classes           = WC_Tax::get_tax_classes();
    $tax_class_options     = array();
    $tax_class_options[''] = __( 'Standard', 'woocommerce' );

    if ( ! empty( $tax_classes ) ) {
        foreach ( $tax_classes as $class ) {
            $tax_class_options[ sanitize_title( $class ) ] = $class;
        }
    }
    return $tax_class_options;
}

So the default tax class (active tax class) has no entry when using WC_Tax::get_tax_classes().

Then you can use wc_get_product_tax_class_options() function to get all WooCommerce tax classes or the code from this function if you want something more custom.

Related: How to get the available tax rates in WooCommerce

Related