// Find data wrappers -- ideally with syntax so that there will not be a console error thrown on non-product pages [ie. no multiple query selectors / finds] var approve_product_wrapper = document.querySelector('.page-content--product'); var approve_price_wrapper = document.querySelector(".product__price"); var approve_model_wrapper = document.querySelector('.product-single__title'); var approve_qty_ele = document.querySelector('[aria-label="Quantity"]'); // Set this to true if you want console logs to run var approve_debug_mode = false; // Equiped to put the button on the page and update the button on subsequent data changes. function init_approve_button(){ if (!approve_product_wrapper || !approve_price_wrapper || !approve_model_wrapper){ if (approve_debug_mode){ console.log("One of the wrapper variables is missing from the page."); } return; } // Find price & clean var approve_price = approve_price_wrapper.innerHTML; if (!approve_price){ if (approve_debug_mode){ console.log("No price found."); } return; } approve_price = approve_price.replace(/[^0-9-.]/g, ''); approve_price = parseInt(approve_price); // Find model. Clean if necessary. var approve_model = approve_model_wrapper.textContent; if (!approve_model){ if (approve_debug_mode){ console.log("No model found."); } return; } // If qty wrapper exists, find qty. Otherwise, qty is set to 1. if (approve_qty_ele){ var approve_qty = approve_qty_ele.value; } else { var approve_qty = 1; } // Element containing SELECTED options goes here. We need to add to the model name. var approve_selected_options = document.querySelectorAll('input:checked'); approve_selected_options.forEach(function (item, index) { var approve_option_name = item.value; approve_model = approve_model + " / " + approve_option_name; }); if(isNaN(approve_price) || !approve_model || !approve_qty){ if (approve_debug_mode){ console.log("Something went wrong in the configuration of button variables"); } return; } // Find the element where we want to insert our button. var approve_insert_after_ele = document.querySelector('.payment-buttons'); if(!approve_insert_after_ele){ if (approve_debug_mode){ console.log("Element to insert approve button after not found."); } return; } // 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){ var approve_button = document.createElement('approve-button'); approve_button.id = "approve_button_id"; approve_button.style.display="inline-block"; approve_button.style.textAlign="center"; approve_button.style.marginTop="10px"; approve_button.setAttribute('application-type',"embedded_app"); approve_insert_after_ele.after(approve_button); } // If price is below $200, teaser rate breaks. Let's ensure that it's above $200. if ((parseFloat(approve_price) * parseInt(approve_qty)) < 200){ if (approve_debug_mode){ console.log("Price is below $200."); } approve_button.style.display = "none"; return; } approve_button.setAttribute('price',approve_price); approve_button.setAttribute('model',approve_model); approve_button.setAttribute('qty',approve_qty); approve_button.setAttribute('type',"new_product"); } 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 (approve_product_wrapper){ init_approve_button(); // Add change listener to qty input if (approve_qty_ele){ approve_qty_ele.addEventListener('change',event => { var approve_qty = parseInt(approve_qty_ele.value); approve_update_qty(approve_qty); }) } if (approve_price_wrapper){ // Add observer to price element const approve_price_observer = new MutationObserver(function() { init_approve_button(); }); approve_price_observer.observe(approve_price_wrapper,{subtree: false, childList: true}); } // Element containing SELECTED options goes here. We need to add to the model name. var approve_options = document.querySelectorAll('input[type="radio"]'); approve_options.forEach(function (item, index) { item.addEventListener('change', function() { init_approve_button(); }); }); } // PRODUCT GALLERY var approve_product_page_check = document.querySelector("#CollectionSection"); if (approve_product_page_check){ var approve_gallery_product_array = document.querySelectorAll('.grid-product'); approve_gallery_product_array.forEach(function(item) { var approve_gallery_price_wrapper = item.querySelector(".grid-product__price"); 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(".grid-product__price"); let approve_button_wrapper = document.createElement("div"); approve_button_wrapper.className = "approve-gallery-btn-container"; approve_button_wrapper.style.marginTop = "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"; // 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(); }