The Foxy forums are on the move!

We're in the process of moving our forums over to a new system, and so these forums are now read-only.
If you have a question about your store in the meantime, please don't hesitate to reach out to us via email.

How to make shipping rates for product categories w/ flat shipping only available in US

Hey guys

I've got a new fun question :)

Most of our products ship with live shipping rates. We have one product category setup for free shipping (flat rate at $0.00). My question is how can I set this to only allow the free shipping within the US?

I'm already using and familiar with the live rates modification snippet. Am I correct in thinking the change would need to be handled as part of the live rates modification (so where category = free & country = US) rather than trying to do this using the category and then modifying the shipping options?

As we've already setup and are using the shipping categories, I'm hesitant to redo the process around it for this exception as it means changing how the client does things again and change management pains.

Any thoughts on the best approach?
Comments
  • fc_adamfc_adam FoxyCart Team
    @TiffaniH,

    Unfortunately right now the only way to achieve conditional shipping by country is with the snippets. The plan moving forward is to support this within the administration but that's still a little ways off.

    What you'd need to do is set all the categories to live rate shipping, and then using the snippets make adjustments if that category is present.

    How is it handled if the free shipping product is present with other non-free shipping products? Is it not counted within the shipping costs, or does it contribute to shipping costs in that instance?
  • thanks @fc_adam. I thought that would be the case.

    With non-free products, it simply wouldn't add to the shipping costs, so that item(s) would ship free, but anything else would be costed via the live rates.

    The product vary from fairly small to large, 11 lb items. We already use the snippets to change the live rates so that orders under 5lbs ship via USPS and heavier packages ship from UPS as these give the best rates for what the customer wants.
  • fc_adamfc_adam FoxyCart Team
    @TiffaniH,

    So the addition of wanting to exclude the free products from rate calculations when it's mixed with normal products adds an additional snippet you'll need to include: https://wiki.foxycart.com/v/2.0/snippets/breaking_large_shipments_into_multiple

    Specifically, that snippet is documented for taking an overweight cart and setting the weight to a smaller weight - and you'd then multiple the returned rates for the smaller box weight by the number of boxes that make up the larger weight.

    You don't need all of that - all you'd need to do is adjust the weight that is sent to the carriers if there is free ship products present with not-free products. It would look something like this:
    function freeShipProductHandler() {
    free_weight = 0;
    for (var p = 0; p < FC.json.items.length; p++) {
    if (FC.json.items[p].category == "freeship") {
    free_weight += FC.json.items[p].weight;
    }
    }
    if (FC.json.total_weight_shippable > free_weight) {
    FC.json.shipping_address.total_weight_shippable = FC.json.total_weight_shippable - free_weight;
    } else {
    FC.json.shipping_address.total_weight_shippable = null;
    }
    }

    FC.client.on('cart-shipping-options-update', freeShipProductHandler );
    FC.client.on('checkout-shipping-options-update', freeShipProductHandler );
    {% if cart_is_fullpage or context == 'checkout' %}FC.client.on("ready.done",freeShipProductHandler);{% else %}freeShipProductHandler();{% endif %}
    Note that that is untested code - but it should give you an idea of how it'd work. That loops through the products in the cart, and looks for any with a category of "freeship". If it finds them, it adds their collective weight to a variable. It then checks to see if the total weight of the cart is greater than the free shipping products collective weight. If it is - that means that there is a mixture and then sets the custom shipping weight of the total weight less the free product weight. If it's not - that means there is only free shipping products, then it nulls that custom value. This is so that a weight is still sent to the live rate endpoint so it can return actual weights and not an error.

    You'd handle that second scenario in the live rates shipping modification snippet - loop through the products and see if only free ship products are present. If they are - then hide all the returned rates and add in a free shipping rate. Otherwise run through the custom logic as normal.
Sign In or Register to comment.