Skip to content

Instantly share code, notes, and snippets.

@BigWillie
Last active February 18, 2016 21:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BigWillie/caa2c3d9aa3884cbd2a9 to your computer and use it in GitHub Desktop.
Save BigWillie/caa2c3d9aa3884cbd2a9 to your computer and use it in GitHub Desktop.
<script src="https://code.jquery.com/jquery-1.12.0.min.js"></script>
<script>
var getQueryVariable = function(variable)
{
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i=0;i<vars.length;i++) {
var pair = vars[i].split("=");
if(pair[0] == variable){return pair[1];}
}
return(false);
}
var addItemToCart = function (variant_id, quantity, shipping_interval_frequency, shipping_interval_unit_type, subscription_id) {
data = {
"quantity": quantity,
"id": variant_id,
"properties[shipping_interval_frequency]": shipping_interval_frequency,
"properties[shipping_interval_unit_type]": shipping_interval_unit_type,
"properties[subscription_id]": subscription_id
}
console.log('clicked',data);
jQuery.ajax({
type: 'POST',
url: '/cart/add.js',
data: data,
dataType: 'json',
success: function() {
window.location.href = '/cart';
}
});
}
// http://www.example.com/index.php?id=1&image=awesome.jpg
var product_id = getQueryVariable("id"); // where id is the query param
if (product_id.length) {
addItemToCart(product_id, 1, "1", "Months", "12599");
} else {
window.history.go(-1) // should redirect back to old page if product id is wrong
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment