Free Shipping if Specific Category Total Exceeds Amount

I've added code to offer customers free shipping if the order total is over $15. However, I want this to only apply to one category of products. So that if the total of products added to cart in that specific category exceeds $15 the free shipping applies.

I used the following code taken from here: https://wiki.foxycart.com/v/2.0/snippets/flat_rate_shipping_modification
FC.customFlatRates.add(1, 5, 'Standard', 'Delivery');
if (FC.json.total_item_price >= 15) {
FC.customFlatRates.update(1, 0);
}
How can I update that, to apply the discount logic to only a specific category?
Comments
  • fc_adamfc_adam FoxyCart Team
    @colewebdev,

    We can certainly help you get started with that. Quick clarification question before we start though - how are you calculating the shipping rates for the rest of the products in your store? That will dictate how to best handle conditionally adding free shipping.
  • 3 categories, each uses a flat rate (or free) shipping rate per product.
  • It's also worth noting that our existing code above, did not work. While the cart showed $0 for shipping, the customer was charged (shows on receipt)
  • fc_adamfc_adam FoxyCart Team
    @colewebdev,

    Thanks for clarifying. With how the checkout flow works - the shipping can't ever be lower than what the server expects it to be. So for example, if the categories are set to be a flat rate shipping of $15, and you try to alter that to $0 on the checkout - while it will look like it's worked, server-side it'll still be $15 when completing the transaction.

    For what you're wanting to do - I'd recommend making use of our new custom shipping endpoint feature. It's replacing the flat rate snippet for creating custom shipping setups. You can see information on it at https://wiki.foxycart.com/v/2.0/shipping#custom_shipping_endpoint

    You could either just use this approach for the one category you need dynamic shipping rates for, or switch all of your categories to calculate by this custom endpoint, and calculate all of the shipping charges for your store from the endpoint. If you need some help getting started with that, just let us know!
  • We addressed the problem, by using a default category fee of $0 - which after placing few tests seems to work alright.

    The Shipping Endpoint is completely new to us ...

    Example 1 looks a lot like the code we just implemented, are you saying this will work better with the server and not be overwritten? https://github.com/Foxy/foxy-shipping-endpoint-php#user-content-code-examples
  • fc_adamfc_adam FoxyCart Team
    @colewebdev,

    The two helper scripts in PHP and Node.JS are based off of how the flat rate snippet worked - so it's very similar in execution for setting it up. The big benefit is that it is a native feature rather than what was essentially a hack of the checkout. This does make it more stable and reliable.
  • Thanks, I'll investigate further.
Sign In or Register to comment.