Foxy Forum Status

We're no longer responding to questions via our forum, but we will keep it up for historical reasons. If you can't find the answer you're looking for, please visit our knowledge base or contact us. If there's enough interest in the future, we may bring the forum back.

Customizing Live Rate Shipping

I have followed the live_rate_shipping_modification wiki exactly (or so I think I am) but this does not seem to be working at all for me. I added this template to the checkout template in the admin area. Simply trying to only offer free shipping for orders over $100. Right now the code I have should just remove free shipping altogether. It looks like all the correct scripting is output on the page, but free shipping is still there. What am I doing wrong?


//
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.hide('free');
/* END CUSTOM SHIPPING LOGIC */
}

//]]>



// /* Live Rate Shipping Modification Logic v1.3 */

jQuery(document).ready(function() {
jQuery(document).ajaxComplete(function(event, request, settings) {
if (settings.url.indexOf('GetShippingCost') != -1) {
FC.customLiveShipping.execute();
}
});

});


FC.customLiveShipping.add = function(code, cost, carrier, service) {
var newShippingOption = '';
jQuery("#fc_shipping_methods_inner").append(newShippingOption);
}

FC.customLiveShipping.hide = function(selector) {
if (typeof(selector) != "number") {
FC.customLiveShipping.alterShippingOptions(FC.customLiveShipping.hide, selector);
} else {
jQuery("label[for=shipping_service_" + selector + "]").hide();
}
}

FC.customLiveShipping.show = function(selector) {
if (typeof(selector) != "number") {
FC.customLiveShipping.alterShippingOptions(FC.customLiveShipping.show, selector);
} else {
jQuery("label[for=shipping_service_" + selector + "]").show();
}
}

FC.customLiveShipping.update = function(selector, modifier) {
if (typeof(selector) != "number") {
FC.customLiveShipping.alterShippingOptions(FC.customLiveShipping.update, selector, modifier);
} else {
var rateAttributes = FC.customLiveShipping.getShippingAttributes(jQuery("input#shipping_service_" + selector)),
price = FC.customLiveShipping.modifyPrice(rateAttributes.price, modifier);
jQuery("input#shipping_service_" + selector).val(selector + '|' + price).siblings("span.fc_shipping_cost").html(FC.formatter.currency(price, true));
}
}

FC.customLiveShipping.remove = function(selector) {
if (typeof(selector) != "number") {
FC.customLiveShipping.alterShippingOptions(FC.customLiveShipping.remove, selector);
} else {
jQuery("label[for=shipping_service_" + selector + "]").remove();
}
}

FC.customLiveShipping.reset = function() {
FC.checkout.updateShipping(-1);
}

FC.customLiveShipping.execute = function() {
if (!jQuery("#fc_shipping_methods_inner input[type='radio']:first").data("custom-shipping-logic-applied")) {
FC.customLiveShipping.logic();

if (FC.customLiveShipping.config.autoSelect) {
jQuery("#fc_shipping_methods_inner input[type='radio']:first").attr("checked", "checked");
}

// Remove hidden rates - they're obviously not needed
jQuery("#fc_shipping_methods_inner label[for^='shipping_service']:hidden").remove();

FC.checkout.updatePrice(-1);
jQuery("#fc_shipping_methods_inner input[type='radio']").data("custom-shipping-logic-applied", true);
}
}

FC.customLiveShipping.alterShippingOptions = function(func, selector, modifier) {
if (typeof(selector) == "number") {
func.call(null, selector, modifier);

} else if (typeof(selector) == "string") {
var rates = [];
if (selector.toLowerCase() == "all") {
// This applies to all returned rates
rates = jQuery("#fc_shipping_methods_inner label[for^='shipping_service']");
} else {
// Some filter has been specified
var regex = /(fedex|usps|ups)?\s?([\w\s]+)?/i,
provider = regex.exec(selector);

if (provider == undefined) return;

var carrierSelector = "span.fc_shipping_carrier";
if (provider[1] != undefined) {
switch(provider[1].toLowerCase()) {
case "fedex":
carrierSelector = "span.fc_shipping_carrier:contains('FedEx')";
break;
case "usps":
carrierSelector = "span.fc_shipping_carrier:contains('USPS')";
break;
case "ups":
carrierSelector = "span.fc_shipping_carrier:contains('UPS')";
break;
}
}

if (provider[2] != undefined) {
rates = jQuery("#fc_shipping_methods_inner label[for^='shipping_service'] "+carrierSelector).siblings("span.fc_shipping_service").filter(function() {
return (jQuery(this).text().toLowerCase().indexOf(provider[2].toLowerCase()) > -1);
}).parent();
} else {
rates = jQuery("#fc_shipping_methods_inner label[for^='shipping_service'] "+carrierSelector).parent();
}
}

rates.each(function() {
var rateAttributes = FC.customLiveShipping.getShippingAttributes(jQuery(this).children("input[name='shipping_service']"));
func.call(null, parseInt(rateAttributes.id), modifier);
});

} else if (typeof(selector) == "object") {
// Assume it's an array of codes
for (var i in selector) {
func.call(null, parseInt(selector[i]), modifier);
}
}
}

FC.customLiveShipping.getShippingAttributes = function(item) {
if (typeof(item) == "number") {
// Passed the code
item = jQuery("input#shipping_service_" + item);
}

var carrier = "",
service = "",
inputVal = 0,
id = 0,
price = 0;

if (item.length) {
carrier = item.siblings(".fc_shipping_carrier").html(),
service = item.siblings(".fc_shipping_service").html(),
inputVal = item.val().split("|"),
id = parseInt(inputVal[0]),
price = parseFloat(inputVal[1]);
}

return {"carrier": carrier, "service": service, "id": id, "price": price};
}

FC.customLiveShipping.modifyPrice = function(price, modifier) {
var modifier = modifier.toString(),
regex = /([\+\-\=\*\/])?(\d+(?:\.\d+)?)(\%)?/,
parts = regex.exec(modifier),
price = parseFloat(price),
modifyBy = parseFloat(parts[2]);

if (parts[3] != undefined) {
modifyBy = price * (modifyBy / 100);
}

var operator = (parts[1] == undefined) ? "=" : parts[1];
switch(operator) {
case "+":
price = (price + modifyBy);
break;
case "-":
price = (price - modifyBy);
break;
case "/":
price = (price / modifyBy);
break;
case "*":
price = (price * modifyBy);
break;
default:
price = modifyBy;
}

return (price < 0) ? 0 : price;
}
//]]>
Comments
  • I should also note that I'm using FoxyShop on Wordpress
  • [SOLVED]
    Having spent (I refrain from using the term "wasted") many hours playing around with what ended up being old code, the link for modifying live shipping rates for those using v2.0+ is: https://wiki.foxycart.com/v/2.0/snippets/live_rate_shipping_modification . Works like a charm and wish I had found it a long time ago.

    I will reiterate the words of some other forum members here. Please make v2.0 docs a little easier to work with. I'm bookmarking marking the snippets page now that I found it since there isn't a link to it that I can see.
  • fc_adamfc_adam FoxyCart Team
    @mrlarson,

    Sorry for the confusion you experienced there. We've tried to make it clear from the snippets page that there is a different page for version 2.0 - I'll add some warning notes to the top of the key snippets too.
Sign In or Register to comment.