var product_list = [ "/products/mahlkonig-ek43-filter-grinder", "/products/mahlkonig-e65s-gbw-espresso-grinder", "/products/mahlkonig-e65s-espresso-grinder-e65s", "/products/nuova-simonelli-mythos-grinder", "/products/mahlkonig-ek43s-filter-coffee-grinder", "/products/mahlkonig-e80-supreme-on-demand-espresso-grinder", "/products/eversys-cameo-espresso-machine", "/products/marco-friia-cs-cold-sparkling-undercounter-water-system", "/products/franke-a1000-flex-superautomatic-coffee-machine", "/products/marco-beverage-systems-friia-hot-cold-sparkling-water-system-hc-hcs-plus", "/products/victoria-arduino-mythos-2-espresso-grinder", "/products/mahlkonig-e80s-gbw-espresso-grinder", "/products/victoria-arduino-eagle-one", "/products/schaerer-coffee-art-plus-super-automatic-coffee-machine", "/products/marco-ecoboiler-ecosmart-tap-dispense-hot-water-dispenser-t5-t10", "/products/victoria-arduino-athena-leva-lever-espresso-machine", "/products/victoria-arduino-white-eagle-digit-espresso-machine", "/products/marco-sp9-commercial-pour-over-coffee-brewer", "/products/sanremo-you-espresso-machine", "/products/franke-a1000-fm-cm-superautomatic-coffee-machine", "/products/eversys-enigma-classic-espresso-machine", "/products/marco-ecosmart-ecoboiler-under-counter", "/products/unic-stella-epic-espresso-machine", "/products/marco-under-counter-mix-boiler-uc3-uc8", "/products/victoria-arduino-mythos-ii-on-demand-grinder", "/products/victoria-arduino-venus-bar-semiautomatic-espresso-maker", "/products/eversys-shotmaster-espresso-machines", "/products/nuova-simonelli-prontobar-touch-super-automatic-machine", "/products/unic-mira-2-group-volumetric-tall-cup-mi-2g-av-tc", "/products/unic-mira-1-group-volumetric-with-steamair-mi-1g-av-sa", "/products/unic-mira-1-group-volumetric-tall-cup-mi-1g-av-tc", "/products/unic-classic-volumetric-espresso-machine", "/products/sanremo-f18sb-espresso-machine", "/products/sanremo-zoe-competition-espresso-machine", "/products/victoria-arduino-black-eagle-volumetric-espresso-machine", "/products/eversys-shotmaster-pro-superautomatic-espresso-machines", "/products/unic-stella-2-group-volumetric-stella-2g", "/products/unic-mira-3-group-volumetric-mi-3g-av", "/products/unic-mira-2-group-volumetric-mi-2g-av", "/products/unic-classic-1-group-volumetric-espresso-machine-tall-cup", "/products/egro-mode-pure-coffee", "/products/egro-mode-quick-milk", "/products/egro-next-pure-coffee", "/products/egro-next-top-milk", "/products/egro-one-pure-coffee", "/products/egro-one-quick-milk", "/products/egro-one-top-milk", "/products/egro-touch-coffee-2-hopper-configuration", "/products/egro-touch-coffee", "/products/rancilio-specialty-invicta", "/products/rancilio-specialty-rs1", "/products/rancilio-classe-9-usb", "/products/rancilio-classe-9-s", "/products/rancilio-classe-7-usb", "/products/rancilio-classe-5-usb", "/products/rancilio-classe-7-s", "/products/rancilio-classe-5-s", "/products/rancilio-classe-5-1-group", "/products/sanremo-cafe-racer-espresso-machine", "/products/sanremo-cube-r-a-version-espresso-machine", "/products/sanremo-f18-espresso-machine", "/products/sanremo-opera-espresso-machine", "/products/sanremo-x-one-espresso-grinder", "/products/sanremo-zoe-competition-compact-volumetric-espresso-machine", "/products/marco-ecoboiler-push-button-dispense-hot-water-dispenser", "/products/poursteady-ps1-5-cup-system-commercial-pour-over-coffee-brewer", "/products/poursteady-ps1-3c-3-cup-system-commercial-pour-over-coffee-brewer", "/products/victoria-arduino-black-eagle-maverick-gravimetric-espresso-machine", "/products/victoria-arduino-myone-espresso-grinder", "/products/victoria-arduino-venus-bar-volumetric-espresso-maker", "/products/victoria-arduino-white-eagle-t3-espresso-machine", "/products/eversys-enigma-super-traditional-espresso-machines", "/products/unic-tango-ace-superautomatic", "/products/unic-stella-3-group-volumetric-stella-3g-av", "/products/unic-mira-2-group-volumetric-with-steamair-mi-2g-av-sa", "/products/unic-mira-1-group-volumetric-tall-cup-with-steamair-mi-1g-av-tc-sa", "/products/unic-mira-1-group-volumetric-mi-1g-av", "/products/unic-classic-steamair-volumetric-espresso-machine", ]; var valid_product = false; product_list.forEach(function (item, index) { if (window.location.pathname.indexOf(item) != -1) { valid_product = true; } }) // the window.location.hostname check will prevent integration from loading on the kwipped approve hosted page // DEBUG MODE -- Set to true if you want console logs to show. var approve_debug_mode = false; approve_debug_log("APPROVE SCRIPT LOADED"); // ****************************************************************** // ****************************************************************** // PRODUCT PAGE // ****************************************************************** // ****************************************************************** function init_approve_product_page_button(){ setTimeout(() => { if (valid_product) { approve_debug_log('Valid Product'); // In the easiest implementation, you will be able to set every variable you need right here. // GENERAL PRODUCT WRAPPER -- element to look for to know we are on a product page var approve_product_wrapper_ele = '.product-block-list.product-block-list--large'; var approve_product_wrapper = document.querySelector(approve_product_wrapper_ele); // Must ensure we are on a product page before attempting to select the remaining elements. if (approve_product_wrapper){ // ****************************************************************** // PRODUCT PAGE Variable Configuration // ****************************************************************** // PRODUCT INFO ELEMENTS -- These strings will be used in document.querySelector(). Ensure that there is only one of each on the page. var approve_model_ele_name = approve_product_wrapper_ele + ' .product-meta__title.heading.h1'; var approve_price_ele_name = approve_product_wrapper_ele + ' .price-list .price.price--highlight'; if (!document.querySelector(approve_price_ele_name)) { approve_price_ele_name = approve_product_wrapper_ele + ' .price-list .price'; } // ELEMENT TO INSERT THE BUTTON BELOW -- This strings will be used in document.querySelector(). Ensure that there is only one on the page. var approve_insert_after_ele_name = '.product-meta'; var approve_qty_ele_name = '.quantity-selector__value'; // QTY INCREASE / DECREASE BUTTONS -- These buttons do not always trigger a change event on the QTY element. We will add click watchers on these elements. [Optional] var approve_inc_quantity_ele = ' .quantity-selector__button[data-action="increase-picker-quantity"]'; var approve_dec_quantity_ele = '.quantity-selector__button[data-action="decrease-picker-quantity"]'; // SKU var approve_sku_ele_name = ''; // OPTIONS -- All of the following is optional. Options are be fairly custom // Wrapper for the ENTIRE options section var approve_options_wrapper_ele_name = ' .product-form__variants'; var approve_options_wrapper_ele_name_2 = ' .bold_option_set'; // Elements that need to have *change* watchers. Mostly used on select elements. Will be used in a foreach, so picking a class with multiple selects is both allowed and encouraged. Ensure these elements are contained within the wrapper above. var approve_select_ele_name = 'input[type="radio"]'; var approve_select_ele_name_2 = 'select'; // APPROVE BUTTON STYLING var approve_button_display_style = 'inline-block'; // '50px' -- All sides will have margin of 50PX // '50px 10px' -- top/bottom will be 50px, right/left 10px // '50px 10px 20px 30px' -- top / right / bottom / left var approve_button_display_margin = '0px 0px 0px 0px'; var approve_button_to_remove_ele = ""; // ****************************************************************** // END PRODUCT PAGE Variable Configuration // ****************************************************************** // ****************************************************************** // INITIAL PRODUCT PAGE ELEMENT SELECTION -- No Action Required // ****************************************************************** // Grabbing variables based on the variable configuration. // IMPORTANT -- this selection has to be made both INSIDE and OUTSIDE of init_approve_button. // This is because we need to first identify the elements for the use of watchers -- but also need them to be dynamic. // model var approve_model_ele = document.querySelector(approve_model_ele_name); if (!approve_model_ele){ approve_debug_log("APPROVE: No initial approve_model_ele found.",1); } // price var approve_price_ele = document.querySelector(approve_price_ele_name); if (!approve_price_ele){ approve_debug_log("APPROVE: No initial approve_price_ele found.",1); } // qty if (approve_qty_ele_name){ var approve_qty_ele = document.querySelector(approve_qty_ele_name); if (!approve_qty_ele){ approve_debug_log("APPROVE: No initial approve_qty_ele found.",1); } } if (approve_sku_ele_name){ var approve_sku_ele = document.querySelector(approve_sku_ele_name); if (!approve_sku_ele){ approve_debug_log("APPROVE: No initial approve_sku_ele found.",1); } } // element to insert after var approve_insert_after_ele = document.querySelector(approve_insert_after_ele_name); if (!approve_insert_after_ele){ approve_debug_log("APPROVE: No initial approve_insert_after_ele found.",1); } // ****************************************************************** // END INITIAL PRODUCT PAGE ELEMENT SELECTION // ****************************************************************** // ****************************************************************** // Initilizes the approve button. // This function handles both the CREATION and UPDATE of button variables // ****************************************************************** function init_approve_button(){ // ****************************************************************** // DYNAMIC PRODUCT PAGE ELEMENT SELECTION -- No Action Required // ****************************************************************** // Grabbing variables based on the variable configuration. // IMPORTANT -- this selection has to be made both INSIDE and OUTSIDE of init_approve_button. // This is because we need to first identify the elements for the use of watchers -- but also need them to be dynamic. // model var approve_model_ele = document.querySelector(approve_model_ele_name); if (!approve_model_ele){ approve_debug_log("APPROVE: No dynamic approve_model_ele found.",0,1); return; } var approve_sku_ele = null; if (approve_sku_ele_name){ approve_sku_ele = document.querySelector(approve_sku_ele_name); if (!approve_sku_ele){ approve_debug_log("APPROVE: No dynamic approve_sku_ele found.",1); } } var approve_model = approve_model_ele.textContent; if (approve_sku_ele) { approve_model += " (SKU: " + approve_sku_ele.textContent + ") "; } // price var approve_price_ele = document.querySelector(approve_price_ele_name); if (!approve_price_ele){ approve_debug_log("APPROVE: No dynamic approve_price_ele found.",0,1); return; } var approve_price = approve_price_ele.innerHTML.replace(/[^0-9.]/g, ''); approve_price = parseFloat(approve_price); if (!approve_price || approve_price == 0){ approve_debug_log("APPROVE: Price not found (or is 0) after removing non-numerical characters.",1); } // qty if (approve_qty_ele_name){ var approve_qty_ele = document.querySelector(approve_qty_ele_name); } var approve_qty = 1; if (!approve_qty_ele){ approve_debug_log("APPROVE: No dynamic approve_qty_ele found.",1); } else { approve_qty = approve_qty_ele.value; } approve_qty = parseInt(approve_qty); // element to insert after var approve_insert_after_ele = document.querySelector(approve_insert_after_ele_name); if (!approve_insert_after_ele){ approve_debug_log("APPROVE: No dynamic approve_insert_after_ele found.",0,1); return; } // ****************************************************************** // END DYNAMIC PRODUCT PAGE ELEMENT SELECTION // ****************************************************************** // ****************************************************************** // FIND SELECT OPTIONS // ****************************************************************** var approve_options_wrapper_ele = null; if (approve_options_wrapper_ele_name){ // Get options wrapper approve_options_wrapper_ele = document.querySelector(approve_options_wrapper_ele_name); if (!approve_options_wrapper_ele){ approve_debug_log("No approve_options_wrapper_ele found."); } else { // TRH - added else to prevent js error when options dont exist var approve_selected_options = approve_options_wrapper_ele.querySelectorAll(approve_select_ele_name); if (!approve_selected_options){ approve_debug_log("No approve_selected_options found."); } approve_selected_options.forEach(function (item, index) { // CONFIGURE OPTIONS HERE if (item.checked) { approve_model += " | " + item.value; } }); } } var approve_options_wrapper_ele_2 = null; if (approve_options_wrapper_ele_name_2){ // Get options wrapper approve_options_wrapper_ele_2 = document.querySelector(approve_options_wrapper_ele_name_2); if (!approve_options_wrapper_ele_2){ approve_debug_log("No approve_options_wrapper_ele_2 found."); } else { // TRH - added else to prevent js error when options dont exist var approve_selected_options_2 = approve_options_wrapper_ele_2.querySelectorAll(approve_select_ele_name_2); if (!approve_selected_options_2){ approve_debug_log("No approve_selected_options found."); } approve_selected_options_2.forEach(function (item, index) { // CONFIGURE OPTIONS HERE // console.log(item.value); if (item.value) { approve_model += " | " + item.value; } }); } } // ****************************************************************** // END SELECT OPTIONS // ****************************************************************** // ****************************************************************** // ADD APPROVE BUTTON TO PAGE // ****************************************************************** // Check if the button is on the page var approve_button = document.getElementById('approve_button_id'); if (!approve_button){ // If event listener is needed for options, here is where it can be initiated. if (approve_options_wrapper_ele_name){ approve_options_wrapper_ele = document.querySelector(approve_options_wrapper_ele_name); if (approve_options_wrapper_ele){ // If a change listener is needed for options, it is initliazed here. var approve_options1 = approve_options_wrapper_ele.querySelectorAll(approve_select_ele_name); approve_options1.forEach(function (item, index) { item.addEventListener('change',event => { // setTimeout(()=>{ init_approve_button(); // }, 500); }) }); } } // Insert Button= var approve_button = document.createElement('approve-button'); approve_button.id = "approve_button_id"; if (approve_button_display_style){ approve_button.style.display = approve_button_display_style; } if (approve_button_display_margin){ approve_button.style.margin = approve_button_display_margin; } approve_button.setAttribute('application-type',"embedded_app"); var approve_btn_wrapper = document.createElement("div"); approve_btn_wrapper.appendChild(approve_button); approve_insert_after_ele.after(approve_btn_wrapper); } // Removing a button if (approve_button_to_remove_ele){ var approve_button_to_remove = document.querySelector(approve_button_to_remove_ele); if (approve_button_to_remove){ approve_button_to_remove.style.display = "none"; } } // If price is below $200, teaser rate breaks. Let's ensure that it's above $500. if ((parseFloat(approve_price) * parseInt(approve_qty)) < 500){ approve_debug_log("Price is below $500."); approve_button.style.display = "none"; return; } // Set approve button variables. approve_button.setAttribute('price',approve_price); approve_button.setAttribute('model',approve_model); approve_button.setAttribute('qty',approve_qty); approve_button.setAttribute('type',"new_product"); } // ****************************************************************** // Simple set button qty. Must pass the new QTY to the button. // ****************************************************************** function approve_update_qty(approve_qty){ var approve_button = document.getElementById('approve_button_id'); if (approve_button){ var approve_btn_qty = parseInt(approve_button.getAttribute('qty')); if (approve_qty != approve_btn_qty){ approve_button.setAttribute('qty',approve_qty); } } } // ****************************************************************** // If timing is an issue, this function creates an interval for setting button qty. // ****************************************************************** var approve_timer = null; function approve_update_qty_timer(){ var approve_button = document.getElementById('approve_button_id'); if (approve_button){ if(approve_timer) { clearInterval(approve_timer); approve_timer = null; } var approve_btn_qty = parseInt(approve_button.getAttribute('qty')); var number_of_checks = 0; approve_timer = setInterval(function(){ if(number_of_checks>=5){ clearInterval(approve_timer); approve_timer = null; } if(approve_btn_qty != parseInt(approve_qty_ele.value)){ approve_button.setAttribute('qty',parseInt(approve_qty_ele.value)); clearInterval(approve_timer); approve_timer = null; } number_of_checks++; },500) } } init_approve_button(); // If there is a qty element, we need to set watchers/listeners. if (approve_qty_ele){ // Add change watcher to qty input approve_qty_ele.addEventListener('change',event => { approve_update_qty_timer(); }) if (approve_inc_quantity_ele && approve_dec_quantity_ele){ activate_increase_and_decrease_buttons(); } } if (approve_price_ele){ var approve_price_watcher = document.querySelector(' .product-block-list.product-block-list--large'); // Add observer to price element const approve_price_observer = new MutationObserver(function() { init_approve_button(); }); approve_price_observer.observe(approve_price_watcher,{subtree: true, childList: true}); var approve_model_watcher = approve_model_ele; if (approve_model_watcher){ // Add observer to price element const approve_model_observer = new MutationObserver(function() { init_approve_button(); }); approve_model_observer.observe(approve_model_watcher,{subtree: false, childList: true}); } } } else { approve_debug_log("APPROVE: approve_product_wrapper not found.",1) } } }, 1900); } init_approve_product_page_button(); var approve_global_watcher2 = document.querySelector(" .bold_option_total div span"); if (approve_global_watcher2){ console.log("watcher was found") // Add observer to element const approve_global_observer2 = new MutationObserver(function() { // checks to see if theres no button, but we are on a product page if(!document.querySelector("approve-button") && document.querySelector(approve_product_wrapper_ele) ){ init_approve_product_page_button(); } }); approve_global_observer2.observe(approve_global_watcher2,{subtree: true, childList: true}); } /** * * Activates quantity buttons. * QTY buttons are, sometimes added to the page after a page has been rendered. This will wait for that to happen. */ function activate_increase_and_decrease_buttons(number_of_tries=0){ number_of_tries++; // buttons that raise or lower the quantity. var approve_qty_btn_inc = document.querySelector(approve_inc_quantity_ele); var approve_qty_btn_dec = document.querySelector(approve_dec_quantity_ele); if ((!approve_qty_btn_inc || !approve_qty_btn_dec) && number_of_tries < 10){ setTimeout(()=>(activate_increase_and_decrease_buttons(number_of_tries)),500); return } // Assign click events to these buttons so that qty is updated on click. if(approve_qty_btn_inc && approve_qty_btn_dec){ approve_qty_btn_inc.addEventListener('click',event => { var approve_button = document.getElementById('approve_button_id'); if (approve_button){ var approve_btn_qty = parseInt(approve_button.getAttribute('qty')); approve_btn_qty = approve_btn_qty + 1; approve_update_qty(approve_btn_qty); } }); approve_qty_btn_dec.addEventListener('click',event => { var approve_button = document.getElementById('approve_button_id'); if (approve_button){ var approve_btn_qty = parseInt(approve_button.getAttribute('qty')); if (approve_btn_qty > 1){ approve_btn_qty = approve_btn_qty - 1; approve_update_qty(approve_btn_qty); } } }); } else { approve_debug_log("APPROVE: No approve_qty_btn_inc or approve_qty_btn_dec found.",1); } } // ****************************************************************** // ****************************************************************** // END PRODUCT PAGE // ****************************************************************** // ****************************************************************** // IF YOU WANT TO USE A LOG WITH COLORS YOU CAN USE function approve_debug_log(log, warn, err) { warn = warn || false; err = err || false; let css = "padding: 5px 20px; "; if (err) { css += "background:#8B0000; color:#fff; "; } else if(warn) { css += "background:#FFBF00; color:#000; "; } else { css += "background:#418AC9; color:#fff; "; } if (approve_debug_mode) console.log("%c"+log,css); }