在 woocommerce 结账时添加自定义税值
Add custom tax value at woocommerce checkout
我想在 woocommerce 结帐页面添加自定义百分比值,但我只想为瑞士国家/地区显示它并为其他国家/地区隐藏它。现在我有了正确的代码,但问题是当用户选择瑞士时我无法显示它。这是一个代码所以请帮我看看我在这里做错了什么
//Add tax for CH country
add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' );
function woocommerce_custom_surcharge() {
global $woocommerce;
if ( WC()->customer->get_shipping_country('CH') )
return;
$percentage = 0.08;
$taxes = array_sum($woocommerce->cart->taxes);
$surcharge = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage;
// Make sure that you return false here. We can't double tax people!
$woocommerce->cart->add_fee( 'TAX', $surcharge, false, '' );
}
我确定我在这里做错了:
if ( WC()->customer->get_shipping_country('CH') )
感谢帮助
WC_Customer
get_shipping_country()
不接受任何国家代码 因为您正在获取国家/地区代码。所以你需要在你的代码条件中进行不同的设置。
此外,由于您的挂钩函数已经将 WC_Cart 对象作为参数,因此您不需要全局 $woocommerce
和 $woocommerce->cart
…
所以你重新访问的代码应该是:
// Add tax for Swiss country
add_action( 'woocommerce_cart_calculate_fees','custom_tax_surcharge_for_swiss', 10, 1 );
function custom_tax_surcharge_for_swiss( $cart ) {
if ( is_admin() && ! defined('DOING_AJAX') ) return;
// Only for Swiss country (if not we exit)
if ( 'CH' != WC()->customer->get_shipping_country() ) return;
$percent = 8;
# $taxes = array_sum( $cart->taxes ); // <=== This is not used in your function
// Calculation
$surcharge = ( $cart->cart_contents_total + $cart->shipping_total ) * $percent / 100;
// Add the fee (tax third argument disabled: false)
$cart->add_fee( __( 'TAX', 'woocommerce')." ($percent%)", $surcharge, false );
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
经过测试并且可以正常工作……您会得到类似以下内容:
But for taxes, you should better use default WooCommerce tax feature in Settings > Tax (tab), where con can set the tax rates for each country…
我想在 woocommerce 结帐页面添加自定义百分比值,但我只想为瑞士国家/地区显示它并为其他国家/地区隐藏它。现在我有了正确的代码,但问题是当用户选择瑞士时我无法显示它。这是一个代码所以请帮我看看我在这里做错了什么
//Add tax for CH country
add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' );
function woocommerce_custom_surcharge() {
global $woocommerce;
if ( WC()->customer->get_shipping_country('CH') )
return;
$percentage = 0.08;
$taxes = array_sum($woocommerce->cart->taxes);
$surcharge = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage;
// Make sure that you return false here. We can't double tax people!
$woocommerce->cart->add_fee( 'TAX', $surcharge, false, '' );
}
我确定我在这里做错了:
if ( WC()->customer->get_shipping_country('CH') )
感谢帮助
WC_Customer
get_shipping_country()
不接受任何国家代码 因为您正在获取国家/地区代码。所以你需要在你的代码条件中进行不同的设置。
此外,由于您的挂钩函数已经将 WC_Cart 对象作为参数,因此您不需要全局 $woocommerce
和 $woocommerce->cart
…
所以你重新访问的代码应该是:
// Add tax for Swiss country
add_action( 'woocommerce_cart_calculate_fees','custom_tax_surcharge_for_swiss', 10, 1 );
function custom_tax_surcharge_for_swiss( $cart ) {
if ( is_admin() && ! defined('DOING_AJAX') ) return;
// Only for Swiss country (if not we exit)
if ( 'CH' != WC()->customer->get_shipping_country() ) return;
$percent = 8;
# $taxes = array_sum( $cart->taxes ); // <=== This is not used in your function
// Calculation
$surcharge = ( $cart->cart_contents_total + $cart->shipping_total ) * $percent / 100;
// Add the fee (tax third argument disabled: false)
$cart->add_fee( __( 'TAX', 'woocommerce')." ($percent%)", $surcharge, false );
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
经过测试并且可以正常工作……您会得到类似以下内容:
But for taxes, you should better use default WooCommerce tax feature in Settings > Tax (tab), where con can set the tax rates for each country…