php 在 WooCommerce 结账时禁用 AJAX

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27023433/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:08:31  来源:igfitidea点击:

Disable AJAX on checkout for WooCommerce

phpajaxwordpresswoocommerce

提问by chdltest

I'd like to ask how I could disable AJAX at the checkout page (where you enter shipping and billing information) and that instead of using AJAX to update the cart summary based on your location, it would update by doing a natural refresh.

我想问一下如何在结账页面(您输入运输和账单信息的地方)禁用 AJAX,而不是使用 AJAX 根据您的位置更新购物车摘要,它会通过自然刷新来更新。

Currently the cart summary would update itself without reloading the page whenever the user switches their location via shipping location. I'd like to remove that AJAX and just have the page reload with the updated information.

目前,每当用户通过送货位置切换他们的位置时,购物车摘要将自行更新而无需重新加载页面。我想删除那个 AJAX,然后用更新的信息重新加载页面。

I'm not too sure what sort of codes or direction I should be pointing at but I'm ready to provide whatever details necessary. Just let me know! Thank you!!

我不太确定我应该指出什么样的代码或方向,但我准备提供任何必要的细节。请告诉我!谢谢!!

回答by helgatheviking

All WooCommerce strings are properly localized with wp_localize_scriptso I would think you could correctly translate them by creating the appropriate .po/.mo file, but I confess to not having a lot of experience with translations. For reference: all available language packs are at Githuband you might also want to read the documentation.

所有 WooCommerce 字符串都已正确本地化,wp_localize_script因此我认为您可以通过创建适当的 .po/.mo 文件来正确翻译它们,但我承认没有很多翻译经验。供参考:所有可用的语言包都在 Github 上,您可能还想阅读文档

Anyway, the checkout scripts are all in checkout.js. Like any script you can dequeue it via wp_dequeue_script()as long as you know the handle.

无论如何,结帐脚本都在checkout.js. 像任何脚本一样wp_dequeue_script(),只要您知道句柄,就可以通过它出列。

function so_27023433_disable_checkout_script(){
    wp_dequeue_script( 'wc-checkout' );
}
add_action( 'wp_enqueue_scripts', 'so_27023433_disable_checkout_script' );

回答by Mateus Silva

I had a similar issue, and instead of remove the entire script, i went to see when the event is created, and i found this.

我遇到了类似的问题,我没有删除整个脚本,而是查看了创建事件的时间,然后发现了这一点。

$( document.body ).bind( 'update_checkout', this.update_checkout );

After reading a little, i found that I will not be able to unbind because of the namespace, so i hooked up on the onevent and since i can't prevent default i stoped the propagation of the event.

稍微阅读后,我发现由于命名空间,我将无法解除绑定,因此我连接了该on事件,并且由于我无法阻止默认,我停止了该事件的传播。

and these solved my problem.

这些解决了我的问题。

jQuery(document.body).on('update_checkout', function(e){
    //e.preventDefault();
    //e.stopPropagation();
    e.stopImmediatePropagation();
    //console.log(e);
});

回答by Eugen Mihailescu

If you follow the logic of checkout.js source code you will notice that these AJAX actions related to editing the billing|shipping addresses can be safely disabled by changing the checkout form's class name. Yes, I know, it's that simple. So instead of form.checkoutmake it form.checkout1where .checkout1is just an imaginary class name (it doesn't have to be real/existent).

如果您遵循 checkout.js 源代码的逻辑,您会注意到这些与编辑帐单|送货地址相关的 AJAX 操作可以通过更改结帐表单的类名安全地禁用。是的,我知道,就是这么简单。因此,而不是form.checkout让它form.checkout1在那里.checkout1只是一个虚构的类名(它不一定是真正的/存在)。

Here is a sample code that might help you understand what's required:

这是一个示例代码,可以帮助您了解需要什么:

var default_class = 'checkout';
var mask_class = 'checkout1';

// hiHyman the form's AJAX by changing form's default class name
$('form.'+default_class).addClass(mask_class).removeClass(default_class);

// restore the original class name whenever you want it
$('form.'+mask_class).addClass(default_class).removeClass(mask_class);

Please note that this is a hack which it's not documented. They may however change the checkout form functionality at any time so keep that in mind. I can confirm it works on WC 2.6.14 and probably in earlier versions too.

请注意,这是一个没有记录的黑客。但是,他们可能会随时更改结帐表单的功能,因此请记住这一点。我可以确认它适用于 WC 2.6.14 并且可能也适用于早期版本。

回答by Adrian

We had a similar problem: loaded checkout scripts at a quote list checkout. The scripts where loaded through another plugin again (WooCommerce Germanized).

我们遇到了类似的问题:在报价单结账处加载结账脚本。再次通过另一个插件加载的脚本(WooCommerce Germanized)。

Our solution is a more explicit one:

我们的解决方案更明确:

  • Configure a custom field on the page, you don't want the checkout script be loaded.
  • 在页面上配置自定义字段,您不希望加载结帐脚本。

And than:

然后:

add_action('wp_enqueue_scripts', 'myprefix_dequeue_woocommerce_checkout', 10000);

function myprefix_dequeue_woocommerce_checkout() {
  if (get_post_meta(get_the_ID(), 'disable_woocommerce_checkout_scripts')) {
    wp_dequeue_script('wc-checkout');
    wp_dequeue_script('wc-gzd-checkout');
    wp_dequeue_script('wc-gzdp-checkout');
  }
}

回答by akelwood

One way to not disable checkout.js.

一种不禁用 checkout.js 的方法。

First it is possible that checkout.min.js is loaded instead of checkout.js.

首先,可能加载的是 checkout.min.js 而不是 checkout.js。

Then comment thoses 2 lines :

然后评论那些 2 行:

update_checkout:function(){
    //b.reset_update_checkout_timer(),
    //b.updateTimer=setTimeout(b.update_checkout_action,"5")
},

Then your checkout page will be ajax free!

那么您的结帐页面将是 ajax 免费的!