Javascript and hmac - Prevent a product from being added to the cart

loopedlooped Member
I am attempting to use the following example from your Javascript documentation:
FC.client.on('cart-submit', function(params, next){
var product1_exists = false;
if(!jQuery.isEmptyObject(FC.json.items)) {
jQuery.each(FC.json.items, function(i, item){
if(item.category == "Product1") {
product1_exists = true;
}
});
}
if(params.data.category == "Product2" && product1_exists) {
next(false);
}
else {
next(true);
}
});
The script was not working so I checked the value by doing alert( params.data.category) The response was 'undefined' .
This is also the case for params.data.code and params.data.name
With alert(JSON.stringify(params.data)) I get the following results for my product:
"name%7C%7Ce41532960c83646aa2959462eba5dd49fe2c35fe23832a1fe5934f7e2342a95f":"Monsoon+Malabar","price%7C%7C952b144504f86f5c1cd933d748fcc69c51c27e81f49409e654f4337a4910f3dc":"4.75","code%7C%7C9b845974f7bfc3352ffd285681d138ea50936e3bbf8694074bba57646c908e2c":"1014","category%7C%7C4af751c460c02c4195d82527e856e146c0735ed3835ffdb4ec6fb90044cffa57":"DEFAULT","grind":"Whole+Bean%7Bc%2Bwholeb%7D%7C%7C6f61e1b55318690e35066cf541dff0819740f00a708133d65e807791ef31d8a6

Does this mean that using the javascript params.data. cannot return the values when the hmac product validation is being used ?
Or is there something else obvious that I am missing?
Tagged:
Comments
  • fc_adamfc_adam FoxyCart Team
    @looped,

    Ah - that would cause issues for that snippet. As the params is the data that has come from the form, that means it'll include the hash too if it's there.

    You can still make it work - but you just need to take a slightly different approach. Something like this should work - put it at the start of the function, and then check against the category variable in the rest of the snippet:

    var category = "";
    for (var name in params.data) {
    if (name.indexOf("category") == 0) {
    category = params.data[name];
    }
    }
    What it does is loops through all the variables in params.data and checks if the attribute starts with "category". If it does, it gets the value and saves it into the new variable.
  • loopedlooped Member
    Thank you very much for providing a clear explanation and solution to my issue.
    It is working well now.
Sign In or Register to comment.