Requiring custom checkout field conditionally

FuzionMonkeyFuzionMonkey Member
in Help edited December 2008
Hi,

I've read up on how to add custom fields at checkout, and how to make them required at the wiki: http://wiki.foxycart.com/docs:checkout:customfields

But how would I add a field, then make it required or optional based on whether or not a certain product category is in the cart or not.

For example, adding a Fax number field, and making it required if "category_a" is in the cart.

Basically I just need to know how I can make the javascript figure out whether or not the category is in the cart or not, I can do the rest.

Thanks
Comments
  • lukeluke FoxyCart Team
    edited December 2008
    Hey Fuzion. You'll need to add some JavaScript on your checkout page to loop through your fc_json products. Something like this:
    var doSomethingSpecial = false;
    var specialCategory = "DEFAULT";
    
    for (i in fc_json.products) {
        if (fc_json.products [ i ] .category == specialCategory) {
            doSomethingSpecial = true;
        }
    }
    
    if (doSomethingSpecial) {
     alert("hello world");
    }
    
    edited: it was eating my "[" brackets. Go ahead and remove the spaces I had to add there.
  • FuzionMonkeyFuzionMonkey Member
    edited December 2008
    Edit: nevermind, see below.
  • FuzionMonkeyFuzionMonkey Member
    edited December 2008
    Hey, thanks for the fast reply. What would be the best way to go about doing that for multiple categories? Another array?

    I came up with something like this:
    var specialCategories=new Array("category1","category2","category3","category4","category5","category6","category7","category8","category9","category10");
    var doSomethingSpecial = false;
    
    for (x in specialCategories) {
    	for (i in fc_json.products) {
    		if (fc_json.products [ i ] .category == specialCategories[x]) {
    			doSomethingSpecial = true;
    		}
    	}
    }
    if (doSomethingSpecial) {
    alert("hello world");
    }
    

    Or would I be better off with simply a bunch of if statements, one for each category.
Sign In or Register to comment.