Your setup sounds good to me. I agree, if your customer looks at the product page for a while before adding to cart, then the token will expire. The solution would be to run the `grecaptcha.execute()` on form submit.
This should still work even if you're using ajax to submit the form, and the customer might re-submit the same form later. For example maybe something like:
Code:
[script] let handleButtonClick = event => { event.preventDefault(); grecaptcha.execute('our_site_key', {action: 'Add2Cart'}).then(function(token) { jQuery("#js-purchase-product input[name='GoogleReCaptchaResponse']").val(token); //proceed with add to cart ajax here, maybe something like: jQuery.ajax( jQuery("#js-purchase-product").attr("action"), jQuery("#js-purchase-product").serialize(), response=>{ //do something with the response }); }); }; [/script] <button type="submit" onClick="handleButtonClick">Submit</button>
Leave a comment: