// 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('.product-details'); var approve_price_wrapper = document.querySelector(".ProductPrice"); var approve_model_wrapper = document.querySelector('[itemprop=name]'); var approve_qty_ele = document.getElementsByName("qty[]")[0]; // 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_product_wrapper){ console.log("The approve product wrapper is missing from the page."); } if (!approve_price_wrapper){ console.log("The approve price wrapper is missing from the page."); } if (!approve_model_wrapper){ console.log("The approve model wrapper 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('.selectedValue'); approve_selected_options.forEach(function (item, index) { var approve_option_name = item.querySelector('label').querySelector('span.name').innerHTML; 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('.AddToCartButtonRow'); 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.margin="15px"; 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}); } }