// DEBUG MODE -- Set to true if you want console logs to show. var approve_debug_mode = false; if (approve_debug_mode) { console.log("APPROVE SCRIPT LOADED"); } var approve_remove_pf_financing_eles = document.querySelectorAll(".pf_financing"); if(approve_remove_pf_financing_eles.length > 0) { // console.log("approve_remove_pf_financing_eles", approve_remove_pf_financing_eles); for (var i = 0; i < approve_remove_pf_financing_eles.length; i++) { approve_remove_pf_financing_eles[i].remove() } } // ****************************************************************** // ****************************************************************** // PRODUCT PAGE // ****************************************************************** // ****************************************************************** function approve_init_product_page_script() { if (document.querySelector("body.product .productView")) { // product page // 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 = 'body.product .productView .container'; 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+' .productView-details .productView-product .productView-title'; var approve_price_ele_name = approve_product_wrapper_ele+' .productView-details .productView-product .productView-price .price-section [data-product-price-without-tax]'; var approve_qty_ele_name = approve_product_wrapper_ele+' .form-increment input.form-input--incrementTotal'; // 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 = approve_product_wrapper_ele+' section.productView-details .productView-options'; // 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 = approve_product_wrapper_ele+' .form-increment button[data-action="inc"]'; var approve_dec_quantity_ele = approve_product_wrapper_ele+' .form-increment button[data-action="dec"]'; // OPTIONS -- All of the following is optional. Options are be fairly custom // Wrapper for the ENTIRE options section var approve_options_wrapper_ele_name = approve_product_wrapper_ele+' .product-variant'; // 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_options_select_ele_name = ''; var approve_options_radio_ele_name = '.form-option-wrapper input'; var approve_attributes_wrapper_ele_name = approve_product_wrapper_ele+' form.form'; // 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_attributes_select_ele_name = '.product-variant [data-product-attribute="set-select"] select'; // APPROVE BUTTON STYLING var approve_button_display_style = '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 = '10px 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){ if (approve_debug_mode){ console.warn("APPROVE: No initial approve_model_ele found."); } } // price var approve_price_ele = document.querySelector(approve_price_ele_name); if (!approve_price_ele){ if (approve_debug_mode){ console.warn("APPROVE: No initial approve_price_ele found."); } } // qty if (approve_qty_ele_name){ var approve_qty_ele = document.querySelector(approve_qty_ele_name); if (!approve_qty_ele){ if (approve_debug_mode){ console.warn("APPROVE: No initial approve_qty_ele found."); } } } // element to insert after var approve_insert_after_ele = document.querySelector(approve_insert_after_ele_name); if (!approve_insert_after_ele){ if (approve_debug_mode){ console.warn("APPROVE: No initial approve_insert_after_ele found."); } } // ****************************************************************** // 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){ if (approve_debug_mode){ console.error("APPROVE: No dynamic approve_model_ele found."); } return; } var approve_model = approve_model_ele.textContent // price var approve_price_ele = document.querySelector(approve_price_ele_name); if (!approve_price_ele){ if (approve_debug_mode){ console.error("APPROVE: No dynamic approve_price_ele found."); } return; } var approve_price = approve_price_ele.innerHTML.replace(/[^0-9.]/g, ''); approve_price = parseFloat(approve_price); if (!approve_price || approve_price == 0){ if (approve_debug_mode){ console.warn("APPROVE: Price not found (or is 0) after removing non-numerical characters."); } } // qty if (approve_qty_ele_name){ var approve_qty_ele = document.querySelector(approve_qty_ele_name); } if (!approve_qty_ele){ if (approve_debug_mode){ console.warn("APPROVE: No dynamic approve_qty_ele found."); } var approve_qty = 1; } else { var 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){ if (approve_debug_mode){ console.error("APPROVE: No dynamic approve_insert_after_ele found."); } return; } // ****************************************************************** // END DYNAMIC PRODUCT PAGE ELEMENT SELECTION // ****************************************************************** // ****************************************************************** // FIND SELECT OPTIONS // ****************************************************************** if (approve_options_wrapper_ele_name){ // Get options wrapper var approve_options_wrapper_ele = document.querySelector(approve_options_wrapper_ele_name); if (!approve_options_wrapper_ele){ if (approve_debug_mode){ console.log("No approve_options_wrapper_ele found."); } } else { if (approve_options_select_ele_name) { var approve_selected_options = approve_options_wrapper_ele.querySelectorAll(approve_options_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 if (item.value && item.value != "" ) { // console.log("ITEM VALUE => ", item.value); var item_text = item.querySelector("[value='"+item.value+"']").innerHTML; approve_model += " | "+item.value+": "+item_text; } }); } // console.log("APPROVE OPTIONS WRAPPER ELE", approve_options_wrapper_ele); var approve_radio_options = approve_options_wrapper_ele.querySelectorAll(approve_options_radio_ele_name); if (!approve_radio_options){ if (approve_debug_mode){ console.log("No approve_radio_options found."); } } approve_radio_options.forEach(function (item, index) { // CONFIGURE OPTIONS HERE if (item.value && item.value != "" && item.checked ) { // var approve_radio_option_parent_ele = item.closest(".form-option-wrapper"); // var approve_radio_option_label_ele = null; // var approve_label_attrib = null; // if (approve_radio_option_parent_ele) { // approve_radio_option_label_ele = item.closest("label"); // if (approve_radio_option_label_ele) { // approve_label_attrib = approve_radio_option_label_ele.getAttribute("aria-label"); // console.log("GET ATTRIB => ", approve_label_attrib); // } // } approve_model += " | "+item.value; var approve_radio_attrib = item.getAttribute("aria-label"); if (approve_radio_attrib) { approve_model += " - "+approve_radio_attrib; } // var item_text = item.querySelector("[value='"+item.value+"']").innerHTML; // approve_model += " | "+item.value+": "+item_text; } }); } } // ****************************************************************** // END SELECT options // ****************************************************************** // ****************************************************************** // FIND SELECT ATTRIBUTES // ****************************************************************** if (approve_attributes_wrapper_ele_name){ // Get options wrapper // console.log("LOOKING FOR ATTRIBS"); var approve_attributes_wrapper_ele = document.querySelector(approve_attributes_wrapper_ele_name); // console.log("LOOKING FOR ATTRIBS", approve_attributes_wrapper_ele); if (!approve_attributes_wrapper_ele){ if (approve_debug_mode){ console.log("No approve_attributes_wrapper_ele found."); } } else { var approve_selected_attributes = approve_attributes_wrapper_ele.querySelectorAll(approve_attributes_select_ele_name); // console.log("LOOKING FOR ATTRIBS 2 ", approve_selected_attributes); if (!approve_selected_attributes){ if (approve_debug_mode){ console.log("No approve_selected_attributes found."); } } approve_selected_attributes.forEach(function (item, index) { // CONFIGURE OPTIONS HERE if (item.value && item.value != "" ) { var item_text = item.querySelector("[value='"+item.value+"']").innerHTML; approve_model += " | "+item.value+": "+item_text; } }); } } // ****************************************************************** // END SELECT attributes // ****************************************************************** // ****************************************************************** // 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){ var approve_options_wrapper_ele = document.querySelector(approve_options_wrapper_ele_name); if (approve_options_wrapper_ele && approve_options_select_ele_name ){ // If a change listener is needed for options, it is initliazed here. var approve_options1 = approve_options_wrapper_ele.querySelectorAll(approve_options_select_ele_name); approve_options1.forEach(function (item, index) { item.addEventListener('change',event => { init_approve_button(); }) }); } } if (approve_attributes_wrapper_ele_name){ var approve_attributes_wrapper_ele = document.querySelector(approve_attributes_wrapper_ele_name); if (approve_attributes_wrapper_ele){ // If a change listener is needed for attributes, it is initliazed here. var approve_attributes1 = approve_attributes_wrapper_ele.querySelectorAll(approve_attributes_select_ele_name); approve_attributes1.forEach(function (item, index) { item.addEventListener('change',event => { init_approve_button(); }) }); } } // 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){ if (approve_debug_mode){ console.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 = 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_debug_mode){ console.warn("APPROVE: approve_product_wrapper not found.") } } /** * * 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 { if (approve_debug_mode){ console.warn("APPROVE: No approve_qty_btn_inc or approve_qty_btn_dec found."); } } } // ****************************************************************** // ****************************************************************** // END PRODUCT PAGE // ****************************************************************** // ****************************************************************** } } setTimeout(() => { approve_init_product_page_script(); },100); if (document.querySelector('[data-product-type="featured"]')) { setTimeout(() => { approve_init_featured_items_script(); }, 200); } function approve_init_featured_items_script() { // gallery homepage if (approve_debug_mode) { console.log("GALLERY IMAGES"); } // ****************************************************************** // PRODUCT GALLERY Variable Configuration // ****************************************************************** // Check to make sure that we are on a product gallery page var approve_product_gallery_page_check = '[data-product-type="featured"]'; // Wrapper for each item var approve_product_gallery_item_ele = '.slick-list [data-product-slide]'; // Price for each item var approve_product_gallery_item_price_ele = '.card-text.myprice .price-section.price-section--withoutTax:nth-of-type(1) [data-product-price-without-tax]'; // Insert button after var approve_product_gallery_insert_btn_after_ele = ' .add-cart'; // PRODUCT GALLERY if (approve_product_gallery_page_check){ 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); // console.log("IN EACH GALLERY ITEM", approve_gallery_price_wrapper); var approve_gallery_price = 0; if (approve_gallery_price_wrapper){ approve_gallery_price = approve_gallery_price_wrapper.innerHTML; 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_gallery_button_wrapper = document.createElement("div"); approve_gallery_button_wrapper.className = "approve-gallery-btn-container"; approve_gallery_button_wrapper.style.marginTop = "10px"; approve_gallery_button_wrapper.innerHTML = /*html*/` `; //button wrapper approve_gallery_button_wrapper.style.width = "100%0"; approve_gallery_button_wrapper.style.textAlign = "center" approve_gallery_button_wrapper.style.display = "block"; //button var approve_gallery_button = approve_gallery_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"; // button teaser var approve_gallery_teaser = approve_gallery_button_wrapper.querySelector('.teaser'); approve_gallery_teaser.style.fontSize = "1.5em"; approve_gallery_teaser.style.fontWeight = "700"; // sm text var approve_gallery_teaser = approve_gallery_button_wrapper.querySelector('.sm-txt'); approve_gallery_teaser.style.fontSize = ".8em"; // button teaser rate var approve_gallery_teaser_rate = approve_gallery_button_wrapper.querySelector('.teaser-rate'); approve_gallery_teaser_rate.setAttribute('approve-total',approve_gallery_price); if (approve_gallery_price >= 1500){ insert_ele.after(approve_gallery_button_wrapper); } }); window.kwipped_approve.core.activate_approve_teaser_rates(); } } setTimeout(() => { // console.log("CHECK => ", document.querySelector("body.category main .container.catagory-space")); if (document.querySelector("body.category main .container.catagory-space")) { approve_init_search_page_script(); } }, 1000); function approve_init_search_page_script() { // gallery page if (approve_debug_mode) { console.log("LIST PAGE"); } // ****************************************************************** // PRODUCT GALLERY Variable Configuration // ****************************************************************** // Check to make sure that we are on a product gallery page var approve_product_list_page_check_name = 'body.category main .container.catagory-space'; // Wrapper for each item var approve_product_list_item_ele = 'ul.productGrid li.product'; // Price for each item var approve_product_list_item_price_ele = '.card-text.myprice .price-section.price-section--withoutTax:nth-of-type(1) [data-product-price-without-tax]'; // Insert button after var approve_product_list_insert_btn_after_ele = '.add-cart'; // PRODUCT GALLERY if (approve_product_list_page_check_name){ var approve_list_page_check = document.querySelector(approve_product_list_page_check_name); } if (approve_list_page_check){ var approve_list_product_array = document.querySelectorAll(approve_product_list_item_ele); // console.log("LIST PAGE CHECK => ", approve_list_page_check, approve_list_product_array) approve_list_product_array.forEach(function(item) { var approve_list_price_wrapper = item.querySelector(approve_product_list_item_price_ele); // console.log("SEARCH PAGE ITEM PRICE ELE ", approve_list_price_wrapper); var approve_list_price = 0; if (approve_list_price_wrapper){ approve_list_price = approve_list_price_wrapper.innerHTML; approve_list_price = approve_list_price.replace(/[^0-9-.]/g, ''); approve_list_price = parseFloat(approve_list_price); } var insert_ele = item.querySelector(approve_product_list_insert_btn_after_ele); let approve_list_button_wrapper = document.createElement("div"); approve_list_button_wrapper.className = "approve-gallery-btn-container"; approve_list_button_wrapper.style.marginTop = "10px"; approve_list_button_wrapper.innerHTML = /*html*/` `; //button wrapper approve_list_button_wrapper.style.width = "100%"; approve_list_button_wrapper.textAlign = "center"; approve_list_button_wrapper.style.display = "inline-block"; //button var approve_list_button = approve_list_button_wrapper.querySelector('.approve-gallery-button'); approve_list_button.style.textAlign = "center"; approve_list_button.style.borderRadius = "12px"; approve_list_button.style.fontFamily = "verdana,sans-serif"; approve_list_button.style.fontSize = "14px"; // button teaser var approve_list_teaser = approve_list_button_wrapper.querySelector('.teaser'); approve_list_teaser.style.fontSize = "1.5em"; approve_list_teaser.style.fontWeight = "700"; // sm text var approve_list_teaser = approve_list_button_wrapper.querySelector('.sm-txt'); approve_list_teaser.style.fontSize = ".8em"; // button teaser rate var approve_list_teaser_rate = approve_list_button_wrapper.querySelector('.teaser-rate'); approve_list_teaser_rate.setAttribute('approve-total',approve_list_price); if (approve_list_price >= 500){ insert_ele.after(approve_list_button_wrapper); } }); window.kwipped_approve.core.activate_approve_teaser_rates(); } }