gpt4 book ai didi

Javascript 在 Woocommerce 结帐事件上执行函数

转载 作者:行者123 更新时间:2023-12-05 02:45:35 24 4
gpt4 key购买 nike

在我可以在 Woocommerce 上运行一些 AJAX 调用之前,我需要等待账单国家更改时更新结帐。

jQuery('select#billing_country').change(function(){
$( document.body ).on( 'updated_checkout', function( e, data ) {
//AJAX calls here
});
});

问题是其中一些调用也会更新结帐,这会导致无限循环。每次更改帐单国家/地区时,有没有办法只等待结帐更新一次(仅第一次)?所以像这样:

jQuery('select#billing_country').change(function(){
$when.firsttime( 'updated_checkout', function( e, data ) {
//AJAX calls here
});
});

谢谢!

最佳答案

由于 updated_checkout 事件,以下功能仅在结帐更新后执行一次。

因此:

  1. #billing_country 字段已更改
  2. 执行 AJAX update_checkout(不是“更新”)调用以更新购物车
  3. 更新结帐后(使用 updated_checkout 事件)进行自定义 AJAX 调用(仅当 checkout_is_updated 变量为 false 时) .

每次更新整个页面时,checkout_is_updated 变量都会初始化为“false”。

// executes one or more instructions only once after checkout has been updated
jQuery( function($) {
// set a control variable
var checkout_is_updated = false;
// if the "#billing_country" field changes, update the checkout
$('form.checkout').on('change', '#billing_country', function(){
$(document.body).trigger('update_checkout');
// after the checkout has been updated
$( document.body ).on( 'updated_checkout', function(){
// just once
if ( checkout_is_updated == false ) {
/*
* AJAX calls here
*/
checkout_is_updated = true;
}
});
});
});

代码已经过测试并且可以工作。

关于Javascript 在 Woocommerce 结帐事件上执行函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65901177/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com