在国家/地区更改Ajax更新结帐以在Woocommerce中发货 [英] On country change ajax update checkout for shipping in Woocommerce

查看:70
本文介绍了在国家/地区更改Ajax更新结帐以在Woocommerce中发货的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm looking for a way to update order review (shipping price) when client change country on checkout page. I want to use jQuery. but wc_checkout_params wc_checkout_params is deprecated.

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相关的所有解决方案,例如

All solutions related to AJAX for WC like this are useless since wc_checkout_params removed on 3.x version.

在woocommerce文档中找不到有用的东西. 堆栈中没有任何内容!

Nothing useful found in woocommerce documentations. nothing in stack overflow!

奇怪为什么为什么没有人在2年以上的时间里回答这个

wondering why no one answered questions like this for 2 years+

推荐答案

通常woocommerce自行完成,不需要任何操作...

Normally woocommerce do it itself, and nothing is needed…

但是您可以尝试以下方法,它应该可以在Woocommerce 3+版本中使用:

But you can try the following instead, that should work in Woocommerce 3+ versions:

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文件中.经过测试,可以正常工作.

Code goes in function.php file of your active child theme (or active theme). Tested and works.

这篇关于在国家/地区更改Ajax更新结帐以在Woocommerce中发货的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆