在国家更改ajax更新结帐运输Woocommerce

时间:2018-05-17 11:29:15

标签: php jquery ajax woocommerce checkout

当客户在结帐页面上更改国家/地区时,我正在寻找更新订单审核(运费价格)的方法。 我想使用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);

任何线索?

由于在3.x版本上删除了wc_checkout_params,所有与像this这样的WC的AJAX相关的解决方案都无用。

在woocommerce文档中找不到任何有用的东西。 堆栈溢出没什么!

想知道为什么没有人回答像this这样的问题2年+

1 个答案:

答案 0 :(得分:2)

  

通常情况下,woocommerce自己做,不需要任何东西......

但您可以尝试以下方法,这应该适用于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文件中。经过测试和工作。