Skip to content

Instantly share code, notes, and snippets.

@VizualAbstract
Created June 2, 2017 19:49
Show Gist options
  • Save VizualAbstract/e93e098e49935e619052d64a584f677d to your computer and use it in GitHub Desktop.
Save VizualAbstract/e93e098e49935e619052d64a584f677d to your computer and use it in GitHub Desktop.
function getProductForm(product_id) {
var forms = document.querySelectorAll('[action^="/cart/add"]');
if (!forms.length) {
alert('ReCharge cannot load: Product form not found.');
return false;
} else if (forms.length > 1) {
var product_form;
forms.forEach(function(form, i) {
if (form[i] && form[i].getAttribute('data-productid') !== null) {
if (form[i].getAttribute('data-productid') == product_id) {
productForm = form[i];
}
} else if (form[i] && form[i].getAttribute('data-product-id') !== null) {
if (form[i].getAttribute('data-product-id') == product_id) {
productForm = form[i];
}
} else {
alert('ReCharge cannot load: Product form not found for: ' + product_id);
productForm = false;
}
});
return productForm;
} else {
return forms[0];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment