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.

Coupon discount good in one State

Hello, I have two products each in separate categories. Both require flat rate shipping of $5 in California and $10 in all other states.

I wanted to set up a coupon to reduce the shipping by $5 if state field in the check out is California. ( excluding all the other US states for this discount.) Is there a way to do this?
Comments
  • fc_adamfc_adam FoxyCart Team
    @Pasquale,

    Good question. There are two ways you could approach this.

    Firstly, you could dynamically add a coupon to the cart if the customer is from California, also removing the coupon if the coupon is present but the state is changed to something else. The coupon wouldn't affect shipping technically though, but would just discount the cart total. In the end though, it would work out the same way (assuming the cart subtotal is greater than the coupon discount).

    Another option would be to switch to setting the shipping costs using a snippet - and then altering the returned rates based on the state value, including the discounted price within the shipping. You can see details on that here: https://wiki.foxycart.com/v/2.0/snippets/flat_rate_shipping_modification
  • @fc_adam thanks for the fast reply.

    Option two is more desireable. One caveat; this set up would only be for a couple of products in their own category and exclude all others.

    Would I be able to separate out the use of the snippet ( based on state value) by updating only the categories I require?
    Step 1: Update Categories
    Update all categories to 'Shipped using a flat rate fee' with a value of 0 in the 'Product Delivery Option' section.
  • fc_adamfc_adam FoxyCart Team
    @Pasquale,

    Unfortunately with the way the snippet works, unless you can guarantee that people will only ever purchase products from these specific categories on their own and never with your other categories, you'll need to handle all of your flat rate shipping costs within the snippet.
  • Just to be clear, if the other products are handled with live rates, this would exclude those from the flat rate shipping mod?
  • fc_adamfc_adam FoxyCart Team
    @Pasquale,

    If your normal products are charged with live rates, I'd recommend using the live rates snippet to add in your custom flat rates as needed. You can't mix live rates shipping with the flat rate snippet.
  • @fc-adam

    Thanks for the guidance, I will give this a whirl and might be back for a bit or two.
  • OK I think I ran into a wall... and might need to use option one dynamically adding the coupon when a particular state is selected.

    Now that I am in deep, I see what you mean about the obstacles. That said:
    ------------------------------
    I tried this with both the live rate mod set (with respective javascript) and the flat rate set (with respective javascript) to no avail.


    The two test products are set in a category by themselves with flat rate shipping selected.

    Using examples from the flat rate modification logic - With Javascript added into the footer... Here is what I put together, but it does not seem to be working. I am missing something for sure...

    Test products: http://www.martinranchwinery.com/holiday-deals

    /* BEGIN CUSTOM SHIPPING LOGIC */

    var hasMCSNWC = false;
    var hasMCSWC = false;
    for (p in fc_json.products) {
    switch (fc_json.products[p].category) {
    case "MCSNWC": // My category = MCSNWC
    hasMCSNWC = true;
    break;
    case "MCSWC": // My category = MCSWC
    hasMCSWC = true;
    break;
    }
    }

    var shippingCost = 0;
    if (hasMCSNWC) {
    shippingCost += 10; // My Category = MCSNWC = flat rate is $10
    } else if (hasMCSWC) { // My Category = MCSWC = flat rate is $10
    shippingCost += 10;
    }


    if (address.state == "CA") { // IF California is selected flat rate is $5
    FC.customFlatRates.add(1, 5, 'USPS');

    /* END CUSTOM SHIPPING LOGIC */
    };
  • fc_adamfc_adam FoxyCart Team
    edited September 2015
    @Pasquale,

    Two things I can see with that code - firstly, you're missing an end curly bracket after the California conditional. You also set up the two shippingCost variable - but you don't actually use it anywhere for a rate amount.

    I'm assuming that's meant to be the rate if they're not from California? If so, the updated code would look like this:
    /* BEGIN CUSTOM SHIPPING LOGIC */

    var hasMCSNWC = false;
    var hasMCSWC = false;
    for (p in fc_json.products) {
    switch (fc_json.products[p].category) {
    case "MCSNWC": // My category = MCSNWC
    hasMCSNWC = true;
    break;
    case "MCSWC": // My category = MCSWC
    hasMCSWC = true;
    break;
    }
    }

    var shippingCost = 0;
    if (hasMCSNWC) {
    shippingCost += 10; // My Category = MCSNWC = flat rate is $10
    } else if (hasMCSWC) { // My Category = MCSWC = flat rate is $10
    shippingCost += 10;
    }


    if (address.state == "CA") { // IF California is selected flat rate is $5
    shippingCost = 5;
    }

    FC.customFlatRates.add(1, shippingCost, 'USPS');
    /* END CUSTOM SHIPPING LOGIC */
    To reiterate though - if you also have live rate products - you need to use the live rate snippet. The flat rate snippet won't work well with live rate products mixed in.
  • @fc-adam
    Thank you for checking in.

    Bummer on the mixing live with flat rate (even with different category settings.) I'll need to revert to Live rate logic Mod.

    Yes, wanting to set each category with the shipping cost to $10.
    Then if California the cost is reduced to $5

    Better explained:
    ------------
    I have two categories:
    1- MCSNWC
    2- MCSWC

    Each with the same shipping options:
    1- Ship to California
    2- Outside California

    If added to the cart and the address field input is:
    California = Shipping = $5
    Outside California = Shipping = $10
    ------------
    Like you mentioned this gets complicated in the check out when additional products are added to the cart?

    I'll play with the updated code above with the live rate logic and see how things work.
  • fc_adamfc_adam FoxyCart Team
    @Pasquale,

    Yeah it does get complicated unfortunately.

    For example - as you're mixing live rates and flat rates - to allow the live rates snippet to work, you'd need to also set all of your categories to live rates. You wouldn't want the flat rate products to increase the live rate weight and possible the returned rates, so their weight would need to be taken off of the variable for that (we have a snippet for that). Finally - you'd also want to alter the weight conditionally to there being other actual live rate products in the cart. If it's just the "flat rate" products, then you wouldn't want to set the weight to 0, as then live rates wouldn't be returned but instead you'd see an error.

    All of that is totally possible - and you'd use the live rates snippet combined with this one: https://wiki.foxycart.com/v/2.0/snippets/breaking_large_shipments_into_multiple - which would show you how to alter the shipping weight amount.
  • @fc-adam Wo boy! I'll keep all of that in mind.

    ...Maybe it would be easier to write a logic that would set up a coupon to reduce the shipping by $5 if state field in the check out is California. ( excluding all the other US states for this discount.)
  • fc_adamfc_adam FoxyCart Team
    @Pasquale,

    In the end it would probably be a simpler approach - and would allow you to maintain the flat rate shipping costs on this products set within the administration.
  • @fc-adam, I have searched for a starter on the coupon good for one state. Have you seen anything here to get me going?

  • fc_adamfc_adam FoxyCart Team
    edited September 2015
    @Pasquale,

    This should hopefully get you started - update COUPONCODE to be the coupon you want to dynamically add and include this in the custom footer code configuration option.
    {% if context == "cart" or context == "checkout" %}
    <script>
    function toggleCoupon() {
    var ca_coupon_code = "COUPONCODE";
    var ca_coupon_id = 0;
    for (var code in FC.json.coupons) {
    if (code == ca_coupon_code) {
    var ca_coupon_id = FC.json.coupons[code].id;
    }
    }

    if (FC.json.shipping_address.region == "CA") {
    if (ca_coupon_id == 0) {
    // It doesn't have the coupon, add it
    FC.cart.addCoupon({coupon_code:ca_coupon_code});
    }
    } else {
    if (ca_coupon_id > 0) {
    // Remove the coupon, it shouldn't be here
    FC.cart.removeCoupon({coupon_code_id:ca_coupon_id});
    }
    }
    }

    FC.client.on("ready.done", toggleCoupon);
    FC.client.on("customer-address-change.done", toggleCoupon);
    </script>
    {% endif %}
  • @fcadam, Many thanks for the start!
Sign In or Register to comment.