Migrating old custom shipping snippet

I'm hoping for a pointer in the right direction. I've looked at the page about migrating custom shipping snippets to the new custom shipping code, but I'm not very javascript-literate, and we've got a slightly more complicated situation than in the examples. Converting the logic seem to be pretty straightforward, but we've got an extra script block that is outside of the custom shipping logic, and I'm not sure where to put that in the migration, or if it's still necessary to keep it in a separate block. I've copied our (quite long, sorry!) custom shipping code below. If anyone can clarify which parts to put where in the migration, then I'm happy to mess with the rest of the conversion on my own and see if I can get through it. Thanks!


{% if context == 'cart' or context == 'checkout' %}
<script type="text/javascript">
//this part needs to be in its own script block before the live rate shipping modification
//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.items) {
if (jQuery.inArray(FC.json.items[p].category, LARGE) > -1) {
countLarge += FC.json.items[p].quantity;
}

if (jQuery.inArray(FC.json.items[p].category, LARGE_ACCESSORIES) > -1) {
countLargeAccessories += FC.json.items[p].quantity;
}

if (jQuery.inArray(FC.json.items[p].category, ACCESSORIES) > -1) {
countAccessories += FC.json.items[p].quantity;
}
}

//counts number of items that are too big for first-class international mail
var quantity_no_first_class = 0;
for (var p in FC.json.items) {
for (var i = 0; i < FC.json.items[p].options.length; i++) {
if (FC.json.items[p].options[i].class == "no_first_class" && FC.json.items[p].options[i].value == "true") {
quantity_no_first_class += FC.json.items[p].quantity;
}
}
}

var package_count = 1;
var additional_package = 0;

//if any large items are present, determine package count by total number of Large and LargeAccessories (which have to ship in separate boxes). Additional packages are anything over the single box that FC assumes.
if (countLarge > 0) {
var package_count = countLarge + countLargeAccessories;
var additional_package = package_count - 1;
}
</script>

<script type="text/javascript" charset="utf-8">
(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 */

//defines tiered rates for US (shippingCost)
var country = FC.json.shipping_address.country;
var shippingCost = 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;
}

//defines FlatRateShipping method based on # of LARGE items
var LargeFlatRate = 0;
LargeFlatRate = 40.00 * countLarge;
var LargeFlatRatePlus = 0;
LargeFlatRatePlus = LargeFlatRate + (7.95 * countLargeAccessories);

if (quantity_no_first_class > 0) FC.customLiveShipping.remove("USPS First-Class");
if (amount > 399) FC.customLiveShipping.remove("USPS First-Class");
FC.customLiveShipping.update("USPS First-Class Package International Service", null, null, "First-Class Package International (1 to 6 weeks)");
FC.customLiveShipping.update("USPS Priority Mail International", null, null, "Priority Mail International (avg. 6 to 10 business days)");

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 && additional_package == 0) {
FC.customLiveShipping.update("all", "+5"); //if a large item is present, but no additional packages, just add the $5 handling fee
} else if (countLarge > 0 && additional_package == 1) {
FC.customLiveShipping.update("all", "+28"); //if any large items are present, add a $23 surcharge per additional package, plus regular $5 handling fee. FC assumes it's all one box, so the returned rates are artificially low. No current way to break into multiple packages, so surcharge is added to cover the difference.
} else if (countLarge > 0 && additional_package > 1) {
FC.customLiveShipping.update("all", "+51"); //if any large items are present, add a $23 surcharge per additional package, plus regular $5 handling fee. Assuming for now that there would be a max of 2 additional packages in any normal order.
} else if (countLargeAccessories > 0 && countAccessories > 0){
FC.customLiveShipping.update ("all", "+20"); //adds $20 surcharge to orders with large_accessories and regular accessories, but no large. To cover multiple packages.
} else if (countLargeAccessories > 1) {
FC.customLiveShipping.update ("all", "+30"); //adds $30 surcharge if sending multiple large_accessories
} else {
FC.customLiveShipping.update ("all", "+5"); //adds $5 handling fee to live rates for string and small accessory orders, or large accessories on their own
}
} else { //countries outside of US or Canada
if (countLarge > 0 && additional_package == 0) {
FC.customLiveShipping.update("all", "+5"); //if a large item is present, but no additional packages, just add the $5 handling fee
} else if (countLarge > 0 && additional_package == 1) {
FC.customLiveShipping.update("all", "+35"); //if any large items are present, add a $30 surcharge per additional package, plus regular $5 handling fee. FC assumes it's all one box, so the returned rates are artificially low. No current way to break into multiple packages, so surcharge is added to cover the difference.
} else if (countLarge > 0 && additional_package > 1) {
FC.customLiveShipping.update("all", "+65"); //if any large items are present, add a $30 surcharge per additional package, plus regular $5 handling fee. Assuming for now that there would be a max of 2 additional packages in any normal order.
} else if (countLargeAccessories > 0 && countAccessories > 0) {
FC.customLiveShipping.update ("all", "+20"); //adds $20 surcharge to orders with large_accessories and regular accessories, but no large. To cover multiple packages. (doesn't quite cover
} else if (countLargeAccessories > 1) {
FC.customLiveShipping.update ("all", "+30"); //adds $30 surcharge if sending multiple large_accessories (assuming separate boxes)
} else {
FC.customLiveShipping.update ("all", "+5"); //adds $5 handling fee to live rates for string and small accessory orders
}
}
/* END CUSTOM SHIPPING LOGIC */
};
Comments
  • brettbrett FoxyCart Team
    Hey @dustystrings. We're happy to help. Give us a bit to review and get back to you.
  • @brett no problem, thank you!
  • fc_adamfc_adam FoxyCart Team
    @dustystrings,

    Taking what you've detailed above, the following should be the code as needed for the new custom shipping code functionality:
    const country = cart['_embedded']['fx:shipment']['country'],
    amount = cart['_embedded']['fx:shipment']['total_item_price'];

    let countLarge = 0,
    LARGE = ["LARGE"],
    countLargeAccessories = 0,
    LARGE_ACCESSORIES = ["LARGE_ACCESSORIES"],
    countAccessories = 0,
    ACCESSORIES = ["ACCESSORIES"],
    quantity_no_first_class = 0,
    package_count = 1,
    additional_package = 0,
    shippingCost = 0,
    LargeFlatRate = 0,
    LargeFlatRatePlus = 0;

    for (let p in cart['_embedded']['fx:items']) {
    let item = cart['_embedded']['fx:items'][p];
    if (LARGE.includes(item['_embedded']['fx:item_category']['code'])) {
    countLarge += item.quantity;
    }
    if (LARGE_ACCESSORIES.includes(item['_embedded']['fx:item_category']['code'])) {
    countLargeAccessories += item.quantity;
    }
    if (ACCESSORIES.includes(item['_embedded']['fx:item_category']['code'])) {
    countAccessories += item.quantity;
    }

    // counts number of items that are too big for first-class international mail
    for (let o in item['_embedded']['fx:options']) {
    let option = item['_embedded']['fx:options'][o];
    if (option.name == "No First Class" && option.value == "true") {
    quantity_no_first_class += item.quantity;
    }
    }
    }

    //if any large items are present, determine package count by total number of Large and LargeAccessories (which have to ship in separate boxes). Additional packages are anything over the single box that FC assumes.
    if (countLarge > 0) {
    package_count = countLarge + countLargeAccessories;
    additional_package = package_count - 1;
    }

    //defines tiered rates for US (shippingCost)
    if (amount <= 30) {
    shippingCost = 4.50;
    } else if (amount <= 90) {
    shippingCost = 7.95;
    } else if (amount <= 150) {
    shippingCost = 10.95;
    } else {
    shippingCost = 15.95;
    }

    //defines FlatRateShipping method based on # of LARGE items
    LargeFlatRate = 40.00 * countLarge;
    LargeFlatRatePlus = LargeFlatRate + (7.95 * countLargeAccessories);

    if (quantity_no_first_class > 0) rates.filter("USPS First-Class").hide();
    if (amount > 399) rates.filter("USPS First-Class").hide();
    rates.filter("USPS First-Class Package International Service").service("First-Class Package International (1 to 6 weeks)");
    rates.filter("USPS Priority Mail International").service("Priority Mail International (avg. 6 to 10 business days)");

    if (country == "US") {
    if (countLarge > 0 && countLargeAccessories > 0) {
    rates.hide(); //removes all live rates
    rates.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) {
    rates.hide(); //removes all live rates
    rates.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 {
    rates.hide(); //removes all live rates
    rates.add(100, shippingCost, "", 'Standard Shipping'); //instead shows tiered rates based on total price of items
    }
    } else if (country == "CA") {
    if (countLarge > 0 && additional_package == 0) {
    rates.price("+5"); //if a large item is present, but no additional packages, just add the $5 handling fee
    } else if (countLarge > 0 && additional_package == 1) {
    rates.price("+28"); //if any large items are present, add a $23 surcharge per additional package, plus regular $5 handling fee. FC assumes it's all one box, so the returned rates are artificially low. No current way to break into multiple packages, so surcharge is added to cover the difference.
    } else if (countLarge > 0 && additional_package > 1) {
    rates.price("+51"); //if any large items are present, add a $23 surcharge per additional package, plus regular $5 handling fee. Assuming for now that there would be a max of 2 additional packages in any normal order.
    } else if (countLargeAccessories > 0 && countAccessories > 0){
    rates.price("+20"); //adds $20 surcharge to orders with large_accessories and regular accessories, but no large. To cover multiple packages.
    } else if (countLargeAccessories > 1) {
    rates.price("+30"); //adds $30 surcharge if sending multiple large_accessories
    } else {
    rates.price("+5"); //adds $5 handling fee to live rates for string and small accessory orders, or large accessories on their own
    }
    } else { //countries outside of US or Canada
    if (countLarge > 0 && additional_package == 0) {
    rates.price("+5"); //if a large item is present, but no additional packages, just add the $5 handling fee
    } else if (countLarge > 0 && additional_package == 1) {
    rates.price("+35"); //if any large items are present, add a $30 surcharge per additional package, plus regular $5 handling fee. FC assumes it's all one box, so the returned rates are artificially low. No current way to break into multiple packages, so surcharge is added to cover the difference.
    } else if (countLarge > 0 && additional_package > 1) {
    rates.price("+65"); //if any large items are present, add a $30 surcharge per additional package, plus regular $5 handling fee. Assuming for now that there would be a max of 2 additional packages in any normal order.
    } else if (countLargeAccessories > 0 && countAccessories > 0) {
    rates.price("+20"); //adds $20 surcharge to orders with large_accessories and regular accessories, but no large. To cover multiple packages. (doesn't quite cover
    } else if (countLargeAccessories > 1) {
    rates.price("+30"); //adds $30 surcharge if sending multiple large_accessories (assuming separate boxes)
    } else {
    rates.price("+5"); //adds $5 handling fee to live rates for string and small accessory orders
    }
    }
    To apply - you'll want to follow these steps:

    1. Remove the existing custom code from your cart include template - save it to a file on your side as a backup. Unless you have other customisations there that you're needing to maintain, we'd recommend setting the cart include template back to the default responsive template.
    2. On the "shipping" page, enable the "use custom code" option, and paste the code from above into the code editor that appears
    3. Save the page, wait 20-30 seconds (the shipping functionality takes a moment to initialize), then refresh the page to confirm it's saved.

    At that point - if you add a product to the cart and enter a shipping address, rates should come back in exactly the same way as they were when using just the snippet.
  • Wow, thank you so much! You guys always go above and beyond. I will try this out and let you know if I have any trouble!
Sign In or Register to comment.