Skip to content

Instantly share code, notes, and snippets.

@schrobby
Last active August 2, 2017 02:48
Show Gist options
  • Save schrobby/78b5f2d64922fd59f101 to your computer and use it in GitHub Desktop.
Save schrobby/78b5f2d64922fd59f101 to your computer and use it in GitHub Desktop.
Works on kithnyc.com. Not sure about any other Shopify site.
(function() {
var size = 'YOUR SHOE SIZE HERE';
var subdomain = 'shop.',
path = '/cart/%ID%:%QUANTITY%';
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
var parsed = JSON.parse(xhr.responseText);
var found = parsed.product.variants.filter(function(o) {
return o.option1 == size;
});
found = found ? found[0] : null;
if (!found) {
alert('Your size is not available. \
Don\'t forget to replace it in the beginning of the script.');
return false;
}
var replacements = {};
replacements['%ID%'] = found.id;
replacements['%QUANTITY%'] = 1;
var url = subdomain + document.location.hostname + path;
url = url.replace(/%\w+%/g, function(match) {
return replacements[match] || match;
});
document.location = document.location.protocol + '//' + url;
}
}
xhr.open('GET', document.location.href + '.json');
xhr.send();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment