Disable AJAX on checkout for WooCommerce

2020-03-04 09:56发布

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.

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.

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!!

5条回答
够拽才男人
2楼-- · 2020-03-04 10:32

All WooCommerce strings are properly localized with wp_localize_script so 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 Github and you might also want to read the documentation.

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.

function so_27023433_disable_checkout_script(){
    wp_dequeue_script( 'wc-checkout' );
}
add_action( 'wp_enqueue_scripts', 'so_27023433_disable_checkout_script' );
查看更多
放我归山
3楼-- · 2020-03-04 10:39

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.checkout make it form.checkout1 where .checkout1 is just an imaginary class name (it doesn't have to be real/existent).

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

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

// hijack 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.

查看更多
The star\"
4楼-- · 2020-03-04 10:42

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 on event and since i can't prevent default i stoped the propagation of the event.

and these solved my problem.

jQuery(document.body).on('update_checkout', function(e){
    //e.preventDefault();
    //e.stopPropagation();
    e.stopImmediatePropagation();
    //console.log(e);
});
查看更多
劳资没心,怎么记你
5楼-- · 2020-03-04 10:42

We had a similar problem: loaded checkout scripts at a quote list checkout. The scripts where loaded through another plugin again (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');
  }
}
查看更多
beautiful°
6楼-- · 2020-03-04 10:50

One way to not disable checkout.js.

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

Then comment thoses 2 lines :

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

Then your checkout page will be ajax free!

查看更多
登录 后发表回答