Reputation: 49
I'm trying to programmatically add a new tax class in WooCommerce from my custom plugin. I want to achieve the same functionality as adding a new tax rate class from "Additional tax classes" under "WooCommerce -> Settings -> Tax" but programmatically. A new section next to the default ones "Tax options | Standard rates | Reduced rate rates | Zero rate rates" must appear with my new class and the functionality must be the same as Standart rates, Reduced rate rates etc... A table where I can add different rates for different countries. I tried achieving this through WC_Tax class but without success:
function wm_add_additional_rate_tax_classes() {
$silver_rates = WC_Tax::create_tax_class('Silver Metal', 'silver-metal-tax-rates');
$platinum_rates = WC_Tax::create_tax_class('Platinum Metal', 'platinum-metal-tax-rates');
$palladium_rates = WC_Tax::create_tax_class('Palladium Metal', 'palladium-metal-tax-rates');
}
add_action( 'init', 'wm_add_additional_rate_tax_classes' );
It's adding new tax rate classes and they appear next to "Tax options | Standard rates | Reduced rate rates | Zero rate rates" but when I click on any of them there is no interface for adding tax rates for different countires. Any idea how to achieve this?
Upvotes: 1
Views: 596
Reputation: 3583
Your code should work as expected, maybe the bug is related to cache or something? Try this:
WC_Cache_Helper::invalidate_cache_group('taxes');
I don't know the reason of your problem. This code shows tax class with UI in dashboard, when pasted directly to functions.php:
function add_additional_rate_tax_classes()
{
$tax_class_name = 'sample-tax-class';
$tax_class_slug = 'sample-tax-class';
$tax_class = WC_Tax::create_tax_class($tax_class_name, $tax_class_slug);
}
add_action('init', 'add_additional_rate_tax_classes');
Upvotes: 1