Test if checkout is complete

Is there a way with Javascript to check if a cart is complete and ready to submit (validate)?
I have a captcha widget on our form, and want to make sure that validates as well as the form is ready to be submitted.

Is there a check for that?
Thanks
Rich
Tagged:
Comments
  • fc_adamfc_adam FoxyCart Team
    @richtestani,

    You can piggy-back on the checkout-submit event, and perform your validations there. As an example:
    FC.client.on("checkout-submit", function(params) {
    if (itemNotValid) {
    return false;
    }
    });
    if itemNotValid returns true there, the return false to the event will stop the checkout submit from proceeding. You would probably also want to add in some custom error display there.
  • Hmmm - Testing out your example (with an alert to see it run something)
    I get an error:

    TypeError: undefined is not an object (evaluating 'FC.client.on')


    //implementation
    $(document).ready(function()
    {
    FC.client.on("checkout-submit", function(params) {
    if (itemNotValid) {
    alert('not valid')
    return false;
    }
    });
    });
  • fc_adamfc_adam FoxyCart Team
    @richtestani,

    Oh sorry - I had thought you were on version 2.0. As your store is on version 0.7.2, you'll need to take a different approach: https://wiki.foxycart.com/v/0.7.2/checkout#adding_custom_validation_to_your_checkout
  • Hey Adam,
    I should have noted my version.

    Thanks for the direction, and everything seems to be working right, and all validates.
    But now even though I am setting FC.checkout.config.isValid = true, it doesn't go ahead and submit.
    Do I need to force the submit?


    function recaptchaValidate()
    {
    alert('checking recaptcha')
    response = $('#g-recaptcha-response').val();
    url = 'https://shopcoobie.com/recaptcha.php?response='+response+'&callback=?';
    FC.checkout.config.isValid = false;
    $.getJSON(url, function(data)
    {
    if(data.status)
    {
    alert(data.message)
    FC.checkout.config.isValid = true;
    }
    else
    {
    //reset captcha
    alert(data.message)
    grecaptcha.reset();
    FC.checkout.config.isValid = false;
    }
    });

    }

    FC.checkout.overload("validateAndSubmit", "recaptchaValidate", null);

  • fc_adamfc_adam FoxyCart Team
    @richtestani,

    The issue is the validateAndSubmit function doesn't wait for the ajax request to come back - so when the validateAndSubmit function runs, isValid is set to false.

    You'll need to take a slightly different approach, like this:
    var originalValidateAndSubmit = FC.checkout.validateAndSubmit;

    function recaptchaValidate()
    {
    alert('checking recaptcha')
    response = $('#g-recaptcha-response').val();
    url = 'https://shopcoobie.com/recaptcha.php?response='+response+'&callback=?';
    FC.checkout.config.isValid = false;
    $.getJSON(url, function(data)
    {
    if(data.status)
    {
    alert(data.message)
    FC.checkout.config.isValid = true;
    originalValidateAndSubmit();
    }
    else
    {
    //reset captcha
    alert(data.message)
    grecaptcha.reset();
    FC.checkout.config.isValid = false;
    originalValidateAndSubmit();
    }
    });

    }

    FC.checkout.override("validateAndSubmit", "recaptchaValidate");
  • Adam,
    That was the piece. Makes sense about the form not waiting for the response.
    Thank you for stepping me through this!

    Take care,
    Rich
Sign In or Register to comment.