在国家/地区更改时 ajax 更新 Woocommerce 中的运输结帐

On country change ajax update checkout for shipping in Woocommerce

我正在寻找一种在客户在结帐页面上更改国家/地区时更新订单评论(运费)的方法。 我想使用 jQuery。但是 wc_checkout_params wc_checkout_params 已弃用。

function custom_checkbox_checker() {

    if (is_checkout()) {

        wp_enqueue_script('jquery');
        ?>

        <script type="text/javascript">

            jQuery(document).ready(function (e) {

                var $ = jQuery;

        // wc_checkout_params is required to continue, ensure the object exists

                if (typeof wc_checkout_params === 'undefined')
                    return false;

                var updateTimer,
                        dirtyInput = false,
                        xhr;



                function update_shipping(billingstate, billingcountry) {

                    if (xhr)
                        xhr.abort();

                    $('#order_methods, #order_review').block({message: null, overlayCSS: {background: '#fff url(' + wc_checkout_params.ajax_loader_url + ') no-repeat center', backgroundSize: '16px 16px', opacity: 0.6}});

                    var data = {
                        action: 'woocommerce_update_order_review',
                        security: wc_checkout_params.update_order_review_nonce,
                        billing_state: billingstate,
                        billing_country: billingcountry,
                        post_data: $('form.checkout').serialize()

                    };



                    xhr = $.ajax({
                        type: 'POST',
                        url: wc_checkout_params.ajax_url,
                        data: data,
                        success: function (response) {

                            var order_output = $(response);

                            $('#order_review').html(response['fragments']['.woocommerce-checkout-review-order-table'] + response['fragments']['.woocommerce-checkout-payment']);

                            $('body').trigger('updated_checkout');

                        },
                        error: function (code) {

                            console.log('ERROR');

                        }

                    });

                }



                jQuery('.state_select').change(function (e, params) {

                    update_shipping(jQuery(this).val(), jQuery('#billing_country').val());

                });



            });

        </script>

    <?php

    }
}

add_action('wp_footer', 'custom_checkbox_checker', 50);

有线索吗?

WC 的 AJAX 相关的所有解决方案,如 this 都是无用的,因为 wc_checkout_params 在 3.x 版本上被删除。

在 woocommerce 文档中找不到有用的内容。 堆栈溢出中没有任何内容!

想知道为什么 2 年+

都没有人回答像 this 这样的问题

Normally woocommerce do it itself, and nothing is needed…

但您可以尝试以下方法,它应该适用于 Woocommerce 3+ 版本:

add_action('wp_footer', 'billing_country_update_checkout', 50);
function billing_country_update_checkout() {
    if ( ! is_checkout() ) return;
    ?>
    <script type="text/javascript">
    jQuery(function($){
        $('select#billing_country, select#shipping_country').on( 'change', function (){
            var t = { updateTimer: !1,  dirtyInput: !1,
                reset_update_checkout_timer: function() {
                    clearTimeout(t.updateTimer)
                },
                trigger_update_checkout: function() {
                    t.reset_update_checkout_timer(), t.dirtyInput = !1,
                    $(document.body).trigger("update_checkout")
                }
            };
            $(document.body).trigger('update_checkout');
            console.log('Event: update_checkout');
        });
    });
    </script>
    <?php
}

代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。