Shipping Charge for Destination State Only

RevConceptRevConcept Member
in Bugs & Feature Requests edited February 2012
I altered some code I found on the snippets page. I need free shipping to all US States with the exception of AK and HI which are a flat rate of $35. I'm not too good with JavaScript, so I don't know what I need to do to fix this...

PROBLEM: Shipping charge is being added when the billing state is AK or HI...good, but shipping is not being removed when the shipping state is anything else. Also, shipping is not being added when billing state is one of the other 48 states...again, good...but then if the shipping state is AK or HI shipping is still not added.

Here is a link to a product page: http://kpmaclane.com.s118336.gridserver.com/shop/kp-maclane-womens-polo-shirt/

Here is the altered code I'm using:

function updateMyShippingCost() {
 
// Create variable for total shipping cost
var totalShippingCost = 0;
 
// Create an array for US States that have the same shipping cost
var unitedStates = new Array("AL", "AZ", "AR", "CA", "CO", "CT", "DE", "DC", "FL", "GA", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ", "NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC", "SD", "TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI", "WY" );
 
// Set default shipping cost per category
var shippingCost = {
DEFAULT: 0
};
 
// Choose the state to ship to
var state_code = (jQuery("#use_different_addresses").is(":checked") ? $("#shipping_state").val() : $("#customer_state").val());
 
// Set shipping costs for each category based on state
switch(state_code) {
case "HI":
shippingCost = {
DEFAULT: 35
};
break;
case "AK":
shippingCost = {
DEFAULT: 35
};
break;
default:
// Set shipping cost for the States defined earlier
if (jQuery.inArray(state_code, unitedStates) > -1) {
shippingCost = {
DEFAULT: 0
};
}
}
 
// Check each category and calculate shipping cost
jQuery.each(fc_json.products, function(index, options) {
 
// Calculate the total shipping cost for this category based on the quantity
var shipping = options.quantity * shippingCost[options.category];
 
// Add the total shipping cost of this category to the grand total shipping cost
totalShippingCost += shipping;
 
});
 
// Set the final shipping cost
FC.checkout.config.orderFlatRateShipping = totalShippingCost;
 
// Return the shipping cost for initially selected country
return totalShippingCost;
}
 
jQuery(document).ready(function() {
 
// Set shipping cost for the initially selected country
FC.checkout.config.orderFlatRateShipping = updateMyShippingCost();
FC.checkout.updateShipping(-1);
 
// Update shipping cost based on categories and country when updating the price
FC.checkout.overload("updatePriceDisplay", "updateMyShippingCost", null);
 
});
Comments
  • lukeluke FoxyCart Team
    Thanks for posting details, RevConcept. We'll have @fc_adam take a look.
  • fc_adamfc_adam FoxyCart Team
    edited February 2012
    @RevConcept,

    I'd actually suggest switching over to the following code instead of what you're using: http://wiki.foxycart.com/snippets/shipping/tiered_flat_rate_shipping

    Follow through the instructions given in that, setting the onLocationChange config option to true and use the following as your custom shipping logic:
    var state_code = (jQuery("#use_different_addresses").is(":checked") ? jQuery("#shipping_state").val() : jQuery("#customer_state").val());
    switch (state_code) {
      case "AK":
      case "HI":
        shippingCost = 35;
        break;
      default:
        shippingCost = 0;
    }
    

    Also, take a look at the following page, and grab the updated code for 'Serving the US only', which updates the states field correctly to US based states: http://wiki.foxycart.com/snippets/shipping/restricting_countries_from_the_checkout_form
  • I knew it was probably overkill ;) Thanks so much...this works PERFECTLY!
  • @fc_adam or @luke,

    What is the ID or Class associated with the state drop down? My font color is showing up white on white so I can't see anything.
  • fc_adamfc_adam FoxyCart Team
    edited March 2012
    The results shown below the location dropdowns have a class of "ac_results". Target that and you should be good to go. Note that it is structured like this:
    <div class="ac_results">
      <ul>
        <li>Name</li>
        ...
      </ul>
    </div>
    
  • Once again, many thanks.
Sign In or Register to comment.