Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Force a default value for the shipping methods in shopify checkout to be selected on page load.
var preferedShipping = "EMS"; // This has to be the exact name of the shipping method you want to be preselected
function waitForFetch(tries, callback) {
if (tries > 40000) {return} // stop waiting after ~ 40 seconds
if (document.querySelector(".blank-slate")) {
setTimeout(function() { waitForFetch(tries + 1, callback) }, 10);
} else {
callback();
}
}
function checkRadioButton() {
var els = document.querySelectorAll(".section--shipping-method .content-box__row .radio-wrapper"),
radioButtonSelector = ".radio__input .input-radio";
for (var i = 0; i < els.length; i++) {
var el = els[i],
radioButton = el.querySelector(radioButtonSelector);
if (el.querySelector(".radio__label__primary").textContent.trim() === preferedShipping && !radioButton.checked) {
radioButton.checked = true;
} else if(radioButton.checked) {
radioButton.checked = false;
}
}
}
(function(fn){var d=document;(d.readyState=='loading')?d.addEventListener('DOMContentLoaded',fn):fn();})(function(){
if (document.querySelector(".blank-slate")) {
waitForFetch(1, checkRadioButton);
} else {
checkRadioButton();
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.