1

I'm building reCAPATCHA into my WooCommerce checkout. It is a fairly easy process as I'm using woocommerce_checkout_process to validate the capatcha. However, I have running into one issue.

When there is a cart error, I want to be able to reset the reCAPATCHA using grecaptcha.reset(); JavaScript function. I'm unsure how to run the JavaScript command upon cart error in WooCommerce. Any tips?

Thanks!

0
4

I had exactly the same need, because of the ajax checkout validation.

Woocommerce has some custom javascript events we can hook to. In reCaptcha specific case we need to hook to checkout_error that is triggered when the ajax call fails.

So, I solved with the following code in my own javascript file, besides the PHP custom validation hooked to woocommerce_checkout_process:

jQuery(document).ready(function($) {
    $( document.body ).on( 'checkout_error', function() {
        grecaptcha.reset();
    });
});

Hope it helps.

1
  • This should be the accepted answer! – MiKE Jun 26 at 17:40
1

I have not been able to figure out how to inject JavaScript upon cart error. However, I am using a workaround -- setInterval at WooCommerce checkout that looks for an error message, and then calls grecaptcha.reset();

1
  • That is really not a good solution. The proper way is just like @Sgaddo said, to bind the "checkout_error" event. If it's not triggering for you, then you probably don't have it in the right place – MiKE Jun 26 at 17:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.