Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/250.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在woocommerce结帐时添加自定义税额_Php_Wordpress_Woocommerce_Checkout_Fee - Fatal编程技术网

Php 在woocommerce结帐时添加自定义税额

Php 在woocommerce结帐时添加自定义税额,php,wordpress,woocommerce,checkout,fee,Php,Wordpress,Woocommerce,Checkout,Fee,我想在woocommerce结账页面添加自定义%值,但我只想为瑞士国家显示它,并为其他国家隐藏它。现在我有了正确的代码,但问题是当用户选择瑞士时,我无法显示它。这是一个代码,请帮我看看我做错了什么 //Add tax for CH country add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' ); function woocommerce_custom_surcharge() { glob

我想在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\u客户
获取装运国家/地区()。因此,您需要在代码条件中对其进行不同的设置

另外,由于钩住的函数已经将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 );
}
//为瑞士国家增加税收
添加行动('woocommerce\u cart\u calculate\u fees','custom\u tax\u Superful\u for\u swiss',10,1);
功能自定义\瑞士税\附加费($cart){
if(is_admin()&&!defined('DOING_AJAX'))返回;
//仅适用于瑞士国家(如果我们不退出)
如果('CH'!=WC()->客户->获取装运国())退货;
$percent=8;
#$taxes=array_sum($cart->taxes);//cart_contents_total+$cart->shipping_total)*$percent/100;
//添加费用(禁用第三个参数tax:false)
$cart->add_-fee(uuu('TAX','woocmerce')。“($percent%)”,$assemption,false);
}
代码位于活动子主题(或主题)的function.php文件或任何插件文件中

经过测试,效果良好……您将获得如下结果:


但对于税收,您最好使用设置>税收(选项卡)中的默认商业税收功能,con可以在其中设置每个国家/地区的税率