根据支付网关和国家/地区对 WooCommerce 海关费用征税

Make taxable a WooCommerce custom fee based on payment gateway and country

当在指定国家/地区的结帐页面上选择 PayPal 时,此代码会增加 29 的费用。但是,它没有被征税。现在的总税额是基于商品+运费。

我将如何在不对人们双重征税的情况下向海关费用加税?

这是我的代码:

function woocommerce_custom_fee( ) {
    global $woocommerce;

    $county = array('SE');
    if ( ( is_admin() && ! defined( 'DOING_AJAX' ) ) || ! is_checkout() )
        return;

    $chosen_gateway = $woocommerce->session->chosen_payment_method ;

    $fee = 29;


    if ( $chosen_gateway == 'paypal' and ( in_array( WC()->customer->get_shipping_country(), $county ) )  ) { //test with paypal method
        $woocommerce->cart->add_fee( 'Paypal Avgift', $fee, false, '' );    
    }
}
add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_fee' );
function cart_update_script() {
    if (is_checkout()) :
    ?>
    <script>
        jQuery( function( $ ) {

            // woocommerce_params is required to continue, ensure the object exists
            if ( typeof woocommerce_params === 'undefined' ) {
                return false;
            }

            $checkout_form = $( 'form.checkout' );

            $checkout_form.on( 'change', 'input[name="payment_method"]', function() {
                    $checkout_form.trigger( 'update' );
            });


        });
    </script>
    <?php
    endif;
}
add_action( 'wp_footer', 'cart_update_script', 999 );

谢谢

To enable Tax in WC_Cart add_fee() method, you need to set the third argument to true.
You can remove the last argument as it is already the default value.

woocommerce_cart_calculate_fees 动作挂钩中,您可以直接使用包含在中的购物车对象参数 $cart_obj它。

另外 global $woocommerce 已被包含它的 WC() 对象替换(因此不再需要声明 global $woocommerce.

下面我已经清理并重新排序了你的第一个钩子函数,试试看(它将作为你的,并且费用将被征税)

add_action( 'woocommerce_cart_calculate_fees','conditional_payment_mothod_custom_fee', 10, 1 );
function conditional_payment_mothod_custom_fee( $cart_obj ) {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) || ! is_checkout() )
        return;

    $fee = 19;
    $target_county = 'SE';
    $chosen_gateway = WC()->session->chosen_payment_method;
    $shipping_country = WC()->customer->get_shipping_country();

    // Enabling fee with paypal method (and 'SE' country)
    if('paypal' == $chosen_gateway && $shipping_country == $target_county)
        $cart_obj->add_fee( __('Paypal Avgift'), $fee, true ); // Tax enabled for the fee
}

Here $cart_obj act just as $woocommerce->cart or WC()->cart

现在在你的第二个函数中,你可以使用 jQuery 快捷方式 change() 并且你可以使你的代码更现代和紧凑,这样:

add_action( 'wp_footer', 'checkout_update_script', 999 );
function checkout_update_script() {
    if ( is_checkout() ) :
    ?>
    <script>
        jQuery( function($){
            // Checking that the variable "woocommerce_params" is defined to continue               
            if ( 'undefined' === typeof woocommerce_params )
                return false;

            $('form.checkout').change('input[name="payment_method"]', function(){
                $(this).trigger( 'update' );
            });
        });
    </script>
    <?php
    endif;
}

代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。

此代码已经过测试,适用于 WooCommerce 版本 2.6.x 和 3.0+