Created
February 4, 2022 01:54
-
-
Save josephclaytonhansen/9aa812f10ee3d6654c284f809b85e3e7 to your computer and use it in GitHub Desktop.
WooCommerce change default customer checkout state (DO NOT USE)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const TO_NAME = 1; | |
const TO_ABBREVIATED = 2; | |
function convertRegion(input, to) { | |
var states = [ | |
['Alabama', 'AL'], | |
['Alaska', 'AK'], | |
['American Samoa', 'AS'], | |
['Arizona', 'AZ'], | |
['Arkansas', 'AR'], | |
['Armed Forces Americas', 'AA'], | |
['Armed Forces Europe', 'AE'], | |
['Armed Forces Pacific', 'AP'], | |
['California', 'CA'], | |
['Colorado', 'CO'], | |
['Connecticut', 'CT'], | |
['Delaware', 'DE'], | |
['District Of Columbia', 'DC'], | |
['Florida', 'FL'], | |
['Georgia', 'GA'], | |
['Guam', 'GU'], | |
['Hawaii', 'HI'], | |
['Idaho', 'ID'], | |
['Illinois', 'IL'], | |
['Indiana', 'IN'], | |
['Iowa', 'IA'], | |
['Kansas', 'KS'], | |
['Kentucky', 'KY'], | |
['Louisiana', 'LA'], | |
['Maine', 'ME'], | |
['Marshall Islands', 'MH'], | |
['Maryland', 'MD'], | |
['Massachusetts', 'MA'], | |
['Michigan', 'MI'], | |
['Minnesota', 'MN'], | |
['Mississippi', 'MS'], | |
['Missouri', 'MO'], | |
['Montana', 'MT'], | |
['Nebraska', 'NE'], | |
['Nevada', 'NV'], | |
['New Hampshire', 'NH'], | |
['New Jersey', 'NJ'], | |
['New Mexico', 'NM'], | |
['New York', 'NY'], | |
['North Carolina', 'NC'], | |
['North Dakota', 'ND'], | |
['Northern Mariana Islands', 'NP'], | |
['Ohio', 'OH'], | |
['Oklahoma', 'OK'], | |
['Oregon', 'OR'], | |
['Pennsylvania', 'PA'], | |
['Puerto Rico', 'PR'], | |
['Rhode Island', 'RI'], | |
['South Carolina', 'SC'], | |
['South Dakota', 'SD'], | |
['Tennessee', 'TN'], | |
['Texas', 'TX'], | |
['US Virgin Islands', 'VI'], | |
['Utah', 'UT'], | |
['Vermont', 'VT'], | |
['Virginia', 'VA'], | |
['Washington', 'WA'], | |
['West Virginia', 'WV'], | |
['Wisconsin', 'WI'], | |
['Wyoming', 'WY'], | |
]; | |
var provinces = [ | |
['Alberta', 'AB'], | |
['British Columbia', 'BC'], | |
['Manitoba', 'MB'], | |
['New Brunswick', 'NB'], | |
['Newfoundland', 'NF'], | |
['Northwest Territory', 'NT'], | |
['Nova Scotia', 'NS'], | |
['Nunavut', 'NU'], | |
['Ontario', 'ON'], | |
['Prince Edward Island', 'PE'], | |
['Quebec', 'QC'], | |
['Saskatchewan', 'SK'], | |
['Yukon', 'YT'], | |
]; | |
var regions = states.concat(provinces); | |
if (to == TO_ABBREVIATED) { | |
input = input.replace(/\w\S*/g, function (txt) { return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase(); }); | |
for (region of regions) { | |
if (region[0] == input) { | |
return (region[1]); | |
} | |
} | |
} else if (to == TO_NAME) { | |
input = input.toUpperCase(); | |
for (region of regions) { | |
if (region[1] == input) { | |
return (region[0]); | |
} | |
} | |
} | |
} | |
function changeState(state){ | |
document.getElementById('billing_state').value = state; | |
} | |
fetch('https://api.ipgeolocation.io/ipgeo?apiKey=096780dab7334fe78bb8ab933f1ed57e') | |
.then((response) => { | |
return response.json(); | |
}).then((data) => { | |
console.log(data); | |
let g = data["state_prov"]; | |
changeState(convertRegion(g, 2)); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment