以编程方式将新税类添加到WooCommerce的“附加税类”中

huangapple go评论55阅读模式
英文:

Programmatically adding a new tax class into "Additional tax classes" in WooCommerce

问题

我正在尝试通过我的自定义插件以编程方式在WooCommerce中添加新的税类。我希望实现与从“WooCommerce ->设置->税收”下的“附加税类”中添加新的税率类相同的功能,但以编程方式。必须出现一个新的部分,位于默认部分“税选项 | 标准税率 | 减少税率 | 零税率”旁边,具有与标准税率、减少税率等相同的功能。这个新部分应该包括一个表格,我可以在其中为不同的国家添加不同的税率。我尝试通过WC_Tax类来实现这一点,但没有成功:

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');

它正在添加新的税率类,并出现在“税选项 | 标准税率 | 减少税率 | 零税率”旁边,但当我单击其中任何一个时,没有界面用于为不同国家添加税率。有没有办法实现这一点?

英文:

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?

答案1

得分: 1

你的代码应该按预期工作,也许 bug 与缓存或其他什么有关?尝试这个:

```php
WC_Cache_Helper::invalidate_cache_group('taxes');

我不知道你的问题的原因。将此代码直接粘贴到 functions.php 后,它将在仪表板中显示具有 UI 的税类:

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');

<details>
<summary>英文:</summary>

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&#39;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 = &#39;sample-tax-class&#39;;
    	$tax_class_slug = &#39;sample-tax-class&#39;;
    	$tax_class = WC_Tax::create_tax_class($tax_class_name, $tax_class_slug);
    }
    
    add_action(&#39;init&#39;, &#39;add_additional_rate_tax_classes&#39;);



</details>



huangapple
  • 本文由 发表于 2023年2月27日 19:32:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75579921.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定