type:
snippet
category:
shipping
name:
Flat rates by country and product category
reference:
http://forum.foxycart.com/comments.php?DiscussionID=4559
tags:
snippets, shipping, advance
date:
2011-05-27

Flat rates by country and product category

Versions 0.7.1 and older: Note that applying javascript shipping modifications using either live or flat rates where you are setting custom values has been found to not work as expected for subscription based products where you need the shipping to apply to each subscription renewal. A fix is in place for versions 0.7.2 and newer.

Using version 2.0? There is a new snippet available for our latest version, available from here.

If you want to have flat rate shipping based on country with different rates for different product categories and charge the shipping per product. Try the following code:

<script type="text/javascript" charset="utf-8">
function updateMyShippingCost() {
 
	// Create variable for total shipping cost
	var totalShippingCost = 0;
 
	// Create an array for Europen countries that have the same shipping cost
	var europeanCountries = new Array("GB", "IE", "DE", "IT", "ES", "FR");
 
	// Set default shipping cost per category
	var shippingCost = {
		DEFAULT: 1,
		dvd: 10,
		tshirt: 20
	};
 
	// Choose the country to ship to
	var country_code = (jQuery("#use_different_addresses").is(":checked") ? $("#shipping_country").val() : $("#customer_country").val());
 
	// Set shipping costs for each category based on country
	switch(country_code) {
		case "AU":
			shippingCost = {
				DEFAULT: 2,
				dvd: 30,
				tshirt: 40
			};
			break;
		case "NZ":
			shippingCost = {
				DEFAULT: 3,
				dvd: 50,
				tshirt: 60
			};
			break;
		case "US":
		case "CA":
			shippingCost = {
				DEFAULT: 4,
				dvd: 70,
				tshirt: 80
			};
			break;
		default:
			// Set shipping cost for the Europen countries defined earlier
			if (jQuery.inArray(country_code, europeanCountries) > -1) {
				shippingCost = {
					DEFAULT: 5,
					dvd: 90,
					tshirt: 100
				};
			}
	}
 
	// 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;
	});
 
	// Set the final shipping cost
	FC.checkout.config.orderFlatRateShipping = totalShippingCost;
 
	// Return the shipping cost for initially selected country
	return totalShippingCost;
}
 
jQuery(document).ready(function() {
 
	// Set shipping cost for the initially selected country
	FC.checkout.config.orderFlatRateShipping = updateMyShippingCost();
	FC.checkout.updateShipping(-1);
 
	// Update shipping cost based on categories and country when updating the price
	FC.checkout.overload("updatePriceDisplay", "updateMyShippingCost", null);
 
});
</script>

Site Tools