This is an old revision of the document!


type:
snippet
category:
shipping
name:
Tiered Flat Rate Shipping
versions:
0.6.0, 0.7.0, 0.7.1, 0.7.2
reference:
http://forum.foxycart.com/comments.php?DiscussionID=2880&page=1
tags:
snippets, shipping, advance
date:
2011-05-27

Tiered Flat Rate Shipping

Tiered Flat Rate Shipping, provides flat rates that are calculated based off of any values you want, like cart cost, total weight, products in the cart etc. Note that this applies the shipping automatically - if you want to provide selectable options, check out http://wiki.foxycart.com/snippets/shipping/multiple_flat_rates

Step 1: Update categories

Update all categories to 'Shipped using a flat rate fee' with a value of 0 in the 'Product Delivery Option' section.

Step 2: Add the javascript

Paste this code right before the closing </head> tag of your checkout template:

<script type="text/javascript" charset="utf-8">
FC.checkout.config.customShipping = {
  onLoad: true,  // Set to false if you don't want shipping calculated when the checkout loads
  onLocationChange: false // Set to true if your shipping logic relies on updating whenever the shipping location for the order changes
};
 
function calculateShipping() {
  var shippingCost = 0;
 
  /* BEGIN CUSTOM SHIPPING LOGIC */
 
 
  /* END CUSTOM SHIPPING LOGIC */
 
  FC.checkout.config.orderFlatRateShipping = shippingCost;
  FC.checkout.updateShipping(-1);
}
 
jQuery(document).ready(function() {
  if (FC.checkout.config.customShipping.onLoad) {
    calculateShipping();
  }
 
  if (FC.checkout.config.customShipping.onLocationChange) {
    FC.checkout.overload("updateTaxes", function() { calculateShipping(); }, null);
  }
});
</script>

Step 3: Customise

Now the fun part - create your tiers to calculate your shipping and add your custom code between the /* BEGIN CUSTOM SHIPPING LOGIC */ and /* END CUSTOM SHIPPING LOGIC */ lines in the first script block. These can be as simple or as advanced as you want - see http://wiki.foxycart.com/static/redirect/jsonp for information on values in the JSON object.

Examples

Example 1: Product Count

This is a basic approach, and is easily interchangeable to total price, weight or other values.

var amount = fc_json.product_count;
 
if (amount <= 5) {
  shippingCost += 5 * amount; // $5 per product
} else if (amount <= 10) {
  shippingCost += 3 * amount; // $3 per product
} else if (amount <= 20) {
  shippingCost += 2 * amount; // $2 per product
} else { // Must be 21 or more products
  shippingCost = 0; // Free shipping because they bought so much
}
Example 2: Shipping Country

For this approach, you'll need to set onLocationChange to true in Step 2.

if (typeof(country_code) === "undefined") {country_code = "";}
new_country_code = (jQuery("#use_different_addresses").is(":checked") ? $("#shipping_country").val() : $("#customer_country").val());
if (country_code != new_country_code) { // The shipping country has changed!
  country_code = new_country_code;
  switch (country_code) {
    case "US":
      shippingCost = 10;
      break;
    case "CA":
      shippingCost = 12;
      break;
    default:
      shippingCost = 20;
  }
}

Site Tools