Skip to content

Instantly share code, notes, and snippets.

@foosantos
Last active August 28, 2021 23:00
Show Gist options
  • Save foosantos/4d853ac2d4388441e63c506eefaf20ad to your computer and use it in GitHub Desktop.
Save foosantos/4d853ac2d4388441e63c506eefaf20ad to your computer and use it in GitHub Desktop.
Test WooCommerce Checkout
// ==UserScript==
// @name Auto fill values for WooCommerce checkout
// @namespace
// @version 0.1
// @description Adds a Tampermonkey script.
// @author felipelousantos
// @updateURL
// @grant GM_registerMenuCommand
// @match http*://*/*
// @noframes
// ==/UserScript==
const presets = {
"United States - Texas": {
billing_first_name: "John",
billing_last_name: "Doe",
billing_company: "Lorem Ipsum LLC",
billing_country: "US",
billing_state: "TX",
billing_city: "Dallas",
billing_postcode: "75240",
billing_address_1: "13350 Dallas Parkway",
billing_address_2: "",
billing_email: "woologin@woocommerce.com",
billing_phone: "+1 (972) 942-7210",
},
"Ireland - Dublin": {
billing_first_name: "John",
billing_last_name: "Doe",
billing_company: "Lorem Ipsum Ireland Limited",
billing_country: "IE",
billing_state: "D",
billing_city: "Leinster",
billing_postcode: "D02 AY86",
billing_address_1: "25 Herbert Pl",
billing_address_2: "",
billing_email: "woologin@woocommerce.com",
billing_phone: "+353 1 269 3224",
woocommerce_eu_vat_number: "IE6556973V",
},
"Brazil - Rio de Janeiro": {
billing_first_name: "João",
billing_last_name: "Silva",
billing_company: "Lorem Ipsum Ltda",
billing_persontype: "2",
billing_ie: "77.568.59-0",
billing_cnpj: "00.623.904/0001-73",
billing_country: "BR",
billing_state: "RJ",
billing_birthdate: "01/01/1990",
billing_city: "Rio de Janeiro",
billing_postcode: "22640-102",
billing_address_1: "Av. das Américas, 4430",
billing_address_2: "",
billing_neighborhood: "Barra da Tijuca",
billing_email: "woologin@woocommerce.com",
billing_phone: "+55 21 4003-3001",
},
};
const compatibleFields = [
"billing_first_name",
"billing_last_name",
"billing_company",
"billing_persontype",
"billing_ie",
"billing_cnpj",
"billing_country",
"billing_state",
"billing_birthdate",
"billing_city",
"billing_postcode",
"billing_address_1",
"billing_address_2",
"billing_neighborhood",
"billing_email",
"billing_phone",
"woocommerce_eu_vat_number",
"billing_number",
];
const selectWooFields = [
"billing_country",
"billing_state",
"billing_sex",
"billing_persontype",
];
function autoFillWooCommerceCheckout(option) {
for (const field of compatibleFields) {
if ( document.getElementById(`${field}`) != null ) {
if (selectWooFields.includes(`${field}`)) {
jQuery(`#${field}`).val("");
jQuery(`#${field}`).trigger('change');
} else {
document.getElementById(`${field}`).value="";
}
}
}
// Compatibilty with "woocommerce-extra-checkout-fields-for-brazil"
if ( `${presets[option].billing_country}` == "BR" && presets[option].compatible_woocommerce_extra_checkout_fields_for_brazil != true && document.getElementById("billing_number") != null ) {
const fullAddress = `${presets[option].billing_address_1}`;
const splitAddress = fullAddress.split(", ");
presets[option].billing_address_1 = splitAddress[0];
presets[option].billing_number = splitAddress[1];
presets[option].compatible_woocommerce_extra_checkout_fields_for_brazil = true;
}
for (const preset in presets[option] ) {
if ( document.getElementById(`${preset}`) != null ) {
if (selectWooFields.includes(`${preset}`)) {
jQuery(`#${preset}`).val(`${presets[option][preset]}`);
jQuery(`#${preset}`).trigger('change');
} else {
document.getElementById(`${preset}`).value=`${presets[option][preset]}`;
}
}
}
}
for (const preset in presets ) {
GM_registerMenuCommand(`${preset}`, () => autoFillWooCommerceCheckout(`${preset}`));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment