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.

Free shipping for one state only

PasqualePasquale Member
in Help edited November 2010
I have updated to 7.0 so far so great! I am still learning the Javascript language so I don't full grok yet. I would like to use a coupon code but it leaves too much to chance as its directed to one certain state only.

I have a free shipping in California (only) hack that is no longer working. It is also directed towards a specific category (HOLIDAY_GIFTS).

How it should work:
When someone selects the product in category HOLIDAY_GIFTS and types California in the state address field - Shipping = 0

I have been searching and learning to wrap my head around what might be missing. If anyone has a chance to look this over for me I would greatly appreciate it, and I will learn a bit more.

var myCustomShipping = function() {

var product_category = FC_json.product_category;
var shipping_cost = FC.checkout.config.originalShipping;

var shipping_state = jQuery('#shipping_state_name').val();
var customer_state = jQuery('#customer_state_name').val();
var state_lookup = customer_state;

if (jQuery('#use_different_addresses').is(':checked'))
{
state_lookup = shipping_state;
}
if(product_category = HOLIDAY_GIFT)
{
if(state_lookup === 'California')
{
// Then free shipping to California
shipping_cost = 0;
}
FC.checkout.config.orderFlatRateShipping = shipping_cost;
}

jQuery(document).ready(function() {
FC.checkout.config.originalShipping = FC.checkout.config.orderShipping;
FC.checkout.overload('updatePriceDisplay', 'myCustomShipping', null);
FC.checkout.updateShipping(-1);
});
Comments
  • lukeluke FoxyCart Team
    edited November 2010
    Hey Pasquale. It looks like you're really close. There are a couple things you have to fix though:
    var product_category = FC_json.product_category;
    

    That won't work because the variable is "fc_json" and the products there is an array you'll have to loop through. Are you assuming if they buy ANY product in the gifts category they get free shipping or is it if they ONLY buy products in the gift category? I'll assume the first one. Also if "HOLIDAY_GIFTS" is the category code, you'll have to wrap that value in quotes to compare it against a variable. Something like this should hopefully get you going:
    containsHolidayProducts = false;
    for (var i=0; i<fc_json.products.length; i++) {
        if (fc_json.products[ i ].category == "HOLIDAY_GIFTS") {
            containsHolidayProducts = true;
        }
    }
    var state_code = (jQuery("#use_different_addresses").is(":checked") ? $("#shipping_state").val() : $("#customer_state").val());
    if (state_code == "CA" && containsHolidayProducts) {
        shipping_cost = 0;
    }
    

    If you're not already using FireBug, make sure you give that a shot because you can test stuff write in the browser and make sure it works. It will also give you line-debugging and other tools that make JavaScript development much easier.

    Please let us know if that works for you or if you need further assistance.
  • Thanks so much Luke - I'll give that a spin and let you know how things are working... Thanks for the Firebug tip

    Really appreciate the help.
  • I dropped that into the check out template, selected a product in the category of HOLIDAY_GIFTS - filled out the address fields and was not able to get the shipping to Zero.

    I was not sure if the spelling of the word "holiday" -> containsHoldiayProducts = false; was on purpose ( I tried it that way and correct with no luck)

    Any additional help would be greatly appreciated - I am still too new at this, yet I almost grok it. I just need to have a bit of success to see the difference.

    here is the store - https://martinranchwinery.foxycart.com
  • lukeluke FoxyCart Team
    Doh't. Sorry about that... I didn't get around to testing this last night and I made a pretty stupid obvious mistake. The for loop should be <, not >. Here's the updated code (I'll edit my post as well). As for the "Holdiay"... that's... um... my special word for it. Or... I didn't get enough sleep and I can't spell worth anything. :)
    var myCustomShipping = function() {
    	var shipping_cost = FC.checkout.config.originalShipping;
    	containsHolidayProducts = false;
    	for (var i=0; i<fc_json.products.length; i++) {
    		if (fc_json.products[ i ].category == "HOLIDAY_GIFTS") {
    			containsHolidayProducts = true;
    		}
    	}
    	var state_code = (jQuery("#use_different_addresses").is(":checked") ? $("#shipping_state").val() : $("#customer_state").val());
    	if (state_code == "CA" && containsHolidayProducts) {
    		shipping_cost = 0;
    	}
    	FC.checkout.config.orderFlatRateShipping = shipping_cost;
    }
    
    jQuery(document).ready(function() {
    	FC.checkout.config.originalShipping = FC.checkout.config.orderShipping;
    	FC.checkout.overload('updatePriceDisplay', 'myCustomShipping', null);
    	FC.checkout.updateShipping(-1);
    });
    
  • Thanks for the continued support and help. Still no joy on this and I cant tell why... One correction that I did make was my category is HOLIDAY_GIFT - I fixed that but it made no difference.
  • fc_adamfc_adam FoxyCart Team
    @Pasquale, remove the spaces around the "i" in this line:
    if (fc_json.products[ i ].category == "HOLIDAY_GIFTS") {
    
    and you should be good to go
  • BINGO! - it works! You guys are geniuses... I learned a bunch here and really appreciate your help on this.
  • brettbrett FoxyCart Team
    Bummers that bbCode doesn't play well with javascript like that. Glad you got it working though!
Sign In or Register to comment.