How to create two-tiered flat-rate shipping options depending on number of items in cart

mattvarumattvaru Member
Hi there,

I'm super new to Foxy, so forgive me if this is a rookie question.

I've got two categories in my store: wine and merchandise. Merchandise is charged at a flat rate of $5/shipment, which I was successfully able to set up in my admin settings.

However, I want to create a two-tier flat-rate system for wine: if a user adds one product from the wine category to the cart, they are assessed a $10 flat-rate shipping fee. However, if they buy more than product in the wine category (say, 3 bottles of wine), then they only are assessed a $5 flat-rate shipping fee.

I'm willing to bet this is something that I would use snippets for, but I'm not really sure where/how to get started. Any help is greatly, greatly appreciated. :-)
Comments
  • fc_adamfc_adam FoxyCart Team
    @mattvaru,

    Welcome! We're happy to help!

    What you're looking to achieve is definitely possible, and we'd recommend using our custom shipping code functionality which you can see details on here: https://wiki.foxycart.com/v/2.0/shipping/custom_code

    We can help you with what the custom code will look like - but I wanted to confirm one thing with you first. You mentioned that the merchandise is charged a flat $5 per shipment fee. If someone buys both wine and merchandise - are the shipping fees just combined? So if it's 1 bottle of wine and 1 piece of merchandise, it'd be $15 shipping? Or should one shipping fee take precedence over the other?

    If you can confirm that for us, we can get you some details on how it'd be set up for your store.
  • mattvarumattvaru Member
    edited June 3
    That's a great question! Thanks for getting back to me so quickly.

    So, the following is the desired workflow:

    - If a user just orders 1 bottle of wine, the shipping total is $10.
    - If a user just orders 2 bottles or more of wine, the shipping total is $5.
    - If a user just orders 1 merch item or more, the shipping total is $5.
    - If a user orders 1 bottle of wine and 1 item (or more) from merch, the shipping total is $15.
    - If a user orders 2 bottles of wine (or more) and 1 item (or more) from merch, the shipping total is $10. ($5 for wine, $5 for merch.)

    - Even if a user orders 20 bottles of wine and 30 items from merch, their shipping total will still be $10.

    Hope that helps! I've got a little snippet of code that I've been working on, derived from your answer here. Been studying over the shipping payload to see how to access the data I need, but if you can get me started or help in some way, I would be forever grateful!
  • mattvarumattvaru Member
    @fc_adam — any chance I could get some assistance on this or get pointed in this right direction? Thanks again!
  • fc_adamfc_adam FoxyCart Team
    @mattvaru,

    Thanks for listing out all of the combinations - that's super helpful!

    For what you've described, the following custom shipping code should achieve it:
    let wine = 0;
    let merchandise = 0;

    for (let p in cart['_embedded']['fx:items']) {
    let item = cart['_embedded']['fx:items'][p];
    switch (item['_embedded']['fx:item_category']['code']) {
    case "Wine":
    wine += item['quantity'];
    break;
    case "Merchandise":
    merchandise += item['quantity'];
    break;
    }
    }

    let shipping = 0;

    if (wine == 1) {
    shipping += 10;
    } else if (wine > 1) {
    shipping += 5;
    }

    if (merchandise > 0) {
    shipping += 5;
    }

    if (shipping > 0) {
    rates.add(10000, shipping, '', 'Standard Shipping');
    }

    Reviewing your store's existing configuration - I see both of your categories are already set to live rate shipping as the delivery method too - so you should be fine to update your custom shipping code with the above and then test it out with products in your cart and entering a shipping address.
  • mattvarumattvaru Member
    You are the man!! Works like a freaking gem. I really appreciate it - came in clutch as we're looking to launch the store in the next couple of days.

    Thanks so much!
  • TLCGCTLCGC Member
    mattvaru always asks the best questions!
Sign In or Register to comment.