根据支付网关和国家/地区征收WooCommerce自定义费用

问题描述 投票:2回答:1

当在定义的国家/地区的结帐页面上选择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 );

谢谢

php wordpress woocommerce payment-gateway checkout
1个回答
3
投票

要在WC_Cart add_fee()方法中启用Tax,您需要将第三个参数设置为true。 您可以删除最后一个参数,因为它已经是默认值。

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
}

在这里,$cart_obj就像$woocommerce->cartWC()->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+

© www.soinside.com 2019 - 2024. All rights reserved.