Problem With Live Rate Shipping Modification Snippet

RaulRaul Member
Hi. I'm getting an error on checkout.

I added the live rate shipping modification snippet to a store so that I could split up overweight shipments.

I fill out the checkout form with an overweight shipment. It updates properly and shows the correct shipping cost. When submitting I get a refresh with an error saying I didn't select a shipping method. When I lowered the quantity to something that results in less than the 70lb limit, it worked.

I had originally combined the shipment splitting snippet with the snippet to remove the USPS timeframe from the shipping option name. This resulted in the error every time no matter the weight. So I removed it and got it working for underweight purchases.

I used the code from the wiki, but I appended it to the end of another script in the store template configuration custom footer. The other code is for validating cart quantities using a remote endpoint. That code has always worked for multiple other stores, but this is the first store where I'm using live shipping rates, so it could be a conflict in there somewhere.



{% if context == 'cart' or context == 'checkout' %}
<script type="text/javascript" charset="utf-8">
/* Break into smaller packages */
(function (FC, $) {
FC.customLiveShipping = {};

FC.customLiveShipping.config = {
autoSelect: false /* Set to true if you'd like the top shipping option to be automatically selected for the user */
};

FC.customLiveShipping.logic = function() {
/* BEGIN CUSTOM SHIPPING LOGIC */

FC.customLiveShipping.update('all', '*' + FC.customLiveShipping.custom_packages);

/* END CUSTOM SHIPPING LOGIC */
};

FC.customLiveShipping.maximum_weight = 70; // Maximum weight allowed by your carriers
FC.customLiveShipping.package_weight = 30; // The average package size you'll send for overweight packages
FC.customLiveShipping.custom_packages = 1; // Leave this at 1

function overweightShippingHandler() {
if (FC.json.total_weight_shippable > FC.customLiveShipping.maximum_weight) {
FC.customLiveShipping.custom_packages = FC.json.total_weight_shippable/FC.customLiveShipping.package_weight;
FC.json.shipping_address.total_weight_shippable = FC.customLiveShipping.package_weight;
} else {
FC.json.shipping_address.total_weight_shippable = null;
FC.customLiveShipping.custom_packages = 1;
}
}

FC.client.on('cart-shipping-options-update', overweightShippingHandler );
FC.client.on('checkout-shipping-options-update', overweightShippingHandler );
{% if cart_is_fullpage or context == 'checkout' %}FC.client.on("ready.done",overweightShippingHandler);{% else %}overweightShippingHandler();{% endif %}


/* Live Rate Shipping Modification Logic v2.0.14 */
FC.customLiveShipping.add=function(a,b,c,d){FC.json.shipping_address.shipping_results.push({method:c,price:b,service_id:a,service_name:d})};FC.customLiveShipping.hide=function(a){a=FC.customLiveShipping.filterShippingOptions(a);for(var b=0;b<a.length;b++)FC.json.shipping_address.shipping_results[a[b]].hide=!0};FC.customLiveShipping.show=function(a){a=FC.customLiveShipping.filterShippingOptions(a);for(var b=0;b<a.length;b++)FC.json.shipping_address.shipping_results[a[b]].hide=!1};
FC.customLiveShipping.update=function(a,b,c,d){a=FC.customLiveShipping.filterShippingOptions(a);for(var e=0;e<a.length;e++){if("number"===typeof b||"string"===typeof b&&""!==b){var f=FC.customLiveShipping.modifyPrice(FC.json.shipping_address.shipping_results[a[e]].price,b);FC.json.shipping_address.shipping_results[a[e]].price=f}"string"===typeof c&&(FC.json.shipping_address.shipping_results[a[e]].method=c);"string"===typeof d&&(FC.json.shipping_address.shipping_results[a[e]].service_name=d)}};
FC.customLiveShipping.remove=function(a){FC.customLiveShipping.hide(a)};FC.customLiveShipping.reset=function(){FC.util.addressHasLocationInfo(FC.json.shipping_address)&&(0<FC.json.shipping_address.shipping_results.length&&(FC.customLiveShipping.ratesHash=""),FC.cart.showShippingLocationInput(),FC[FC.json.context].getShippingOptions({address:FC.json.shipping_address}))};function customLiveShippingHandler(){FC.customLiveShipping.execute()}
function customCouponHandler(){FC.customLiveShipping.reset()}
FC.customLiveShipping.execute=function(){if(0<FC.json.shipping_address.shipping_results.length&&FC.customLiveShipping.hasChanged()){FC.customLiveShipping.logic(FC.json.shipping_address);for(var a=!1,b=FC.json.shipping_address.shipping_results.length-1;0<=b;b--)FC.json.shipping_address.shipping_results[b].hide?FC.json.shipping_address.shipping_results.splice(b,1):FC.json.shipping_address.shipping_results[b].service_id==FC.json.shipping_address.shipping_service_id&&(a=!0);a||(FC.json.shipping_address.shipping_service_description=
"",FC.json.shipping_address.shipping_service_id=0);FC.json.shipping_address.shipping_results.sort(function(a,b){return a.price-b.price});FC.customLiveShipping.config.autoSelect&&0==FC.json.shipping_address.shipping_service_id&&(FC.json.shipping_address.shipping_service_id=FC.json.shipping_address.shipping_results[0].service_id,FC.json.show_shipping_tbd=!1);FC.Template(FC.json.context).clearOutput();FC.cart.recalculateCartTotals();FC[FC.json.context].renderShippingRates(FC.json.shipping_address);0<
FC.json.shipping_address.shipping_service_id&&jQuery("#shipping_service_id_"+FC.json.shipping_address.shipping_service_id).trigger("click.fc");FC.customLiveShipping.ratesHash=FC.util.hashString(JSON.stringify(FC.json.shipping_address.shipping_results))}};
FC.customLiveShipping.hasChanged=function(){return FC.customLiveShipping.hasOwnProperty("ratesHash")&&""!=FC.customLiveShipping.ratesHash?FC.customLiveShipping.ratesHash!=FC.util.hashString(JSON.stringify(FC.json.shipping_address.shipping_results))?!0:!1:!0};
FC.customLiveShipping.filterShippingOptions=function(a){if("number"==typeof a){for(var b=[],c=0;c<FC.json.shipping_address.shipping_results.length;c++)FC.json.shipping_address.shipping_results[c].service_id==a&&(b=[c]);return b}if("string"==typeof a){for(var b=[],d={},c=0;c<FC.json.shipping_address.shipping_results.length;c++){var e=FC.json.shipping_address.shipping_results[c];d[c]=e.method+" "+e.service_name}if("all"!=a.toLowerCase()){a=/(fedex|usps|ups)?\s?(.+)?/i.exec(a);if(void 0==a||""==a)return;
for(c in d)void 0!=a[1]&&""!=a[1]&&-1==d[c].toLowerCase().indexOf(a[1].toLowerCase())?delete d[c]:void 0!=a[2]&&""!=a[2]&&-1==d[c].toLowerCase().indexOf(a[2].toLowerCase())&&delete d[c]}for(c in d)b.push(parseInt(c));return b}if("object"==typeof a){b=[];for(c=0;c<a.length;c++)for(d=0;d<FC.json.shipping_address.shipping_results.length;d++)FC.json.shipping_address.shipping_results[d].service_id==a[c]&&b.push(d);return b}};
FC.customLiveShipping.modifyPrice=function(a,b){b=b.toString();var c=/([\+\-\=\*\/])?(\d+(?:\.\d+)?)(\%)?/.exec(b);a=parseFloat(a);var d=parseFloat(c[2]);void 0!=c[3]&&""!=c[3]&&(d=d/100*a);switch(void 0==c[1]&&""!=c[1]?"=":c[1]){case "+":a+=d;break;case "-":a-=d;break;case "/":a/=d;break;case "*":a*=d;break;default:a=d}return 0>a?0:a};
FC.client.on("cart-coupon-add.done",customCouponHandler);FC.client.on("cart-coupon-remove.done",customCouponHandler);
{% if context == "cart" and not cart_is_fullpage %}
FC.client.on("cart-shipping-options-update.done",customLiveShippingHandler);FC.customLiveShipping.reset();
{% else %}
$(document).ajaxComplete(function(b,c,a){b=/ThisAction=(\w+)/;void 0!==a&&a.hasOwnProperty("url")&&-1!=a.url.search(b)&&a.url.match(b);FC.customLiveShipping.execute()});FC.client.on("customer-login.done",FC.customLiveShipping.reset);FC.client.on("ready.done",FC.customLiveShipping.reset);
{% endif %}
})(FC, jQuery);


</script>
{% endif %}
Comments
  • fc_adamfc_adam FoxyCart Team
    @Raul,

    Sorry to hear you're running into errors here. From what you've described there - it sounds like the shipping rate signing feature is enabled for your store. This feature prevents the shipping rates from being altered in any way, ensuring that the rates that are received from the carriers is what is chosen by the customer. When you use either the overweight or timeframe snippets, they're altering aspects of the rate that comes in.

    If you jump onto the "shipping" section of your store's FoxyCart administration, down the bottom there is a checkbox for enabling shipping rate signing. If that's checked - uncheck it and you should then see the snippet work as expected. If it isn't selected - if you could confirm your store, we can take a closer look.
  • RaulRaul Member
    Thanks Adam, that was it. I can't believe I missed that!

    One other thing I noticed, is that when combined with the code to remove the USPS timeframe from the option names, the sidecart shipping calculation breaks down. The amount is much larger than expected.

    The USPS timeframe isn't really a big bother though, so I just removed the script from the sidecart and only use it on the checkout page.
  • fc_adamfc_adam FoxyCart Team
    @Raul,

    That's weird! It shouldn't be any different in terms of the calculations between the cart and the checkout. I'm glad you've found a solution for your store though.
Sign In or Register to comment.