Ami*_*ino 1 php ajax jquery checkout 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);
Run Code Online (Sandbox Code Playgroud)
任何线索?
有关AJAX的WC像所有的解决方案,这是因为在3.X版本中删除wc_checkout_params没用。
在 woocommerce 文档中找不到任何有用的东西。堆栈溢出中没有任何内容!
不知道为什么没有人回答这样的问题这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
}
Run Code Online (Sandbox Code Playgroud)
代码位于活动子主题(或活动主题)的 function.php 文件中。测试和工作。