在WooCommerce中为特定国家/地区停用增值税

时间:2020-06-03 12:49:23

标签: php wordpress woocommerce

无论增值税百分比是多少,原始代码都将所有价格都设置为相同。因此,如果一件商品的价格为100美元(含25%的增值税),那么它的费用为100美元(含80%的增值税,甚至是0%的增值税)。

这很好,但是,我想取消某些国家的增值税。

来自this answer thread的原始代码:

add_filter( 'woocommerce_adjust_non_base_location_prices', '__return_false' );

我的代码不起作用:

add_filter( 'woocommerce_adjust_non_base_location_prices', 'custom_eu_vat_number_country_codes' );
function custom_eu_vat_number_country_codes( $vat_countries ) {

// Which countries should it be applide to?
    $countries = array( 'AX', 'AT', 'BE', 'BA', 'HR', 'CZ', 'DK', 'FI', 'GR', 'HU', 'IS', 'IE', 'IT', 'LU', 'NL', 'PO', 'PT', 'RO', 'SK', 'SI', 'ES', 'SE', 'CH', 'GB');

    // Avoiding errors on admin and on other pages
    if( is_admin() || WC()->cart->is_empty() )
        return $countries;

// Remove field $countries
if (($key = array_search($countries, $vat_countries)) !== false) {
    return false;
}
return $vat_countries;
}

我在做什么错了?

1 个答案:

答案 0 :(得分:1)

main函数参数与国家/地区无关,它是一个布尔值(默认为true),请参见on wc_get_price_excluding_tax() function code.

您需要从WC_Customer对象(或运送国家)获取客户开票国家/地区。

因此您的代码应为:

add_filter( 'woocommerce_adjust_non_base_location_prices', 'custom_eu_vat_number_country_codes' );
function custom_eu_vat_number_country_codes( $boolean ) {
    // Avoiding errors on admin and on other pages
    if( is_admin() || WC()->cart->is_empty() )
        return $boolean;

    // Defined array of countries where the boolean value should be "false"
    $countries = array( 'AX', 'AT', 'BE', 'BA', 'HR', 'CZ', 'DK', 'FI', 'GR', 'HU', 'IS', 'IE', 'IT', 'LU', 'NL', 'PO', 'PT', 'RO', 'SK', 'SI', 'ES', 'SE', 'CH', 'GB');

    // Remove field $countries
    if ( in_array( WC()->customer->get_billing_country(), $countries ) ) {
        $boolean = false;
    }
    return $boolean;
}

代码进入活动子主题(或活动主题)的functions.php文件中。它应该工作(未经测试)。

相关问题