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.

run live rate shipping modification on location change?

dustystringsdustystrings Member
in Help edited June 2014
I am trying some complicated maneuvering with with the Live Rate Shipping Modification snippet, and it seems to do some strange things when I change the address. Is there a way to make it run again when the address is changed? I've seen the onLocationChange option for some of the other snippets, but my javascript knowledge is limited and I'm not sure how to apply that to the FC.customLiveShipping script. Or if there's some equivalent already in place in that snippet, perhaps it is not the cause of my problem.

The script is copied directly from the Live Shipping Rate Modification (https://wiki.foxycart.com/snippets/shipping/live_rate_shipping_modification), and I have only changed stuff in the custom shipping logic area.

What happens is like this:
When I put in an address in France, I get the correct shipping cost and everything looks good. Then, if I simply change the address to one in Canada, the cost next to "Shipping Methods" is more than twice what it should be, while the cost next to "Shipping & Handling" is much too low. Not to mention that the two don't match. :-) If I refresh the checkout page and put in the Canada address, I get correct and matching amounts.

Any thoughts?
Comments
  • fc_adamfc_adam FoxyCart Team
    @dustystrings,

    The live rate shipping modification snippet should re-run your logic whenever new rates are fetched - which is whenever you change the shipping address. That should all happen automatically already.

    Could you let us know what store this is for and what addresses we can use to replicate what you're seeing? Feel free to whisper it if it's private.
  • Hmm, I think my other comment went missing...

    I did some more testing, and it looks like the problem is only with items in certain categories, so I think it's actually a problem with a section of my custom logic. The wonky section is this:
    if (countLarge > 0) {
    		//if any LARGE items are present, break into multiple packages and add $5 to total shipping cost
    		jQuery(document).ready(function() {
    		  var package_count = countLarge + countLargeAccessories;
    		 
    		  // sets average_weight to expected total weight
    		  var average_weight = fc_json.total_weight;
    		 
    		  if (package_count > 1) {
    			average_weight = fc_json.total_weight/package_count;
    		 
    			FC.checkout.config.orderLiveRateShipmentWeight = average_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]) * package_count) + 5;
    				jQuery(this).val(rate[0] + "|" + adjusted_total).siblings(".fc_shipping_cost").html(FC.formatter.currency(adjusted_total, true));
    			  });
    			}
    		  });
    		});
    	} 
    

    What I'm trying to do is modify this script: https://wiki.foxycart.com/snippets/shipping/breaking_shipments_over_maximum_weight_into_smaller_packages so that it takes the total weight and divides it by the number of packages to get the average package weight, then sends that to the carrier and multiplies the returned rate by the number of packages and adds a $5 handling fee. I obviously don't know what I'm doing, because this code spits out a number that's pretty far off, and it also causes the problem described before when I change the shipping country.
  • I think the code got cut off, so here's another try:

    if (countLarge > 0) {
    //if any LARGE items are present, break into multiple packages and add $5 to total shipping cost
    jQuery(document).ready(function() {
    var package_count = countLarge + countLargeAccessories;

    // sets average_weight to expected total weight
    var average_weight = fc_json.total_weight;

    if (package_count > 1) {
    average_weight = fc_json.total_weight/package_count;

    FC.checkout.config.orderLiveRateShipmentWeight = average_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]) * package_count) + 5;
    jQuery(this).val(rate[0] + "|" + adjusted_total).siblings(".fc_shipping_cost").html(FC.formatter.currency(adjusted_total, true));
    });
    }
    });
    });
    }
  • fc_adamfc_adam FoxyCart Team
    @dustystrings,

    Ah yep - that would be an issue, it's not quite as simple as just pasting that other snippet into the custom logic of the live rate shipping modification. You'll need to break part of your logic out into it's own script block, and change the other logic to match how the live shipping rate modification snippet works:

    Paste this before the shipping modification snippet script block:
    <script type="text/javascript">
    //trying a different version to count how many of each category there are:
    var countLarge = 0;
    var LARGE = ["LARGE"];
    var countLargeAccessories = 0;
    var LARGE_ACCESSORIES = ["LARGE_ACCESSORIES"];
    var countAccessories = 0;
    var ACCESSORIES = ["ACCESSORIES"];
    
    for (var p in fc_json.products) {
    	if (jQuery.inArray(fc_json.products[p].category, LARGE) > -1) {
    		countLarge += fc_json.products[p].quantity;
    	}
    
    	if (jQuery.inArray(fc_json.products[p].category, LARGE_ACCESSORIES) > -1) {
    		countLargeAccessories += fc_json.products[p].quantity;
    	}
    
    	if (jQuery.inArray(fc_json.products[p].category, ACCESSORIES) > -1) {
    		countAccessories += fc_json.products[p].quantity;
    	}
    }
    
    if (countLarge > 0) {
    	var package_count = countLarge + countLargeAccessories;
    		 
    	// sets average_weight to expected total weight
    	var average_weight = fc_json.total_weight;
    
    	if (package_count > 1) {
    		average_weight = fc_json.total_weight/package_count;
    
    		FC.checkout.config.orderLiveRateShipmentWeight = average_weight;
    	}
    }
    </script>
    

    And then update your custom shipping logic code like this:
    /* BEGIN CUSTOM SHIPPING LOGIC */
     
    //defines tiered rates for US (shippingCost) and Canada (CAshippingCost)
    var country = (jQuery("#use_different_addresses").is(":checked") ? $("#shipping_country").val() : $("#customer_country").val());
    var shippingCost = 0;
    var CAshippingCost = 0;
    var amount = fc_json.total_item_price;
    	if (amount <= 30) {
    		 shippingCost = 4.50;
    		} else if (amount <= 90) {
    		 shippingCost = 7.95; 
    		} else if (amount <= 150) {
    		 shippingCost = 10.95; 
    		} else {
    		 shippingCost = 15.95;
    		}
    	if (amount <= 90) {
    		 CAshippingCost = 7.95;
    		} else if (amount <= 150) {
    		 CAshippingCost = 10.95; 
    		} else {
    		 CAshippingCost = 15.95;
    		}
    		
    
    //defines FlatRateShipping method based on # of LARGE items	
    var LargeFlatRate = 0;
    LargeFlatRate = 40.00 * countLarge;
    var LargeFlatRatePlus = 0;
    LargeFlatRatePlus = LargeFlatRate + (7.95 * countLargeAccessories);
    
    if (country == "US") {
    	if (countLarge > 0 && countLargeAccessories > 0) {
    		FC.customLiveShipping.remove("all"); //removes all live rates	
    		FC.customLiveShipping.add(135, LargeFlatRatePlus, "", 'Standard Shipping'); //instead shows $40 flat rate for each Large item, plus $7.95 for each Large Accessory that can't fit in the same box (assuming that smaller accessories can fit in the same box).
    	}
    	else if (countLarge > 0) {
    		FC.customLiveShipping.remove("all"); //removes all live rates	
    		FC.customLiveShipping.add(125, LargeFlatRate, "", 'Standard Shipping'); //instead shows flat rate based on number of large items present and disregards other items (assuming they can go in one of the larger boxes)
    	} else {
    		FC.customLiveShipping.remove("all"); //removes all live rates
    		FC.customLiveShipping.add(100, shippingCost, "", 'Standard Shipping'); //instead shows tiered rates based on total price of items
    	}
    } else if (country == "CA") {
    	if (countLarge > 0) {
    		//if any large items are present, break into multiple packages and add $5 to total shipping cost
    		FC.customLiveShipping.update("all", "*" + package_count);
    		FC.customLiveShipping.update("all", "+5");
    
    	} else if (countLargeAccessories > 0) {
    		FC.customLiveShipping.update ("all", "+10"); //adds $10 handling fee to orders including large_accessories but not large (to cover instances where other accessories/strings are ordered also, and can't be combined in a single box)
    	} else {
    		FC.customLiveShipping.update ("all", "+2.50"); //adds $2.50 handling fee to live rates for string and accessory orders
    	} 
    } else {
    	if (countLarge > 0) {
    		//if any large items are present, break into multiple packages and add $5 to total shipping cost
    		FC.customLiveShipping.update("all", "*" + package_count);
    		FC.customLiveShipping.update("all", "+5");
    	} else if (countLargeAccessories > 0) {
    		FC.customLiveShipping.update ("all", "+10"); //adds $10 handling fee to orders including large_accessories but not large
    	} else {
    		FC.customLiveShipping.update ("all", "+5"); //adds $5 handling fee to live rates for string and small accessory orders
    	}
    }
        /* END CUSTOM SHIPPING LOGIC */
    


    I haven't tested that, so make sure you give it a good test - but that should work.
  • Thank you very much!

    It does appear to function, and does not give me any problems when I change shipping addresses, but I'm not sure it's doing quite what I want. Can you explain what these lines are doing?

    FC.customLiveShipping.update("all", "*" + package_count);
    FC.customLiveShipping.update("all", "+5");

    Is that actually multiplying the returned rate by package_count and then adding $5? If I do a test with two 27 lb. large items (total weight = 54 lbs, package_count should be 2), the total cost that's returned is about $15 higher than if I look it up directly with USPS.

    If I test with a single large item, the calculated cost is about $10 higher than it should be.

    Is there any way for me to tell whether this is something in the script vs. something to do with how rates are looked up with USPS?
  • Never mind! It looks like Foxycart looks up the regular price, and our shipping program here looks up the "online price" so it is actually working.

    Thanks for fixing it for me!
  • fc_adamfc_adam FoxyCart Team
    @dustystrings,

    Happy to help!
Sign In or Register to comment.