Custom shipping options and cost formula

JBurnsJBurns Member
I am investigating moving my fulfillment to Amazon Multi-Channel Fulfillment. Amazon offers 3 shipping options, "Standard", "Expedited", and "Priority", which Amazon defines as 3-5 day, 2 day, and 1 day, respectively (the actual times are apparently longer). Amazon does not specify the shipper -- the actual shipping method is likely some combination of Amazon and USPS or UPS. For the MCF service, they charge a fee per item that decreases from as the number of items goes from 1 to 5 and is then a flat per item rate at 5 and above. I'm trying to figure out if it is possible to customize the shipping options to:

1) Have 3 shipping options in the cart, Standard, Expedited, and Priority (or whatever I decide to name them).

2) Have a formula that calculates the shipping rate based on (i) the shipping option and (ii) the number of items in the cart. The formula for calculating the shipping charge would look something like [ Shipping Rate = Base Rate + V1 * SQRT(quantity * V2) ]. The "quantity" is the total quantity of products purchased, regardless of SKU. V1 and V2 would vary depending on the shipping method selected.

Does this sound possible at all?

Thanks,
James
Comments
  • JBurnsJBurns Member
    edited February 20
    To follow up on this, I've tried to build some sample code. This would create 3 rates (Standard, Priority, and Express). The base rate is 5.85, 7.90 and 12.80 for 1 unit. The rate then increases based on a formula depending on the item_count, which I understand is a foxycart variable. Does this make sense as a custom shipping snippet?
    rates.add(10001, 5.85, '', 'Standard (5-10 day)');
    rates.add(10002, 7.90, '', 'Priority (3-5 day)');
    rates.add(10003, 12.80, '', 'Express (1-3 day)');

    var srate_base = 2.25;
    var srate_mult = 2;
    var srate_factor = 2.6;

    var prate_base = 2.25;
    var prate_mult = 2;
    var prate_factor = 4;

    var erate_base = 4.5;
    var erate_mult = 2;
    var erate_factor = 6;

    var standard_by_count = get_rate(srate_base, srate_mult, item_count, srate_factor);
    var priority_by_count = get_rate(prate_base, prate_mult, item_count, prate_factor);
    var express_by_count = get_rate(erate_base, erate_mult, item_count, erate_factor);

    function get_rate(base, mult, count, factor) {
    var calc = base + Math.sqrt(mult * count) * factor;
    return Number.parseFloat(calc).toFixed(2);
    }

    if (cart['_embedded']['fx:shipment']['item_count'] > 1) {
    rates.filter(10001).price(standard_by_count);
    rates.filter(10002).price(priority_by_count);
    rates.filter(10003).price(express_by_count);
    }
  • brettbrett FoxyCart Team
    Hi @JBurns.
    At a glance, that looks solid. I haven't tested it, but I don't see anything glaring in the logic. Have you give that some testing to see if it works? We're happy to help if something in particular isn't working as expected, though it looks like you've got a solid handle on things already :)
  • JBurnsJBurns Member
    edited March 6
    Brett,

    Thanks for the follow-up. I've finally gotten to try and test thing on my test site. The code above was giving an error in the admin page saying "item_count is not defined". I figured out that you can't just treat the foxy variables as any other variable. So in the function instead of using "item_count", I had to use "cart['_embedded']['fx:shipment']['item_count']". WIth that it all works well. I've changed the code some to do a sort of straight line based on Amazon's fulfillment rates. The final code is below on the off chance that someone else is trying to do something similar.
    rates.add(10001, 5.85, '', 'Standard (5-10 day)');
    rates.add(10002, 7.90, '', 'Priority (3-5 day)');
    rates.add(10003, 12.80, '', 'Express (1-3 day)');

    var srate_u10base = 5.05;
    var srate_u10mult = 0.8;
    var srate_a10base = 10;
    var srate_a10mult = 0.3;

    var prate_u10base = 7.1;
    var prate_u10mult = 0.8;
    var prate_a10base = 11;
    var prate_a10mult = 0.41;

    var erate_u10base = 9.92;
    var erate_u10mult = 2.88;
    var erate_a10base = 33.6;
    var erate_a10mult = 0.51;

    var standard_by_count = get_rate(srate_u10base, srate_u10mult, srate_a10base, srate_a10mult, cart['_embedded']['fx:shipment']['item_count']);
    var priority_by_count = get_rate(prate_u10base, prate_u10mult, prate_a10base, prate_a10mult, cart['_embedded']['fx:shipment']['item_count']);
    var express_by_count = get_rate(erate_u10base, erate_u10mult, erate_a10base, erate_a10mult, cart['_embedded']['fx:shipment']['item_count']);

    function get_rate(u10base, u10mult, a10base, a10mult, count) {
    var calc = 1.0;
    if (count < 11) {
    calc = u10base + u10mult * count;
    } else {
    calc = a10base + a10mult * count;
    }
    return Number.parseFloat(calc).toFixed(2);
    }

    if (cart['_embedded']['fx:shipment']['item_count'] > 1) {
    rates.filter(10001).price(standard_by_count);
    rates.filter(10002).price(priority_by_count);
    rates.filter(10003).price(express_by_count);
    }
  • brettbrett FoxyCart Team
    Thanks for the followup, @JBurns. I'm sorry I missed the `item_count`. I should have caught that, as I think another user recently asked about the same thing.

    It's really really cool for us to see our users do (and share) stuff like this. We're still pretty excited about the possibilities the custom shipping code functionality allows :)
Sign In or Register to comment.