Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save PiotrKrzyzek/45bfda726fe489849a43586039f2b395 to your computer and use it in GitHub Desktop.
Save PiotrKrzyzek/45bfda726fe489849a43586039f2b395 to your computer and use it in GitHub Desktop.
Cartflows tweak: select a product (or variation) on a cartflows checkout page by passing in a URL parameter (GET)
// Setting up and splitting up the parameters passed to this url
var QueryString = (function (paramsArr) {
let params = {};
for (let i = 0; i < paramsArr.length; i++) {
let param = paramsArr[i].split("=", 2);
if (param.length !== 2) continue;
params[param[0]] = decodeURIComponent(param[1].replace(/\+/g, " "));
}
return params;
})(window.location.search.substr(1).split("&"));
// CartFlows' checkout page has unique IDs per product/variation so we can select
// them. It uses it's own full id, but it does have the post id in there so the
// unique HTML ID looks something like this: wcf-item-product-123456
// So we thus then select "#wcf-item-product-123456" with jQuery and CLICK it
// We cannot set the checked property to true, as that does not actually then
// Change the items in the CF 'cart' on the checkout
// We also have a 500ms (half a second) delay to allow the page items to load a
// bit before we run this so that it'll actually work
// If we detect a 'product' parameter, use that to select that product
if (QueryString["product"] && QueryString["product"] != ""){
setTimeout(() => {
jQuery("#" + QueryString["product"]).click();
}, 500);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment