The Foxy forums are on the move!

We're in the process of moving our forums over to a new system, and so these forums are now read-only.
If you have a question about your store in the meantime, please don't hesitate to reach out to us via email.

item count per category?

dustystringsdustystrings Member
in Help edited May 2014
I'm doing a crazy custom shipping configuration and I'm wondering if there's a way to retrieve the number of items in a particular category. For example, if a customer buys some products in category A and also some products in category B, is there a way to find out the number of items in category B only? I'd like to do a switch statement where different code is executed depending on the number of category B items that are in the cart.
  • fc_adamfc_adam FoxyCart Team

    You'd do that by creating the totals yourself - looping through the cart and calculating the total number of products in each category to a variable. Note that you'd add the product.quantity into that variable, rather than just add 1 for each different product.
  • Thanks for the quick answer! Unfortunately my knowledge of javascript is in the copy-and-paste-with-my-own-adjustments arena and not quite up to understanding what you just said...

    I don't expect you to teach me javascript, so I'll do my own research on looping and hopefully be able to come back with a more specific question and/or a wholehearted attempt at my own code!

  • Will this put me on the right track? Found here:
    var count = 0;
    	for(var key in fc_json.products) {
    		var prod = fc_json.products[key];
    		if(prod['category'] == 'hvp01') {
    			count += prod['quantity'];

    Feel free to just say no, and I'll keep looking. :-)
  • fc_adamfc_adam FoxyCart Team

    Yep - that's it! If you have multiple categories you need to track, just create a variable for each one, like:
    var hvp01_count = 0;
    var hvp02_count = 0;
Sign In or Register to comment.