Server-side validation of cart item attributes BEFORE they're added to the cart.

temuritemuri Member
in Help edited December 2012
Hi all,

So, here's the cart setup that I have to work with.

I have BUY ITEM and MULTISELECT combo to select types of item you want to buy:
> TYPE 1
> TYPE 2
> TYPE 3
> TYPE 4
> TYPE 5

Price is calculated depending on number of product types customer is buying ($5 for 1, $9 for 2 and $20 if more than two types are selected).

I know that there's HMAC validation and it works fine when you know how much an item would cost. However, I don't see how HMAC can be used in my situation.

Thus, I have to validate calculated price server-side using AJAX call to prevent customer from meddling with the form elements using Firebug.

Question:

Can fcc.events.cart.preprocess.add be used to return true/false depending on what's coming back from AJAX call?

I'm having a problem with this:
fcc.events.cart.preprocess.add(function(e, arr) {
    $.ajax({'validate.php'}, {
        data: foxycartFormData
    }).done(function(ajaxResponse)) {
        return ajaxResponse;
    });
})

Obviously, preprocess.add never waits for AJAX call to complete. I can't do "return false" in it either.

What would be the best approach in my situation?

Thanks,
Temuri
Comments
  • fc_adamfc_adam FoxyCart Team
    @Temuri,

    You'd use 'return "pause"' and then a resume call in your ajax done function. See details of that here: http://wiki.foxycart.com/static/redirect/javascript

    Note that depending on what version your store is, you may need to upgrade to take advantage of pause and resume.
  • Thanks a lot.

    I did see the "pause"/"resume" in the docs and did try it before posting.

    However, my AJAX call had {async: false} param and that resulted in validation AJAX call being called over and over again, eventually crashing my browser.

    Here's the code that works:
    fcc.events.cart.preprocess.add_pre(function(e, arr) {
        $.ajax('validatecart.php', {
            type: 'post',
            async: true,
            data: $('form#foxycart').serialize(),
            success: function(response, textStatus, jqXHR) {
                if (parseInt(response)) { // Returns '0' or '1'.
                    fcc.events.cart.preprocess.resume();
                } else {
                    alert('Cart validation error. Your order has failed.');
                    return false;
                }
            }
        });
        return 'pause';
    });
    

    Cheers!
  • fc_adamfc_adam FoxyCart Team
    @temuri - awesome, glad you got it!
Sign In or Register to comment.