Restricting State/Region in 0.7.2

sparkwebsparkweb Member, Integration Developer, FoxyShop, Order Desk
in Help edited November 2011
I'm running into an issue where some of my "states to remove" are being removed but some are not. Specifically, I can't get Hawaii and some of the armed force networks to go away. Site is running 0.7.2.

Luke said that there's some new code needed for 0.7.2 - Adam do you have some recommendations?

Site: http://extravagonzofoods.com/
This is a multiship store.

Current Code:
function stateIsValid(state) {
var statesToRemove = new Array("MP","AK","PR","AA","AS","AR","AF","AC","AE","AM","AP","FM","GU","MH","HI");
for (var i = 0; i < statesToRemove.length; i++) {
if (statesToRemove == state) {
return false;
}
}

var newregions = [];
for (var i = 0; i < FC.locations.config.locations[usIndex].r.length; i++) {
if (stateIsValid(FC.locations.config.locations[ usIndex ].r.c)) {
newregions[newregions.length] = FC.locations.config.locations[usIndex].r;
}
}

FC.locations.config.locations[usIndex].r = newregions;
//console.log(FC.locations.config.locations[usIndex].r);
FC.checkout.setAutoComplete("customer_country");
FC.checkout.setAutoComplete("customer_state");
FC.checkout.setAutoComplete("shipping_country");
FC.checkout.setAutoComplete("shipping_state");
Comments
  • fc_adamfc_adam FoxyCart Team
    Hey David,

    The code you posted in the thread isn't what you're using on that page - have you tried what you posted?
  • sparkwebsparkweb Member, Integration Developer, FoxyShop, Order Desk
    edited December 2011
    Hi Adam,

    So I just jumped back into this and after troubleshooting it further I'm getting some strange results that are really baffling. This is the code I'm using:
    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 statesToRemove = new Array("MP","PR","AK","AS","AR","AA","AF","AC","AE","AM","AP","FM","GU","MH","HI");
    var newregions = FC.locations.config.locations[usIndex].r;
    console.log("Total Count: " + FC.locations.config.locations[usIndex].r.length);
    for (var i = 0; i < FC.locations.config.locations[usIndex].r.length; i++) {
    	console.log("#" + i + ": " + FC.locations.config.locations[usIndex].r[ i ].c + ' ' + $.inArray(FC.locations.config.locations[usIndex].r[ i ].c, statesToRemove));
    	if ($.inArray(FC.locations.config.locations[usIndex].r[ i ].c, statesToRemove) >= 0) {
    		newregions.splice(i,1);
    	}
    }
    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");
    
    
    (For anyone else looking, the forum screws with the brackets so be cautious if copying and pasting)

    This is what's being returned in my log:

    Total Count: 64
    #0: AL -1
    #1: AK 2
    #2: AZ -1
    #3: AR 4
    #4: AA 5
    #5: AE 8
    #6: AP 10
    #7: CO -1
    #8: CT -1
    #9: DE -1
    #10: DC -1
    #11: FM 11
    #12: GA -1
    #13: GU 12
    #14: ID -1
    #15: IL -1
    #16: IN -1
    #17: IA -1
    #18: KS -1
    #19: KY -1
    #20: LA -1
    #21: ME -1
    #22: MH 13
    #23: MA -1
    #24: MI -1
    #25: MN -1
    #26: MS -1
    #27: MO -1
    #28: MT -1
    #29: NE -1
    #30: NV -1
    #31: NH -1
    #32: NJ -1
    #33: NM -1
    #34: NY -1
    #35: NC -1
    #36: ND -1
    #37: MP 0
    #38: OK -1
    #39: OR -1
    #40: PA -1
    #41: PR 1
    #42: SC -1
    #43: SD -1
    #44: TN -1
    #45: TX -1
    #46: UT -1
    #47: VT -1
    #48: VI -1
    #49: VA -1
    #50: WA -1
    #51: WV -1
    #52: WI -1
    #53: WY -1

    So you can see it's not returning everything. After a lot of poking around I found that if I commented up the splice it would return all values. And I think that it's not checking on Hawaii because the splice is somehow making it skip the next value after Guam. Plus, the newregion indexes won't be correct anymore since the splice changes the newregion array right away. So I think maybe it needs to loop through newregion every time it wants to remove one OR not add to new region unless the inArray check return -1.

    Hope that all makes some sense. I've just blown a couple hours fighting with this and I understand a little more what's going on but am still really confused.
  • fc_adamfc_adam FoxyCart Team
    @sparkweb,

    Instead of this:
    for (var i = 0; i < FC.locations.config.locations[usIndex].r.length; i++) {
    
    try this:
    for (var i = FC.locations.config.locations[usIndex].r.length-1; i > -1; i--) {
    
  • sparkwebsparkweb Member, Integration Developer, FoxyShop, Order Desk
    That did the trick Adam! Great idea.... didn't even think of that.
Sign In or Register to comment.