type:
snippet
category:
shipping
name:
Breaking shipments over the maximum weight into smaller packages
versions:
0.6.0, 0.7.0, 0.7.1, 0.7.2, 1.0
reference:
https://forum.foxycart.com/discussion/4365/
tags:
snippets, shipping, weight, advance
date:
2011-05-27

Breaking shipments over the maximum weight into smaller packages

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

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.

For some stores shipping larger products, it can be possible to go over the weight limit for the shipping provider, in which case your customers just receive an error saying that the rates can't be calculated. This script works around that by setting the shipment weight to a smaller acceptable weight and calculating the shipping by the returned rate for the smaller package multiplied by how many of the smaller packages there are. Not perfect, but a great work around until more powerful shipping is implemented in an upcoming version.

Step 1: Add the javascript

Paste the following code above the closing </head> tag in your checkout template:

FoxyCart version 0.6.0+

<script type="text/javascript" charset="utf-8">
jQuery(document).ready(function() {
  maximum_weight = 70;
  package_weight = 30;
 
  // set custom packages to the expected default of 1
  custom_packages = 1;
 
  if (fc_json.total_weight > maximum_weight) {
    custom_packages = fc_json.total_weight/package_weight;
 
    FC.checkout.config.orderShipmentWeight = package_weight;
  }
 
  jQuery(document).ajaxComplete(function(event, request, settings) {
    if (settings.url.indexOf('GetShippingCost') != -1) {
      jQuery("#fc_shipping_methods_inner input.fc_radio").each(function() {
        var rate = jQuery(this).val().split("|");
        var adjusted_total = parseFloat(rate[1]) * custom_packages;
        jQuery(this).val(rate[0] + "|" + adjusted_total).siblings(".fc_shipping_cost").html(FC.formatter.currency(adjusted_total, true));
      });
    }
  });
});
</script>

FoxyCart version 0.7.2+

<script type="text/javascript" charset="utf-8">
jQuery(document).ready(function() {
  maximum_weight = 70;
  package_weight = 30;
 
  // set custom packages to the expected default of 1
  custom_packages = 1;
 
  if (fc_json.total_weight > maximum_weight) {
    custom_packages = fc_json.total_weight/package_weight;
 
    FC.checkout.config.orderLiveRateShipmentWeight = package_weight;
  }
 
  jQuery(document).ajaxComplete(function(event, request, settings) {
    if (settings.url.indexOf('GetShippingCost') != -1) {
      jQuery("#fc_shipping_methods_inner input.fc_radio").each(function() {
        var rate = jQuery(this).val().split("|");
        var adjusted_total = parseFloat(rate[1]) * custom_packages;
        jQuery(this).val(rate[0] + "|" + adjusted_total).siblings(".fc_shipping_cost").html(FC.formatter.currency(adjusted_total, true));
      });
    }
  });
});
</script>

To customise the script, edit the two variables at the start of the script called 'maximum_weight' (set to the heaviest package your shipping provider will return rates for) and 'package_weight' (the size of the smaller package you will send larger shipments in). Obviously, 'package_weight' must be smaller than 'maximum_weight' and should take into account the products your shipping. For example, if the products you're shipping are 30lb and 40lb, and the shipping maximum is 70lb, set your package_weight to 40lb.

What does this script do?

  1. Check if the total weight in the cart is greater than the maximum_weight variable.
  2. If it is, it sets a variable called custom_packages with the number of packages to be shipped which is basically total_weight divided by package_weight (the weight that you plan to send larger shipments out in as multiple packages). It also sets the orderShipmentWeight variable (the figure sent to the rates request functions) to package_weight
  3. Set's a function that is run after the shipping rate request function is run. This script runs through each returned rate, gets its cost, multiplies it by custom_packages and updates each rate in the page with the new total.

Site Tools