Set FoxyCart to Ship Only to Continental 48 States

Written by on July 1, 2011 in jQuery - No comments
foxycart-ecommerce

I love FoxyCart.

It’s built for designers and developers, and is extremely easy to add to a website. It’s simple to style, and I’ve been having a lot of fun building it into a variety of websites for clients. If there is one negative I suppose you would say it’s lacking some features. One feature that I was surprised to not see was a simple way to set FoxyCart to ship to only the Continental US. I’m currently working on 2 websites for clients with FoxyCart built in, and they both want to ONLY ship to the 48 states.

One thing I can’t praise enough is the support at FoxyCart. They have a forum, and they actually keep up with reading and responding to the posts people make regarding different problems. I believe this support is what will help FoxyCart grow in the future.

So back to my problem. A standard FoxyCart install has an area for shipping info that looks like this:

foxycart checkout

By default, all the countries are listed for you to choose from:

foxycart checkout

As are the states:

We need to shut off the ability for the user to choose any country other than US, and any state other than the 48 states.

In order to do this you’ll need to update your checkout template in FoxyCart.

foxycart admin

Add the following code into the head of your FoxyCart checkout template (thanks FoxyCart admin!):

<script type="text/javascript" charset="utf-8">
jQuery(document).ready(function() {
 var usIndex = -1;
 for (var i = 0; i < FC.locations.config.locations.length; i++) {
 if (FC.locations.config.locations[i].cc2 == "US") {
 usIndex = i;
 }
 }
 var newregions = [];
 for (var i = 0; i < FC.locations.config.locations[usIndex].r.length; i++) {
 if (
 FC.locations.config.locations[usIndex].r[i].c != "HI" &&
 FC.locations.config.locations[usIndex].r[i].c != "AK" &&
 FC.locations.config.locations[usIndex].r[i].c != "MH" &&
 FC.locations.config.locations[usIndex].r[i].c != "MP" &&
 FC.locations.config.locations[usIndex].r[i].c != "PW" &&
 FC.locations.config.locations[usIndex].r[i].c != "PR" &&
 FC.locations.config.locations[usIndex].r[i].c != "VI" &&
 FC.locations.config.locations[usIndex].r[i].c != "AF" &&
 FC.locations.config.locations[usIndex].r[i].c != "AA" &&
 FC.locations.config.locations[usIndex].r[i].c != "AC" &&
 FC.locations.config.locations[usIndex].r[i].c != "AE" &&
 FC.locations.config.locations[usIndex].r[i].c != "AM" &&
 FC.locations.config.locations[usIndex].r[i].c != "AP" &&
 FC.locations.config.locations[usIndex].r[i].c != "AS" &&
 FC.locations.config.locations[usIndex].r[i].c != "GU" &&
 FC.locations.config.locations[usIndex].r[i].c != "DC" &&
 FC.locations.config.locations[usIndex].r[i].c != "FM"
 ) {
 newregions.push(FC.locations.config.locations[usIndex].r[i]);
 }
 }
 FC.locations.config.locations[usIndex].r = newregions;
 FC.checkout.setAutoComplete("customer_country");
 FC.checkout.setAutoComplete("customer_state");
 FC.checkout.setAutoComplete("shipping_country");
 FC.checkout.setAutoComplete("shipping_state");

 jQuery('#customer_country, #shipping_country').val('US');
 jQuery('#customer_country_name, #shipping_country_name').val('United States');
 jQuery('#customer_country_name, #shipping_country_name').attr('readonly', true).focus(function() {
 this.blur();
 });
});

</script>

After updating your template you’ll now see that only the 48 states are available. Also, there is no dropdown for the country, and no way to select or change the country text.

foxycart shopping cart

About the Author

Mike Ilsley "ilz" is a Boston based website designer, front-end developer, WordPress addict, owner of Beantown Design, and exclusive ThemeForest author. Check him out on Twitter, Facebook, Google+ and don't forget to subscribe to the WebDesign&Such email RSS feed. Also, check out his latest project, jQueryMobileShowcase.com.