Distance Based Shipping

I need to be able to calculate a shipping cost based on the distance between the Pickup/Dropoff address and the main shop. I am thinking - In the cart, when the user enters the shipping address: Geocoding the two addresses or asking a mapping provider, then where to put the code? And is there an existing snippet that's close?

Comments
  • klyleklyle Member
    https://wiki.foxycart.com/v/2.0/snippets/flat_rate_shipping_modification seemed like the best place to start...

    So, I have the two services, Courier In and Courier Back, in a category Transport so that I can apply the driving distance based shipping rate to them. I am expecting that the shipping costs won't be calculated until checkout, so I think I'll have to figure out how to zero the price of each Transport item once I do the calculation. For now, I am setting the Transport items to $1.00 as a reminder to the user that they added it to the cart, even if they later selected Self/Pickup-Dropoff for $0.

    Ok, with the snippet, I get the two currently hardcoded flat rate options, if I have a Transport product in the cart. If I remove the Transport product, the other products have No Shipping, so that option goes away...all good. Next, I am going to Multiply my flat rate by the number of Transport Items in the cart, then, finally, put in the logic from https://gist.github.com/levymetal/5083949 - Still open to good advice...
  • fc_adamfc_adam FoxyCart Team
    @klyle,

    It sounds like that approach could work well for you. I'm not sure I completely understand what you're describing in your middle paragraph, but it sounds like you're on the right track.

    In 2.0 - it is possible for a user to calculate shipping on the cart though, which if you use the snippet you linked to, that would allow that.

    For future-proofing your integration though, I'd actually recommend using this snippet instead: https://wiki.foxycart.com/v/2.0/snippets/custom_live_rate_endpoint. We have a feature coming soon which will be a native implementation of that snippet, and will be where we suggest people set up custom shipping functionality rather than with the snippets. If you use that custom endpoint snippet now, it will be easier to jump over to the native approach when that launches.

    The main difference between the snippet you linked to and the custom endpoint is that the shipping calculations will happen on an endpoint on your server - so you'd be calculating the custom rates there.
  • klyleklyle Member
    Thanks very much, @fc_adam.
  • klyleklyle Member
    @fc_adam
    In that middle paragraph, I am saying that I have isolated the two Transport items in that category, because those items will have a calculated "shipping" price based on distance, whereas none of the other items in the store require shipping (Category Default, NoShipping). In order to fire the snippet that I first picked, I have to have an item from a category that is not No Shipping, which is...Transport... Which, I think, explains why I believe that I need to have those $1.00 "stub" items in the cart. Does that make the mud any clearer? Am I mistaken?

    Regardless of the method, because this particular store offers both Courier In and Courier Back, it seems I still need the "stub" items so that I can count them and determine whether to charge the custom per-mile shipping charge in one direction or two (because the customer could choose, either, neither, or both).
  • fc_adamfc_adam FoxyCart Team
    @klyle,

    Thanks for clarifying that - I think I do better understand now.

    So it might be a little strange for your customers to have the two additional line items in the cart - but if it makes sense for your use case, then that could definitely work. You could add them as $0 products to begin with - which would simplify the need to zero them out afterwards, which you could only really do by removing the completely.

    If you didn't want to add them as line items - you could instead add them as hidden session variables by prepending the options with h:. They would then be available in the JSON as FC.json.custom_fields.OPTION_NAME.value, where OPTION_NAME is the name you give the value in the add to cart. That way you can track the customers selection, but you don't need to have a line item in the cart for it.
  • klyleklyle Member
    Very slick, Thanks!
Sign In or Register to comment.