Skip to content

Instantly share code, notes, and snippets.

@jacobdubail
Created November 4, 2019 23:28
Show Gist options
  • Save jacobdubail/8c9157002c2793f9561faa509459ac96 to your computer and use it in GitHub Desktop.
Save jacobdubail/8c9157002c2793f9561faa509459ac96 to your computer and use it in GitHub Desktop.
add service plan at checkout
<script>
function checkServices() {
var physical = "Physical_Products",
services = "Subscriptions",
//services_index = [],
//physical_index = [],
basic_service = "305-2000-01",
deluxe_service = "305-2000-02",
addon_service = "305-2000-03",
basic_item_id = 0,
basic_qty = 0,
beacon_index = null,
deluxe_item_id = 0,
deluxe_qty = 0,
addon_item_id = 0,
addon_qty = 0,
connect = "301-3000-01",
beacons = "301-3000-01",
connect_item_id = 0,
connect_qty = 0,
beacon_item_id = 0,
beacon_qty = 0,
basic_subscription = "fcsid=fp8qeanmhq8895gh8nm6cn3d72&price%7C%7C6fc9f957c32cae8e798f371dc17d8f83d32d408653d13106d82a915761280038=29.97&x%3Aoriginalprice=29.97&x%3Al18n=%24%7C.%7C%2C%7C1%7C0&image%7C%7Cecfd1f18f69e5de947ad27c17dcb11ef7b62de6fa72c86f2136df6eced421d32%7C%7Copen=https%3A%2F%2Fstaging.theoracare.com%2Fwp-content%2Fplugins%2Ffoxyshop%2Fimages%2Fno-photo.png&url%7C%7C40534a72c2ad296c11c7d64625c7395437e148b5d8ca0bb32018cc22c7c912cf=https%3A%2F%2Fstaging.theoracare.com%2Fproducts%2Ftheora-service-monthly-recurring%2F&quantity_min%7C%7C9d1019d4bcedd42385539e841a647d1b1e82927150751f36909a8e29f72c329c%7C%7Copen=0&quantity_max%7C%7C10c83226e193b994442f0863a19a52838a0fcd6b2cfccb3919453cbf9b0ce161%7C%7Copen=0&x%3Aquantity_max=0&sub_frequency%7C%7C06c69bc7ea96d051c8c88b0ffe55bd5aa9747155a501ecc81c95c1aa5cc0ab35%7C%7Copen=1m&name%7C%7C9bc1351c27aefc3e95a07943d062d94687c4df11abd6ac45391fa783c3989783=Theora+Service+(monthly+recurring)&code%7C%7Ccb54afd5055b11fb862d320120ee39e60d3113ebd74e4f447fbd77ab71e08884=305-2000-01&category%7C%7C802fe1849a99b6efce20d27c1b56ecf0ee3022179ed8f32fd0e881ba8aef3764=Subscriptions",
deluxe_subscription = "fcsid=fp8qeanmhq8895gh8nm6cn3d72&price%7C%7Caea3974dfdc166c1c6a965cdb14648dae925ac9d17ec709ad5ac540630969f65=34.97&x%3Aoriginalprice=34.97&x%3Al18n=%24%7C.%7C%2C%7C1%7C0&image%7C%7C0556ce493b66b02a4269e53505d26015520ac267eaab3234048463f078615498%7C%7Copen=https%3A%2F%2Fstaging.theoracare.com%2Fwp-content%2Fplugins%2Ffoxyshop%2Fimages%2Fno-photo.png&url%7C%7C1fab094af2aa5dd4d88fa214c4eab588ffdac044e4308bddc4c9192be446350c=https%3A%2F%2Fstaging.theoracare.com%2Fproducts%2Fdeluxe-upgrade-theora-care-subscription-upgrade%2F&quantity_min%7C%7Cbe90dbcd9a01e3b495ab535d328d364488073b60ae00828610bff69a4d7e9e56%7C%7Copen=0&quantity_max%7C%7C3c215d278679e124725ef18ecfdcf990bc264e0e2bf84cfcd225d950b8bda73f%7C%7Copen=0&x%3Aquantity_max=0&sub_frequency%7C%7C64fa05c5ca97984a2055fda9b501e02a3889567bb5fd31a15feba081d91c4b38%7C%7Copen=1m&name%7C%7C6ac537b6fe1b4ea75811a2d802965807683f367630fe5e1c936513976356fba7=Deluxe+Theora+Care+Subscription&code%7C%7C1c6d389bfa239c0d2de576b4ecdde5609726fa5e46e5bd2c87cf9581762ed899=305-2000-02&category%7C%7C6a2f7779269545cdfb13c68ab3b6ecd0f650add432df442b3fabdf8182ca062d=Subscriptions",
addon_subscription = "fcsid=fp8qeanmhq8895gh8nm6cn3d72&price%7C%7C4846d9ddc0c7bf86356ed38912d6abf4d5622f0a38df713d7f38b059626faa55=5.00&x%3Aoriginalprice=5.00&x%3Al18n=%24%7C.%7C%2C%7C1%7C0&image%7C%7C6be0e53aca88d3c13d0aef0fcbaec24fb064a2a8c0c1c8e6ecc45f4322429538%7C%7Copen=https%3A%2F%2Fstaging.theoracare.com%2Fwp-content%2Fplugins%2Ffoxyshop%2Fimages%2Fno-photo.png&url%7C%7C8bbf55bf3c30d1fb40a18661651981f0b1b8000151ba1ff6a7702c7d508476fb=https%3A%2F%2Fstaging.theoracare.com%2Fproducts%2Fbeacon-addon-subscription%2F&quantity_min%7C%7C8822fd461696a49fa0a880ccd1526477d773ff8d3349fabd826ba02b7cfcc089%7C%7Copen=0&quantity_max%7C%7C1dd05fec257009d5826b96e0cb339322072718b6bfbe3d68411126224bd8e203%7C%7Copen=0&x%3Aquantity_max=0&sub_frequency%7C%7C5b3a89671834c684fed3ad1369b141f42017848b726ec36f2d6debe867260f36%7C%7Copen=1m&name%7C%7Cd1c50736e0a4ba63b17545acec56a55599a47d88ad42d5265994cee873edb5d8=Beacon+Addon+Subscription&code%7C%7C61990f98ec71b68eac25185ca01c0829178822c4403920c8c0399d2b513ffad5=305-2000-03&category%7C%7Cd1bf0c283d34aab87bbd3eb7def342c92e7d24f860d4437009c7d959c37f40dd=Subscriptions&quantity%7C%7C39f7f9ed1b387f7bf23eec7f5d47e4aae472b074b1c25b8fa6b57df92756c73d%7C%7Copen=1";
for ( var i = 0; i < FC.json.items.length; i++ ) {
if (FC.json.items[i].code == basic_service) {
basic_item_id = FC.json.items[i].id;
basic_qty = FC.json.items[i].quantity;
}
if (FC.json.items[i].code == deluxe_service) {
deluxe_item_id = FC.json.items[i].id;
deluxe_qty = FC.json.items[i].quantity;
}
if ( FC.json.items[i].code == connect ) {
connect_item_id = FC.json.items[i].id;
connect_qty = FC.json.items[i].quantity;
}
if ( FC.json.items[i].code == beacons ) {
beacon_item_id = FC.json.items[i].id;
beacon_qty = FC.json.items[i].quantity;
beacon_index = i;
}
}
if ( connect_item_id && beacon_item_id ) {
var remove_basic = true;
var remove_addon = true;
console.log("CONNECT AND BEACON");
if ( ! deluxe_item_id ) {
console.log("no deluxe id");
// add deluxe service
FC.client.request("https://" + FC.settings.storedomain + "/cart?" + deluxe_subscription).done(function(dataJSON) {
for ( var i = 0; i < FC.json.items.length; i++ ) {
if (FC.json.items[i].code == deluxe_service) {
deluxe_item_id = FC.json.items[i].id;
}
}
console.log("Added deluxe sub, ", deluxe_item_id);
if ( deluxe_item_id && connect_qty > 1 || beacon_qty > 1 ) {
var diff = Math.abs(connect_qty - beacon_qty);
console.log("Have new deluxe. connect is diff from beacon qty", connect_qty, beacon_qty, diff);
// qty is the same or more beacons than connects
if ( diff === 0 || beacon_qty > connect_qty ) {
FC.client.request("https://" + FC.settings.storedomain + "/cart?cart=update&quantity=" + connect_qty + "&id=" + deluxe_item_id).done(function(dataJSON) {
FC.cart.render();
console.log("2 Updated Quantity, connect qty:", connect_qty, beacon_qty);
});
} else {
// more connects than beacons
FC.client.request("https://" + FC.settings.storedomain + "/cart?cart=update&quantity=" + beacon_qty + "&id=" + deluxe_item_id).done(function(dataJSON) {
FC.cart.render();
console.log("2 Updated Quantity, beacon qty:", connect_qty, beacon_qty);
});
// also add the diff as basic service plans:
FC.client.request("https://" + FC.settings.storedomain + "/cart?" + basic_subscription).done(function(dataJSON) {
FC.cart.render();
remove_basic = false;
for ( var i = 0; i < FC.json.items.length; i++ ) {
if (FC.json.items[i].code == basic_service) {
basic_item_id = FC.json.items[i].id;
}
}
if ( basic_item_id ) {
FC.client.request("https://" + FC.settings.storedomain + "/cart?cart=update&quantity=" + diff + "&id=" + basic_item_id).done(function(dataJSON) {
FC.cart.render();
console.log("Updated basic Quantity");
});
}
});
}
}
FC.cart.render();
});
} else {
console.log("have deluxe service");
console.log("CONNECT qty", connect_qty);
console.log("beacon qty", beacon_qty);
if ( deluxe_item_id && connect_qty > 1 || beacon_qty > 1 || deluxe_qty !== connect_qty ) {
FC.client.request("https://" + FC.settings.storedomain + "/cart?cart=update&quantity=" + connect_qty + "&id=" + deluxe_item_id).done(function(dataJSON) {
FC.cart.render();
console.log("2 Updated Quantity, connect qty:", connect_qty, beacon_qty);
});
}
}
if ( basic_item_id && remove_basic )
FC.cart.removeItem({ id: basic_item_id });
if ( addon_item_id )
FC.cart.removeItem({ id: addon_item_id });
}
if ( connect_item_id && ! beacon_item_id ) {
console.log("CONNECT, NO BEACON");
if ( ! basic_item_id ) {
// add basic service
FC.client.request("https://" + FC.settings.storedomain + "/cart?" + basic_subscription).done(function(dataJSON) {
FC.cart.render();
for ( var i = 0; i < FC.json.items.length; i++ ) {
if (FC.json.items[i].code == basic_service) {
basic_item_id = FC.json.items[i].id;
}
}
if ( basic_item_id && connect_qty > 1 && basic_qty !== connect_qty ) {
FC.client.request("https://" + FC.settings.storedomain + "/cart?cart=update&quantity=" + connect_qty + "&id=" + basic_item_id).done(function(dataJSON) {
FC.cart.render();
console.log("Updated Quantity");
});
}
});
} else {
if ( basic_item_id && connect_qty > 1 && basic_qty !== connect_qty ) {
FC.client.request("https://" + FC.settings.storedomain + "/cart?cart=update&quantity=" + connect_qty + "&id=" + basic_item_id).done(function(dataJSON) {
FC.cart.render();
console.log("Updated Quantity");
});
}
}
if ( deluxe_item_id )
FC.cart.removeItem({ id: deluxe_item_id });
if ( addon_item_id )
FC.cart.removeItem({ id: addon_item_id });
}
if ( ! connect_item_id && beacon_item_id && ! addon_item_id ) {
console.log("NO CONNECT AND BEACON");
if ( beacon_index !== null ) {
var item = FC.json.items[beacon_index],
has_plan = findObjectByAttribute(item.options, "name", "Do you have a Deluxe Service Plan?");
if ( has_plan.value === 'No' ) {
FC.client.request("https://" + FC.settings.storedomain + "/cart?" + addon_subscription).done(function(dataJSON) {
FC.cart.render();
});
}
if ( deluxe_item_id )
FC.cart.removeItem({ id: deluxe_item_id });
if ( basic_item_id )
FC.cart.removeItem({ id: basic_item_id });
} // beacon item index, search for has_plan
}
if ( ! connect_item_id && ! beacon_item_id ) {
if ( deluxe_item_id )
FC.cart.removeItem({ id: deluxe_item_id });
if ( basic_item_id )
FC.cart.removeItem({ id: basic_item_id });
if ( addon_item_id )
FC.cart.removeItem({ id: addon_item_id });
}
}
function findObjectByAttribute (objects, attribute, value) {
for (var i = 0; i < objects.length; i++) {
if (objects[i][attribute] === value) {
return objects[i];
}
}
return null;
}
</script>
{% if context == "cart" %}
<script>
// var FC = FC || {};
// FC.onLoad = function () {
//FC.client.on("sidecart-show.done", checkServices);
//FC.client.on("cart-submit.done", checkServices);
//FC.client.on("cart-item-remove.done", checkServices);
//FC.client.on("cart-item-quantity-update.done", checkServices);
//FC.client.on("cart-coupon-add.done", checkServices);
// }
</script>
{% endif %}
{% if context == "checkout" %}
<script>
FC.client.on("ready.done", checkServices);
FC.client.on("cart-item-remove.done", checkServices);
FC.client.on("cart-item-quantity-update.done", checkServices);
FC.client.on("cart-coupon-add.done", checkServices);
</script>
{% endif %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment