Flat rates by COUNTY (not country) and product category

intandemintandem Member
in Help edited October 2012
Hi! I hope somebody can help me with these 2 queries:

1. County instead of country and flat fee per category, not item

My client has 2 product categories. Both have one flat rate shipping charge for deliveries to a range of counties in Ireland and another flat rate shipping charge to all other counties (incl. Northern Ireland). So that's 2 x flat rate to zone A and 2 x flat rate to zone B. They do not ship beyond the island of Ireland.

I have found the following sample code for a "by country" scenario: http://wiki.foxycart.com/snippets/shipping/flat_rates_by_country_and_product_category

I have changed the code to use counties instead of countries. I hope it's all done correct. However, I don't want to charge per item (as in that sample code) but just per category.

Can someone please confirm for me how to amend the code in the above link to NOT charge the flat rate shipping fees per product but leave it at the standard per category, no matter how many products are in it? Do I need to change this part:
// Check each category and calculate shipping cost
jQuery.each(fc_json.products, function(index, options) {
 
// Calculate the total shipping cost for this category based on the quantity
var shipping = options.quantity * shippingCost[options.category];
 
// Add the total shipping cost of this category to the grand total shipping cost
totalShippingCost += shipping;
 
});

to this:
// Check each category and calculate shipping cost
jQuery.each(fc_json.products, function(index, options) {
 
// Calculate the total shipping cost for this category 
var shipping = shippingCost[options.category];
 
// Add the total shipping cost of this category to the grand total shipping cost
totalShippingCost += shipping;
 
});




2. Separate County lists by Country

As some of the counties are based in Northern Ireland, it would be preferable to allow clients to first select a country (Ireland or Northern Ireland) and then be presented by the relevant drop-down list of counties (as opposed to a full list of all counties for the entire island in one go). Please note, Zone B above is made up of all of the counties in Northern Ireland as well as a range of counties in the Republic. So the flat rate zones have nothing to do with countries.

This second query is more of a desirable as opposed to a mandatory feature for this shop.


All help appreciated. Thanks!
Comments
  • And another, probably really stupid, question ... where do I insert the above script in the first place? I'll probably hav e a "doh!" moment when I see the answer ;-)
  • lukeluke FoxyCart Team
    Hey intandem. Since you want to change the functionality of the checkout page, you'll want to put that script in your checkout template. As long as it's in a document ready test, you can put it anywhere you want.

    @fc_adam can help out with more details as well.
  • fc_adamfc_adam FoxyCart Team
    @intandem,

    I'd actually suggest switching over to this approach: http://wiki.foxycart.com/snippets/shipping/tiered_flat_rate_shipping

    And use this for your custom code: http://pastie.org/private/q2e1hedxvy8qttmz3rwbsw

    Note that you'd need to update the validCounties array at the top, but while we work on adding the valid counties to Ireland for the checkout selection, you'd need to make an assumption on how it actually is typed into the field.

    You'd also need to update the value that is assigned per category for the relevant counties in Ireland, and subsequently for the rest. I've added a default case there as well, which could be a sort of safety net, that if for any reason the others aren't matched, that shipping is still set.
    2. Separate County lists by Country

    Based on the ISO 3166 listing, Northern Ireland is only listed as a part of the United Kingdom, and not it's own listing. So with that in mind, your best bet would be to restrict to just the United Kingdom and Ireland in the country selector. You could add some additional custom logic that if the UK is selected that the county field is pre-filled with "Northern Ireland" to restrict it to just that location - would that work?
  • Ok, great! I will give that a go. For the moment, I'll use the county ISO codes as listed in this link: http://www.statoids.com/uie.html but if you use something else later, I can change it.

    The suggestion regarding Northern Ireland could work. Will take a closer look after I've done the other bit.

    Thanks for your help!

    Carolin
  • intandemintandem Member
    edited October 2012
    I have written up the custom shipping logic as per below for the moment. The validCounties list includes all counties except "D" for Dublin as there is a different shipping flat rate charge here.

    The charges are:

    "DEFAULT" category: 5.00 in Dublin, 8.50 everywhere else.
    "other" category: 10.00 Dublin, 20.00 everywhere else.

    Is this set up correctly (pending your county code implementation)?
    /* BEGIN CUSTOM SHIPPING LOGIC */
    
                   FC.locations.limitCountriesTo("IE","UK");
    	       FC.locations.limitStatesTo("UK", "NI");
      
    		var validCounties = ["CW", "CN", "CE", "CO", "DL", "G", "KY", "KE", "KK", "LS", "LM", "LK", "LD", "LH", "MO", "MH", "MN", "OY", "RN", "SO", "TA", "WD", "WH", "WX", "WW"];
    		var country_code = (jQuery("#use_different_addresses").is(":checked") ? $("#shipping_country").val() : $("#customer_country").val());
    		var state_name = (jQuery("#use_different_addresses").is(":checked") ? $("#shipping_state_name").val() : $("#customer_state_name").val());
    		
    		var categories = [];
    		
    		for (var p in fc_json.products) {
    		  var pCat = fc_json.products[p].category;
    		  if (categories[pCat] == null) {
    			categories[pCat] = 0;
    		  }
    		  categories[pCat] += fc_json.products[p].quantity;
    		}
    		
    		for (var c in categories) {
    		  switch(c) {
    			case "other":
    			  if (country_code == "IE" && jQuery.inArray(state_name, validCounties) > -1) {
    				shippingCost += 20;
    			  } else {
    				shippingCost += 10;
    			  }
    			  break;
    			default:
    			  if (country_code == "IE" && jQuery.inArray(state_name, validCounties) > -1) {
    				shippingCost += 8.5;
    			  } else {
    				shippingCost += 5;
    			  }
    		  }
    		}
    		 
      /* END CUSTOM SHIPPING LOGIC */
    


    However, if I now want to add the UK/Northern Ireland bit and let's say you create "NI" as a pseudo-county code for Northern Ireland (as I used in the limitStates code above), do I have to change the validCounties entry to something like this:

    ("IE", ["CW", "CN", "CE", "CO", "DL", "D", "G", "KY", "KE", "KK", "LS", "LM", "LK", "LD", "LH", "MO", "MH", "MN", "OY", "RN", "SO", "TA", "WD", "WH", "WX", "WW"]),("UK", ["NI"]);

    Not sure of the syntax.

    Thanks for your help. Feels like I'm moving forward with this.
  • fc_adamfc_adam FoxyCart Team
    @intandem,

    Just to confirm, your shipping cost is one cost to Dublin, and a different cost to everywhere else on the island? Is that right?
  • Yes, that's right. One for Dublin (city and county) and one for the rest of Ireland and Northern Ireland.

    There are two categories, each with one price for Dublin and one for the rest.
  • intandemintandem Member
    edited October 2012
    Just thinking ... Perhaps I should reverse my thinking and simply have "D" for Dublin as a single valid county. I think I was making the code over-complicated?
  • fc_adamfc_adam FoxyCart Team
    Yeah, use this: http://pastie.org/private/ywu5cojelpye6dxxhtks1q

    If you also throw in the code I posted in your other thread, that should work with the county dropdown as well.
  • intandemintandem Member
    edited November 2012
    Hi Adam

    I hope you can help me one last time. At the moment, we are using the code that you so kindly worked out for me as follows:
    /* BEGIN CUSTOM SHIPPING LOGIC */
      
      			/* Set flat rate shipping by category */
      
    			var country_code = (jQuery("#use_different_addresses").is(":checked") ? $("#shipping_country").val() : $("#customer_country").val());
    			var state_code = (jQuery("#use_different_addresses").is(":checked") ? $("#shipping_state").val() : $("#customer_state").val());
    			
    			var categories = [];
    			
    			for (var p in fc_json.products) {
    			  var pCat = fc_json.products[p].category;
    			  if (categories[pCat] == null) {
    				categories[pCat] = 0;
    			  }
    			  categories[pCat] += fc_json.products[p].quantity;
    			}
    			
    			for (var c in categories) {
    			  switch(c) {
    				case "BOUQUET":
    				  if (country_code == "IE" && state_code == "D") {
    					if (shippingCost < 10) {
    					  shippingCost = 10;
    					}
    				  } else {
    					if (shippingCost < 20) {
    					  shippingCost = 20;
    					}
    				  }
    				  break;
    				default:
    				  if (country_code == "IE" && state_code == "D") {
    					if (shippingCost < 5) {
    					  shippingCost = 5;
    					}
    				  } else {
    					if (shippingCost < 8.5) {
    					  shippingCost = 8.5;
    					}
    				  }
    			  }
    			}
    						
    
    			
    		 
      /* END CUSTOM SHIPPING LOGIC */
    

    This covers 2 product categories: bouquet and default. Both have two different flat rate shipping costs, depending on shipping address.

    Would you please be able to tell me how I can add a third category (e.g. called "voucher") which would have a single flat rate no matter where it's shipped to, e.g. 1.50 (it's for a gift certificate which would be sent by standard mail)? We're going live tomorrow (fingers crossed) and the client has just added this product which makes no sense to ship via the courier rates of all the "real" products. Thanks so much!
  • fc_adamfc_adam FoxyCart Team
    @intandem,

    This should do it for you, if your category code is VOUCHER.
    for (var c in categories) {
      switch(c) {
    	case "BOUQUET":
    	  if (country_code == "IE" && state_code == "D") {
    		if (shippingCost < 10) {
    		  shippingCost = 10;
    		}
    	  } else {
    		if (shippingCost < 20) {
    		  shippingCost = 20;
    		}
    	  }
    	  break;
    	case "VOUCHER":
    		shippingCost = 1.5;
    		break;
    	default:
    	  if (country_code == "IE" && state_code == "D") {
    		if (shippingCost < 5) {
    		  shippingCost = 5;
    		}
    	  } else {
    		if (shippingCost < 8.5) {
    		  shippingCost = 8.5;
    		}
    	  }
      }
    }
    

    The script has changed a little from what it was previously - and isn't an accumulative shipping total anymore - so if products from multiple categories are ordered, it won't add shipping for both categories. Is that an issue?
  • Thanks so much for that! Yes, that's correct. Where there are products from multiple categories in an order we want only the highest flat-rate shipping charge calculated once . It would actually be really great if this was an option that could be set in the Foxycart dashboard with a single click as this would be a very common scenario for private courier companies here.
  • fc_adamfc_adam FoxyCart Team
    @intandem,

    We're currently rebuilding our shipping functionality which will allow for custom shipping settings in the admin without having to resort to javascript. No word on release timeline yet, it's a big project, but we know it's a huge need for users too.
  • Oh yes, Supership!! Waiting in anticipation :) It will be great, I'm sure and will certainly make the most painful part of developing an ecommerce site a lot better!
  • intandemintandem Member
    edited November 2012
    By the way, if the client were to give the VOUCHER category a 0.00 shipping fee, do I still need to add it via this javascript (set to shippingCost = 0;) or is it sufficient to set up the category with the Product Delivery Options in the dashboard (shipped using a flat rate fee at 0.00 per shipment)?

    Never mind, just checked it out. Need the code. Thanks again for your help!!
Sign In or Register to comment.