【问题标题】:Make taxable a WooCommerce custom fee based on payment gateway and country根据支付网关和国家/地区对 WooCommerce 自定义费用征税
【发布时间】:2017-10-20 20:10:52
【问题描述】:

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

谢谢

【问题讨论】:

  • 你打算如何计算税收?你有现成的配方吗?
  • @OluwafemiSule 我尝试使用$total_taxes = array_sum($woocommerce-&gt;cart-&gt;get_taxes());,但没有帮助。我不确定我是否正确使用它。花了我一整天才让主要代码工作(我在 3 周前开始使用 PHP)
  • [$woocommerce->cart->get_cart_tax][docs.woocommerce.com/wc-apidocs/… 返回购物车中所有商品的总税款。我的问题是,您认为目前没有对购物车征税吗?
  • @ErikaJohansson ...没问题,我们都像你一样经历过这个。在您未来的问题中,不要忘记提及您正在使用 Klarna 插件并提供有关...的最大详细信息

标签: php wordpress woocommerce payment-gateway checkout


【解决方案1】:

要在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-&gt;cartWC()-&gt;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+ 版本上运行

【讨论】:

  • 此代码仍然适用于 WooCommerce 版本 5.4.x
猜你喜欢
  • 2017-05-09
  • 2017-03-02
  • 2017-08-05
  • 2014-06-06
  • 2023-03-03
  • 2019-07-29
  • 2016-10-06
  • 1970-01-01
  • 2018-11-15
相关资源
最近更新 更多