Shipping calculation

caddycaddy Member
in Help edited March 2010
I got tasked with implementing a really simple little store for a client. Only six products, no options, etc.

I need to figure out how to implement their shipping rates, which is $7 plus .50 per pound up to 10 pounds, $1 per pound after that.

It appears I have to create a small bit of code and put it somewhere but I need a starting place to explain how this works. I've found several threads, but they seem to be several years old. Is this actually documented anywhere?
  • brettbrett FoxyCart Team
    Hi @caddy.
    Yeah, this is one of those custom tiered rate plans that requires a little javascript hacking. It's not impossible but you'll need to be up on your javascript chops. Here are some links to get you started:

    If you need help we can recommend somebody to you or we can try to help you on the forum.

    Out of curiosity, what about shipping to Alaska or Hawaii? Canada? Other non-US countries? I'm interested in how that shipping rate structure makes sense for all destinations. Would a live rate with a custom handling fee perhaps make more sense from USPS or UPS or FedEx? Those are much easier.
  • caddycaddy Member
    Thanks, but I don't see how those links really help. They all start from the assumption that you know the basics of how the underlying system works. Is there any documentation anywhere that explains *where* this code goes, *how* it gets invoked, etc?
  • brettbrett FoxyCart Team
    We're seeing if somebody on our end can document this a little prettier, but since it's all kind of advanced javascript we want to make sure we don't give you something that'd just cause frustration.
  • bjbkbjbk Member
    edited April 2010
    @caddy, I'll try to give you a bit more information on how it all works, the reason its not really documented heaps well as this sort of javascript isnt officially supported by FoxyCart, but Brett and Luke do a lot of support on it anyway because until SuperShip comes out in the next major version, there's a lot of people that need custom shipping.

    To start off with, heres the code that would cover what you're asking for:
    <script type="text/javascript" charset="utf-8">
    function calculateShipping(weight) {
    // Set the initial shipping cost
    var shippingCost = 7;
    // Tiered shipping calculations
    if (weight <= 10) {
    // The weight is less than or equal to 10 pounds, calculate at the discounted rate
    shippingCost += .5 * weight;
    } else {
    // Weight is over 10, add shipping rate for 10 pounds then add the rest at $1 per pound
    shippingCost += 5;
    shippingCost += weight - 10;
    // return the calculated shipping cost
    return shippingCost;
    jQuery(document).ready(function() {
    // Manually set the orderFlatRateShipping value using our function, passing the total_weight figure from the json to the function
    FC.checkout.config.orderFlatRateShipping = calculateShipping(fc_json.total_weight);
    // Run the function which will update the shipping figures for the order (-1 marks it as not a multiship order) which will use the orderFlatRateShipping value we just set.

    You then paste the above code before the
    tag in your checkout template, and you would also need to set your shipping to use flat rate shipping with a value of 0 and per shipment in the category settings in the FoxyCart administration. Because you set it to flat rate in your settings, FoxyCart looks for the orderFlatRateShipping figure when it updates the shipping values, which explains the last two lines of code in the block there.

    Hopefully the comments in the code explain how the code works. The function could be altered to be tiered against any value from the json. For example, you could easily change it to the product_count, and test against that in the tiered calculations. To prove my point, this is what it would look like for product count:
    <script type="text/javascript" charset="utf-8">
    function calculateShipping(amount) {
    var shippingCost = 0;
    if (amount <= 5) {
    shippingCost += 5 * amount; // $5 per product
    } else if (amount <= 10) {
    shippingCost += 3 * amount; // $3 per product
    } else if (amount <= 20) {
    shippingCost += 2 * amount; // $2 per product
    } else { // Must be 21 or more products
    shippingCost = 0; // Free shipping because they bought so much
    return shippingCost;
    jQuery(document).ready(function() {
    FC.checkout.config.orderFlatRateShipping = calculateShipping(fc_json.product_count);

    Anyway, hope that helps you with what you need.
  • @bjbk This is awesome -- thanks so much for posting such nicely commented code!

    I have an additional question: I would like to calculate the shipping without abandoning the ability to use the dynamically-calculated shipping value. That is to say, my client is looking to implement the following shipping scenario:

    * If users go with FedEx, shipping costs whatever the FedEx rate is plus $10 handling. There is no order total discount.
    * If users go with UPS, shipping costs whatever the UPS rate is plus $3.50 handling. For orders over $100, shipping & handling is free.

    I've got a function that is doing part of this, but the two things I'm missing are: 1) The variable I can check to see if the shipping method is FedEx or UPS and 2) the calculated shipping value, rather than a flat rate value.

    Any ideas?

    Thanks in advance!

  • brettbrett FoxyCart Team
    I'll let @bjbk continue this, but:
    1) I'd just do a quick search on the string with javascript to see if it's UPS or FedEx. Pretty straightforward and bulletproof.
  • bjbkbjbk Member
    edited April 2010
    @TheCosmonaut, that function is a little different, as it has to hook into a function to ensure it runs everytime the user changes their shipping choice, ill post back soon.

    [edit] Replied to @TheCosmonaut in this thread
  • Is it possible to set the shipping cost based on the subtotal for three locations: US, Canada & everyone else:

    Example USA
    0-$30 subtotal = $6 shipping
    $31-$60 subtotal = $8 shipping
    $61-$90 subtotal = $10 shipping
    $91 & greater subtotal = FREE shipping

    Example Canada
    0-$30 subtotal = $7 shipping
    $31-$60 subtotal = $10 shipping
    $61-$90 subtotal = $12 shipping
    $91 & greater subtotal = FREE shipping

    Example International
    0-$40 subtotal = $16 shipping
    $41-$70 subtotal = $22 shipping
    $71-$100 subtotal = $26 shipping
    $101 & greater subtotal = FREE shipping
  • lukeluke FoxyCart Team
    Hello ajstocco. There are some examples listed here that should help you get going:
  • Old thread but...

    Tiered Flat Rate Shipping.

    I've set
    onLoad: false, // Set to false if you don't want shipping calculated when the checkout loads
    onLocationChange: true;

    So customer has to enter location to run some country tests for shipping.

    But it appears to run twice. Second pass has shippingCost=0 and finds that the country hasn't changed so stops the calculation. Possibly due to shipping including Tax so Tax updates and results in the code being called again... just a guess.

    Store isn't live
  • Update.

    Above wasn't very clear.
    Essentially, if the customer puts in the correct country the first time they will probably be okay. But when the country is changed a chain of events occurs which results in multiple calls to calculateShipping via updateTaxes and runShippingLogic. One test I did had 4 calls to calculateShipping, with subsequent calls determining that the country hadn't changed so no need to set a shippingCost which defaults to 0.

    So a workaround I'm using is to move FC.checkout.config.orderFlatRateShipping = shippingCost; up into the if country != new country block.
    This way only if there is a country change will shipping be set. I worried it might not work on first pass (ie if default country is used) but it seems okay.

    Any thoughts? Are multiple calls to calculateShipping inevitable and harmless with my fix?

  • fc_adamfc_adam FoxyCart Team

    Interesting. A similar issue has come up for the multiple flat rates snippet, so I'll need to come up with a more solid way around this possibly - but your approach would get around it as well, good catch.
  • Adam,

    I found a few similar oddities on mult flat rates a while back. But not this same thing. Will try and replicate on

    Puzzling why no one spotted this before. Probably if the country etc are entered correctly in order the issue never arises and most people tend to not get confused as to which country they live in :-)
  • Red_EmberRed_Ember Member
    edited February 2014
    I'm new to FoxyCart, and have the beginnings of a store just about ready to go on a static site (no CMS). Everything seemed to be going smoothly until I got to Shipping.
    I'm trying to do the same thing as ajstocco.

    This approach to shipping seems simple enough, but:
    A) I'm concerned about the stern warning given on the Shipping Snippets page (How touchy/sensitive is this approach?)

    B ) People have posted concerns about country calculations fouling up the checkout process. Has this issue been worked out so there's a clear process for us to follow?

    C) The Tiered Flat Rate Shipping snippet page doesn't list version 1.1 as supported, but as a new account, it seems I'm forced into the latest version. Will this work?

    D) Even if this does work for 1.1, the examples show adjustments based on Product Count and Country (not combined). Can someone post the code that solves the scenario that ajstocco posted over 3 years ago since it's exactly my issue as well.

    Would be so convenient if tiered options could be input on the Shipping Options page. Even the WP e-commerce plugin we're replacing with FoxyCart made this easier to do, and that was nearly 4 years ago.
  • winstonwinston FoxyCart Team
    @Red_Ember, I just responded to your email with more information, please let me know if you have further questions there!
Sign In or Register to comment.