// DEBUG MODE -- Set to true if you want console logs to show. var approve_debug_mode = true; // GENERAL PRODUCT WRAPPER -- element to look for to know we are on a product page var approve_product_wrapper_ele = '.product-info-main'; var approve_product_wrapper = document.querySelector(approve_product_wrapper_ele); if (approve_product_wrapper){ // ****************************************************************** // PRODUCT PAGE Variable Configuration // ****************************************************************** // In the easiest implementation, you will be able to set every variable you need right here. // GENERAL PRODUCT INFO WRAPPERS (optional) -- elements that wrap the product information that we need. Leave blank if you can use querySelector to find the element directly, without fear of selecting the wrong element. var approve_model_wrapper_ele_name = ''; var approve_price_wrapper_ele_name = '.price-container.price-final_price'; var approve_qty_wrapper_ele_name = '.box-tocart .field.qty'; // PRODUCT INFO ELEMENTS -- if there is a wrapper present, these elements will be found within their wrappers. Otherwise, it will be a document query selector search. var approve_model_ele_name = "[data-ui-id='page-title-wrapper']"; var approve_price_ele_name = '.price'; var approve_qty_ele_name = 'input'; // OPTIONS (entire sectional is optional) // Options wrapper (Required if there are options) -- used to help find select elements var approve_options_wrapper_ele_name = ''; // Select element -- this will be used in a foreach loop (querySelectorAll) var approve_select_ele_name = ''; // Selected option var approve_option_selected_ele_name = ''; // Label (optional) -- will be appended before the selected option value in model name. var approve_option_selected_ele_label_name = ''; // ELEMENT TO INSERT THE BUTTON BELOW var approve_insert_after_ele = document.querySelector('.box-tocart'); // qty inc / dec elements (optional) -- will add click watchers on these elements for QTY updates. var approve_qty_ele_wrapper_name = ''; var approve_inc_quantity_ele = '.qty-inc'; var approve_dec_quantity_ele = '.qty-dec'; // ****************************************************************** // END PRODUCT PAGE Variable Configuration // ****************************************************************** // ****************************************************************** // END PRODUCT PAGE Variable Configuration // ****************************************************************** // ****************************************************************** // PRODUCT PAGE ELEMENT SELECTION -- No Action Required // ****************************************************************** // Grabbing variables based on the variable configuration. // model if (approve_model_wrapper_ele_name){ var approve_model_ele = document.querySelector(approve_model_wrapper_ele_name).querySelector(approve_model_ele_name); } else { var approve_model_ele = document.querySelector(approve_model_ele_name); } // price if (approve_price_wrapper_ele_name){ var approve_price_ele = document.querySelector(approve_price_wrapper_ele_name).querySelector(approve_price_ele_name); } else { var approve_price_ele = document.querySelector(approve_price_ele_name); if (!approve_price_ele){ var approve_price_ele = document.querySelector(approve_price_ele_name_del); } } // qty if (approve_qty_wrapper_ele_name){ var approve_qty_ele = document.querySelector(approve_qty_wrapper_ele_name).querySelector(approve_qty_ele_name); } else if (approve_qty_ele_name){ var approve_qty_ele = document.querySelector(approve_qty_ele_name); } // ****************************************************************** // END PRODUCT PAGE ELEMENT SELECTION // ****************************************************************** // ****************************************************************** // PRODUCT PAGE CODE // ****************************************************************** // Handles both the creation and update of the approve button. function init_approve_button(){ // model if (!approve_model_ele){ if (approve_debug_mode){ console.log("No approve_model_raw found."); } return; } var approve_model = approve_model_ele.textContent var initial_approve_model = approve_model; // price if (!approve_price_ele){ if (approve_debug_mode){ console.log("No approve_price_raw found."); } return; } var approve_price = approve_price_ele.innerHTML.replace(/[^0-9.]/g, ''); approve_price = parseFloat(approve_price); // qty if (!approve_qty_ele){ if (approve_debug_mode){ console.log("No approve_qty_raw found. Qty is set to 1."); } var approve_qty = 1; } else { var approve_qty = approve_qty_ele.value; } approve_qty = parseInt(approve_qty); // ****************************************************************** // FIND SELECT OPTIONS // ****************************************************************** if (approve_options_wrapper_ele_name){ // Get options wrapper approve_options_wrapper_ele = document.querySelector(approve_options_wrapper_ele_name); // if (approve_debug_mode) { // console.log("approve_options_wrapper_ele", approve_options_wrapper_ele); // } if (!approve_options_wrapper_ele){ if (approve_debug_mode){ console.log("No approve_options_wrapper_ele found."); } } var approve_selected_options = approve_options_wrapper_ele.querySelectorAll(approve_select_ele_name); if (!approve_selected_options){ if (approve_debug_mode){ console.log("No approve_selected_options found."); } } approve_selected_options.forEach(function (item, index) { // CONFIGURE OPTIONS HERE // console.log("APPROVE OPTIONS ---- ", item, index); var approve_option_name = item.value; approve_model = approve_model + " / " + approve_option_name; }); } // Check to see if the approve button is already on the page. If it isn't, we will create it now. 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){ // If a change listener is needed for options, it is initliazed here. var approve_options_wrapper_ele1 = document.querySelector(approve_options_wrapper_ele_name); var approve_options1 = approve_options_wrapper_ele1.querySelectorAll(approve_select_ele_name); approve_options1.forEach(function (item, index) { let this_item = item; // let value = this_item.value; let value = item[item.selectedIndex].innerHTML; item.addEventListener('change',event => { var approve_option_name = item.value; approve_model = approve_model + " / " + approve_option_name; // initial_approve_model = approve_model + " "+item.value; // init_approve_button(); }) }); } // Insert Button if(!approve_insert_after_ele){ if (approve_debug_mode){ console.log("Element to insert approve button after not found."); } return; } var approve_button = document.createElement('approve-button'); approve_button.id = "approve_button_id"; approve_button.style.display="inline-block"; approve_button.style.marginTop="25px"; approve_button.style.marginBottom="25px"; approve_button.setAttribute('application-type',"embedded_app"); var approve_btn_wrapper = document.createElement("div"); approve_btn_wrapper.style.width="100%"; approve_btn_wrapper.appendChild(approve_button); approve_insert_after_ele.after(approve_btn_wrapper); } // window.kwipped_approve.core.init // If price is below $200, teaser rate breaks. Let's ensure that it's above $500. if ((parseFloat(approve_price) * parseInt(approve_qty)) < 500){ if (approve_debug_mode){ console.log("Price is below $200."); } 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) } } if (approve_debug_mode){ console.log("IN Approve Product Wrapper"); } 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){ // buttons that raise or lower the quantity. if (approve_qty_ele_wrapper_name){ var approve_qty_btn_inc = document.querySelector(approve_qty_ele_wrapper_name).querySelector(approve_inc_quantity_ele); var approve_qty_btn_dec = document.querySelector(approve_qty_ele_wrapper_name).querySelector(approve_dec_quantity_ele); } else { var approve_qty_btn_inc = document.querySelector(approve_inc_quantity_ele); var approve_qty_btn_dec = document.querySelector(approve_dec_quantity_ele); } // 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 { if (approve_debug_mode){ console.log("No qty buttons found."); } } } } else { if (approve_debug_mode){ console.log("No qty element found."); } } if (approve_price_ele){ var approve_price_watcher = approve_price_ele; // 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 { if (!approve_product_wrapper && approve_debug_mode){ console.log("No approve_product_wrapper found"); } } // ****************************************************************** // PRODUCT GALLERY Variable Configuration // ****************************************************************** // Check to make sure that we are on a product gallery page var approve_product_gallery_page_check = '.columns4.products-grid'; // Wrapper for each item var approve_product_gallery_item_ele = '.product-item-info.type1'; // Price for each item var approve_product_gallery_item_price_ele = '.product-item-info.type1 > .product-item-details > .price-box.price-final_price > .price-final_price > .price-wrapper > .price'; // Insert button after var approve_product_gallery_insert_btn_after_ele = ".product-item-inner"; // ****************************************************************** // END PRODUCT GALLERY Variable Configuration // ****************************************************************** if (approve_product_gallery_page_check){ // PRODUCT GALLERY var approve_product_page_check = document.querySelector(approve_product_gallery_page_check); if (approve_product_page_check){ var approve_gallery_product_array = document.querySelectorAll(approve_product_gallery_item_ele); approve_gallery_product_array.forEach(function(item) { var approve_gallery_price_wrapper = item.querySelector(approve_product_gallery_item_price_ele); var approve_gallery_price = 0; if (approve_gallery_price_wrapper){ approve_gallery_price = approve_gallery_price_wrapper.textContent; approve_gallery_price = approve_gallery_price.replace(/[^0-9-.]/g, ''); approve_gallery_price = parseFloat(approve_gallery_price); } var insert_ele = item.querySelector(approve_product_gallery_insert_btn_after_ele); let approve_button_wrapper = document.createElement("div"); approve_button_wrapper.className = "approve-gallery-btn-container"; approve_button_wrapper.style.marginBottom = "10px"; approve_button_wrapper.innerHTML = /*html*/`
`; //button wrapper approve_button_wrapper.style.width = "auto"; // approve_button_wrapper.style.display = "inline-block"; //button var approve_gallery_button = approve_button_wrapper.querySelector('.approve-gallery-button'); approve_gallery_button.style.textAlign = "center"; approve_gallery_button.style.borderRadius = "12px"; approve_gallery_button.style.fontFamily = "verdana,sans-serif"; approve_gallery_button.style.fontSize = "14px"; approve_gallery_button.style.color = "#000000"; // button teaser var approve_gallery_teaser = approve_button_wrapper.querySelector('.teaser'); approve_gallery_teaser.style.fontSize = "1.5em"; approve_gallery_teaser.style.fontWeight = "700"; // sm text var approve_gallery_teaser = approve_button_wrapper.querySelector('.sm-txt'); approve_gallery_teaser.style.fontSize = ".8em"; // button teaser rate var approve_gallery_teaser_rate = approve_button_wrapper.querySelector('.teaser-rate'); approve_gallery_teaser_rate.setAttribute('approve-total',approve_gallery_price); if (approve_gallery_price >= 1500){ insert_ele.after(approve_button_wrapper); } }); window.kwipped_approve.core.activate_approve_teaser_rates(); } }