The Foxy forums are on the move!

We're in the process of moving our forums over to a new system, and so these forums are now read-only.
If you have a question about your store in the meantime, please don't hesitate to reach out to us via email.

Custom shipping logic w/Multiple flat rate shipping

jurassicfrogjurassicfrog Member
in Help edited October 2013
Hey guys,

Well, I updated the template with the new logic (based off of the Multiple Custom Flat Rate Shipping Options v2.4 snippet) but it doesn't appear to be kicking off... shipping never populates or gets an option. I'm super green w/javascript... any chance you might be able to look and see what I'm screwing up? Thanks!
<script type="text/javascript" charset="utf-8">
  //<![CDATA[
 
  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
    onPreSubmit: true // Set to false if you don't want to load shipping if it hasn't already loaded before the user tries to checkout
  };
 
  function customShippingLogic() {
    /* BEGIN CUSTOM SHIPPING LOGIC */

// shipping rates
	// up to 8oz, Foxycart category "upTo8oz"
		var upTo8ozUS1 = 3.25;
		var upTo8ozUS2 = 1;
		var upTo8ozCanada1 = 8;
		var upTo8ozCanada2 = 4;
		var upTo8ozOther1 = 12;
		var upTo8ozOther2 = 6;

	// Large Molds, Foxycart category "largeMold"
		var largeMoldUS1 = 4;
		var largeMoldUS2 = 1;
		var largeMoldCanada1 = 8;
		var largeMoldCanada2 = 4;
		var largeMoldOther1 = 12;
		var largeMoldOther2 = 6;
  
	// Flat Rate ALL, Foxycart category "flatRate"
		var flatRateUS1 = 5.70;
		var flatRateUS2 = 2.50;
		var flatRateCanada1 = 20;
		var flatRateCanada2 = 10;
		var flatRateOther1 = 24;
		var flatRateOther2 = 12;

	// Init other vars
	var shippingCost = 0;
	var numUpTo8oz = 0;
	var numLargeMold = 0;
	var numFlatRate = 0;
	var upTo8ozExists = 0;
	var largeMoldExists = 0;
	var flatRateExists = 0;
	
	for (p in fc_json.products) { // determine number of products in each Foxycart Category
		switch (fc_json.products[p].category) {
			case "upTo8oz":
				upTo8ozExists = 1;
				numUpTo8oz++;
				break;
			case "largeMold":
				largeMoldExists = 1;
				numLargeMold++;
				break;
			case "flatRate":
				flatRateExists = 1;
				numFlatRate++;
				break;
		}
	}
	
	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;
		removeCustomShippingContainer(); // This call will make sure that when it updates, it starts fresh.

			// Total shipping cost, display options
		if (country_code == "US") { // Domestic shipping
			shippingCost = Math.max((upTo8ozUS1 * upTo8ozExists), (largeMoldUS1 * largeMoldExists), (flatRateUS1 * flatRateExists)) + (upTo8ozUS2 * (numUpTo8oz - 1)) + (largeMoldUS2 * (numLargeMold - 1)) + (flatRateUS2 * (numFlateRate - 1));
			addShippingOption(1, shippingCost, 'USPS','');
		} else if (country_code == "CA") { // Canada
			shippingCost = Math.max((upTo8ozCanada1 * upTo8ozExists), (largeMoldCanada1 * largeMoldExists), (flatRateCanada1 * flatRateExists)) + (upTo8ozCanada2 * (numUpTo8oz - 1)) + (largeMoldCanada2 * (numLargeMold - 1)) + (flatRateCanada2 * (numFlateRate - 1));
			addShippingOption(1, shippingCost, 'USPS','');
		} else { // All other countries
			shippingCost = Math.max((upTo8ozOther1 * upTo8ozExists), (largeMoldOther1 * largeMoldExists), (flatRateOther1 * flatRateExists)) + (upTo8ozOther2 * (numUpTo8oz - 1)) + (largeMoldOther2 * (numLargeMold - 1)) + (flatRateOther2 * (numFlateRate - 1));
			addShippingOption(1, shippingCost, 'USPS','');
		}	
	}
    /* END CUSTOM SHIPPING LOGIC */
  }
 
  //]]>
</script>
 
<script type="text/javascript" charset="utf-8">
  //<![CDATA[
  /* Multiple Flat Rate Shipping Options Logic v2.4 */
 
  jQuery(document).ready(function() {
    jQuery("#fc_custom_shipping_methods_container").on('click', 'input[name=shipping_service]', function(){
      shipping_service_description = jQuery(this).siblings(".fc_shipping_carrier").html();
      shipping_service_description += ((shipping_service_description == "") ? '' : ' ');
      shipping_service_description += jQuery(this).siblings(".fc_shipping_service").html();
      $("#shipping_details").val(shipping_service_description);
      // Launch FoxyCart functionality
      FC.checkout.updatePrice(-1);
    });
 
    if (FC.checkout.config.customShipping.onLoad) {
      runShippingLogic();
    }
 
    if (FC.checkout.config.customShipping.onLocationChange) {
      isValidateAndSubmit = false;
      FC.checkout.overload("updateTaxes", function() { if (!isValidateAndSubmit) { runShippingLogic(); } }, null);
      FC.checkout.overload("validateAndSubmit", function() { isValidateAndSubmit = true; }, function() { isValidateAndSubmit = false });
    }
 
    if (FC.checkout.config.customShipping.onPreSubmit) {
      FC.checkout.overload("validateAndSubmit", function() {if (!jQuery("#shipping_service_id").length) { runShippingLogic(); }}, null);
    }
  });
 
  function runShippingLogic() {
    // Check to see if there are actually shippable products in the current cart before running the custom shipping (0.7.1+ only), or just run it for older carts
    if ((typeof(FC.checkout.config.hasShippableProducts) === "boolean" && FC.checkout.config.hasShippableProducts) || typeof(FC.checkout.config.hasShippableProducts) === "undefined") {
        customShippingLogic();
    }
  }
 
  // example: addShippingOption(1, 4.99, 'PostBox', 'Express Local');
  function addShippingOption(code, cost, carrier, service) {
    if (jQuery("#fc_shipping_methods_inner").length == 0) {
      addCustomShippingContainer();
    }
    carrier = (typeof(carrier) == 'undefined' || carrier == null) ? "" : carrier;
    service = (typeof(service) == 'undefined' || service == null) ? "" : service;
    var newShippingOption = '<label for="shipping_service_' + code + '" class="fc_radio"><input type="radio" class="fc_radio fc_required" value="' + code + '|' + cost + '" id="shipping_service_' + code + '" name="shipping_service" /><span class="fc_shipping_carrier">' + carrier + '</span><span class="fc_shipping_service">' + service + '</span><span class="fc_shipping_cost">' + FC.formatter.currency(cost, true) + '</span></label>';
    jQuery("#fc_shipping_methods_inner").append(newShippingOption);
  }
 
  // example: updateShippingOptionCost(1, 4);
  function updateShippingOptionCost(code, cost) {
    jQuery("input#shipping_service_" + code).val(code + '|' + cost).siblings("span.fc_shipping_cost").html(FC.formatter.currency(cost, true));
    FC.checkout.updatePrice(-1);
  }
 
  // example: removeShippingOption(1);
  function removeShippingOption(code) {
    jQuery("label[for=shipping_service_" + code + "]").remove();
    if (jQuery("#fc_shipping_methods_inner").html() == "") {
      removeCustomShippingContainer();
    }
    FC.checkout.updatePrice(-1);
  }
 
  function addCustomShippingContainer() {
 
jQuery("#fc_custom_shipping_methods_container").html('<h2>Shipping Options</h2><div class="fc_row fc_shipping_methods_container" id="fc_shipping_methods_container"><div class="fc_radio_group_container fc_row fc_shipping_methods" id="fc_shipping_methods"><input type="hidden" value="0" id="shipping_service_id" name="shipping_service_id"><input type="text" style="display:none;" value="" id="shipping_service_description" name="shipping_service_description"><input type="text" value="" id="shipping_details" name="Shipping_Details" style="display:none;" /><div class="fc_shipping_methods_inner" id="fc_shipping_methods_inner"></div><label style="display: none;" class="fc_error" for="fc_shipping_methods">Please select a shipping method.</label></div></div>');
  }
 
  function removeCustomShippingContainer() {
    jQuery("#fc_custom_shipping_methods_container").html("");
    FC.checkout.updatePrice(-1);
  }
  //]]>
</script>
Comments
  • fc_adamfc_adam FoxyCart Team
    @jurassicfrog,

    Could you link us to your checkout where you have this code added? We'd love to take a look at it in action
  • Unfortunately it's on a local dev box running xampp. I'm pasting this into the checkout template in Admin. I'm thinking I may just have to get this on a live site before I can get it working.
  • fc_adamfc_adam FoxyCart Team
    @jurassicfrog - yeah that's fine, but we can still test your checkout template as it's online - could you let me know your store domain?
  • Sure, it's supplyemporium.com. Thanks!
  • fc_adamfc_adam FoxyCart Team
    @jurassicfrog,

    You've mis-spelt a variable name which is causing an error - specifically you've got "numFlateRate" instead of "numFlatRate".

    For an easy way to pick up on these errors, when you're testing your checkout, enable a console of some sort - either a browser native one (Chrome has the Chrome developer tools you can enable) or by using an addon like Firebug. The console will show any javascript errors that may be occuring.
  • Ack... whatta maroon. Thanks. Getting the shipping option. Now I just have to figure out what's wrong with the logic. Negative shipping costs are bad.

    Thanks for all the help!
Sign In or Register to comment.