type:
snippet
category:
shipping
name:
Shipping calculation on cart
versions:
1.0, 1.1
tags:
snippets, shipping, advance
date:
2013-10-22

Shipping calculation on cart

This script is designed to let you add a live rate calculator on the cart. It's been tested in version 1.1, but could probably be ported to a lower version with a bit of tweaking. This is designed for shipping to and from the US-only. You could likely add more countries in a dropdown if you wanted international. Be sure to fill in the config values and then paste before the </body> tag in the cart template.

<style type="text/css">
#shipping_estimate_container {
	width: 100%;
	margin-top: -53px;
	font-family: Verdana;
	font-size: 12px;
}
</style>
<div id="shipping_estimate_container">
	<h3>Estimate Shipping</h3>
	<form id="estimateform">
	<label for="zip_estimate">US State and Zip Code</label>
	<select name="state_estimage" id="state_estimate">
		<option value="">- -</option>
		<option value="AL">Alabama</option>
		<option value="AK">Alaska</option>
		<option value="AZ">Arizona</option>
		<option value="AR">Arkansas</option>
		<option value="CA">California</option>
		<option value="CO">Colorado</option>
		<option value="CT">Connecticut</option>
		<option value="DE">Delaware</option>
		<option value="DC">District Of Columbia</option>
		<option value="FL">Florida</option>
		<option value="GA">Georgia</option>
		<option value="HI">Hawaii</option>
		<option value="ID">Idaho</option>
		<option value="IL">Illinois</option>
		<option value="IN">Indiana</option>
		<option value="IA">Iowa</option>
		<option value="KS">Kansas</option>
		<option value="KY">Kentucky</option>
		<option value="LA">Louisiana</option>
		<option value="ME">Maine</option>
		<option value="MD">Maryland</option>
		<option value="MA">Massachusetts</option>
		<option value="MI">Michigan</option>
		<option value="MN">Minnesota</option>
		<option value="MS">Mississippi</option>
		<option value="MO">Missouri</option>
		<option value="MT">Montana</option>
		<option value="NE">Nebraska</option>
		<option value="NV">Nevada</option>
		<option value="NH">New Hampshire</option>
		<option value="NJ">New Jersey</option>
		<option value="NM">New Mexico</option>
		<option value="NY">New York</option>
		<option value="NC">North Carolina</option>
		<option value="ND">North Dakota</option>
		<option value="OH">Ohio</option>
		<option value="OK">Oklahoma</option>
		<option value="OR">Oregon</option>
		<option value="PA">Pennsylvania</option>
		<option value="RI">Rhode Island</option>
		<option value="SC">South Carolina</option>
		<option value="SD">South Dakota</option>
		<option value="TN">Tennessee</option>
		<option value="TX">Texas</option>
		<option value="UT">Utah</option>
		<option value="VT">Vermont</option>
		<option value="VA">Virginia</option>
		<option value="WA">Washington</option>
		<option value="WV">West Virginia</option>
		<option value="WI">Wisconsin</option>
		<option value="WY">Wyoming</option>
	</select>
	<input type="text" name="zip_estimate" id="zip_estimate" placeholder="Zip Code" value="">
	<button type="submit" id="estimatenow">Estimate</button>
	</form>
	<div id="shipping_results"></div>
</div>
<script type="text/javascript">
jQuery(document).ready(function($) {

	//Set Config Values **REQUIRED**
	var store_id = ""; //Store ID can be found in source of the checkout. Look for "FC.checkout.config.storeDetails"
	var store_state = ""; //Two-letter abbreviation
	var store_postal_code = ""; //US Zip
	var store_domain = "YOURDOMAIN.foxycart.com"; //Your FoxyCart Domain
	var handling_fee = 0;
	var flat_rate_fee = 0;
	var customs_value_fee = 0;
	var is_residential = "true";

	//Get Current Value
	var current_zip_estimate = fc_json.custom_fields.zip_estimate;
	var current_state_estimate = fc_json.custom_fields.state_estimate;
	if (current_zip_estimate) {
		$("#zip_estimate").val(current_zip_estimate);
		$("#state_estimate").val(current_state_estimate);
		getShippingEstimate();
	}

	$("#estimateform").submit(function(e) {
		e.preventDefault();

		$("#shipping_results").html("Loading...");

		//Sets default values as hidden session vars
		$.getJSON('https://'+store_domain+'/cart?fcsid=' + fc_json.session_id + '&h:state_estimate=' + $("#state_estimate").val() + '&h:zip_estimate=' + $("#zip_estimate").val() + '&output=json&callback=?', function() {
			getShippingEstimate();
		});

		return false;
	});

	function getShippingEstimate() {

		$("#shipping_results").html("Loading...");

		var url = 'https://' + store_domain + '/v/1.1.0/api_json.php?';
		url += "fcsid=" + fc_json.session_id;
		url += "&shipping_state=" + $("#state_estimate").val();
		url += "&shipping_postal_code=" + $("#zip_estimate").val();
		url += "&shipping_country=US";
		url += "&store_state=" + store_state;
		url += "&store_postal_code=" + store_postal_code;
		url += "&store_country=US";
		url += "&UOM_weight=LBS";
		url += "&UOM_length=IN";
		url += "&TotalWeight=" + fc_json.total_shippable_weight;
		url += "&HandlingFee=" + handling_fee;
		url += "&FlatRate=" + flat_rate_fee;
		url += "&CustomsValue=" + customs_value_fee;
		url += "&is_residential=" + is_residential;
		url += "&store_id=" + store_id;
		url += "&ThisAction=GetShippingCost";
		url += "&callback=?";

		$.getJSON(url, function(data) {
			if (data.result == "Error") {
				$("#shipping_results").html(data.details);
			} else if (data.result == "OK") {
				var results = "<ul>";
				for (p in data.shipping_options) {
					price = parseFloat(data.shipping_options[p].price);
					results += "<li>" + data.shipping_options[p].method + " " + data.shipping_options[p].service_name + " $" + price.toFixed(2) + "</li>";
				}
				results += "</ul>";
				$("#shipping_results").html(results);
			}
			console.log(data);
		});

	}

});
</script>

Site Tools