// DEBUG MODE -- Set to true if you want console logs to show. var approve_debug_mode = true; approve_debug_log("APPROVE SCRIPT LOADED"); // Checks if we are on a mobile device or not // Use boolean to render different properties whether a user is on mobile or not var isMobile = window.innerWidth <= 600; var button_styling = { button_color: '#E65C01', hover_color: '#FFF', button_height: '', button_border: '3px solid #E65C01', border_radius: '3px', background_gradient: '', background_gradient_hover: '', font: '', font_color: '', font_hover_color: '#E65C01', font_size: '', font_weight: '', text_transform: '', powered_by_color: '', powered_by_size: '', max_width: '300px', min_width: '', width: '100%' }; if (isMobile) { button_styling.font_size = "smaller"; } var minimum_threshold = null; // Global setup var approve_product_wrapper_ele = '.body .productView'; var cart_url_pathname = '/cart.php'; var approve_gallery_wrapper_ele = '#product-listing-container ul.productGrid'; // Driver function to run integration function start_approve_integration() { // Checks if we are on a product page if (document.querySelector(approve_product_wrapper_ele)){ approve_product_page_integration(); } else { approve_debug_log('approve_product_wrapper_ele not found', 1); } // Checks if we are on a cart page if (window.location.pathname === cart_url_pathname){ approve_cart_page_integration(); } else { approve_debug_log('Cart page pathname not found', 1); } // Checks if we are on a gallery page if (document.querySelector(approve_gallery_wrapper_ele)){ approve_gallery_page_integration(); } else { approve_debug_log('approve_gallery_wrapper_ele not found', 1); } } // Product page integration function approve_product_page_integration() { // ****************************************************************** // PRODUCT PAGE 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_product = { model_ele_name: '.productView-product > .productView-title', price_ele_name: '.productView-product .productView-price .price-section[itemprop="offers"] .price', qty_ele_name: '.qty-panel .form-input.form-input--incrementTotal ', sku_ele_name: '.productView-info .row.sku-panel [itemprop="sku"]', insert_after_ele_name: '.productView-rating', insert_after_price_text: '', inc_quantity_ele: '.form-increment [data-action="inc"]', dec_quantity_ele: '.form-increment [data-action="dec"]', button_display_style: 'inline-block', button_wrapper_display_style: 'inline-block', // top / right / bottom / left button_wrapper_display_margin: '0px 0px 20px 0px', button_display_margin: '0px 0px 0px 0px', price_text_margin: '0px 0px 0px 0px', options_wrapper_ele_name: '', // DO NOT CHANGE select_ele_name: ' select', radio_ele_name: ' input[type="radio"]', cb_ele_name: ' input[type="checkbox"]', // END DO NOT CHANGE select_on: false, radio_on: false, cb_on: false, global_watcher: '', button_to_remove_ele: '' }; // ****************************************************************** // INITIAL PRODUCT PAGE ELEMENT SELECTION -- No Action Required // ****************************************************************** // model var approve_model_ele = document.querySelector(approve_product.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_product.price_ele_name); if (!approve_price_ele){approve_debug_log("APPROVE: No initial approve_price_ele found.",1);} // qty if (approve_product.qty_ele_name){ var approve_qty_ele = document.querySelector(approve_product.qty_ele_name); if (!approve_qty_ele){approve_debug_log("APPROVE: No initial approve_qty_ele found.",1);} } if (approve_product.sku_ele_name){ var approve_sku_ele = document.querySelector(approve_product.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_product.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 // ****************************************************************** function init_approve_button(){ // ****************************************************************** // DYNAMIC PRODUCT PAGE ELEMENT SELECTION -- No Action Required // ****************************************************************** // model var approve_model_ele = document.querySelector(approve_product.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_product.sku_ele_name){ approve_sku_ele = document.querySelector(approve_product.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.trim() + ") "; } // price var approve_price_ele = document.querySelector(approve_product.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_product.qty_ele_name){ var approve_qty_ele = document.querySelector(approve_product.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_product.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 OPTIONS // ****************************************************************** var approve_options_wrapper_ele = null; if (approve_product.options_wrapper_ele_name){ // Get options wrapper approve_options_wrapper_ele = document.querySelector(approve_product.options_wrapper_ele_name); if (!approve_options_wrapper_ele){ approve_debug_log("No approve_options_wrapper_ele found."); } else { // SELECT if (approve_product.select_on) { var approve_selected_options = approve_options_wrapper_ele.querySelectorAll(approve_product.select_ele_name); if (!approve_selected_options){ approve_debug_log("No approve_selected_options found."); } approve_selected_options.forEach(function (item, index) { // CONFIGURE SELECT OPTIONS HERE if (item && item.value) { approve_model += " | " + item.value; } }); } // RADIO if (approve_product.radio_on) { var approve_radioed_options = approve_options_wrapper_ele.querySelectorAll(approve_product.radio_ele_name); if (!approve_radioed_options){ approve_debug_log("No approve_radioed_options found."); } approve_radioed_options.forEach(function (item, index) { // CONFIGURE RADIO OPTIONS HERE if (item && item.checked) { approve_model += " | " + item.value; } }); } // CHECKBOX if (approve_product.cb_on) { var approve_checked_options = approve_options_wrapper_ele.querySelectorAll(approve_product.cb_ele_name); if (!approve_checked_options){ approve_debug_log("No approve_checked_options found."); } approve_checked_options.forEach(function (item, index) { // CONFIGURE RADIO OPTIONS HERE if (item && item.checked) { var next_sibling = item.nextElementSibling; approve_model += " | " + next_sibling.textContent; } }); } approve_debug_log('Model: '+approve_model + ' Price: ' + approve_price + ' Qty: ' + approve_qty); } } // ****************************************************************** // 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_product.options_wrapper_ele_name){ approve_options_wrapper_ele = document.querySelector(approve_product.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_product.select_ele_name); approve_options1.forEach(function (item, index) { item.addEventListener('change',event => { init_approve_button(); }) }); } } // Insert Button var approve_button = document.createElement('approve-button'); approve_button.style.width = button_styling.width; approve_button.style.minWidth = button_styling.min_width; approve_button.style.maxWidth = button_styling.max_width; approve_button.id = "approve_button_id"; if (approve_product.button_display_style){ approve_button.style.display = approve_product.button_display_style; } if (approve_product.button_display_margin){ approve_button.style.margin = approve_product.button_display_margin; } approve_button.setAttribute('application-type',"embedded_app"); var approve_btn_wrapper = document.createElement("div"); if (approve_product.button_wrapper_display_style){ approve_btn_wrapper.style.display = approve_product.button_wrapper_display_style; } if (approve_product.button_wrapper_display_margin){ approve_btn_wrapper.style.margin = approve_product.button_wrapper_display_margin; } approve_btn_wrapper.appendChild(approve_button); approve_btn_wrapper.style.width = "100%"; approve_insert_after_ele.after(approve_btn_wrapper); } if (approve_product.insert_after_price_text) { if (!document.querySelector(' .approve-price-teaser-container')) { var insert_ele = document.querySelector(approve_product.insert_after_price_text); if (!insert_ele) { approve_debug_log("Cant Find price text button insert after ele", 1); return; } let approve_button_wrapper = document.createElement("div"); approve_button_wrapper.className = "approve-price-teaser-container"; approve_button_wrapper.style.marginTop = "10px"; approve_button_wrapper.style.zIndex = "999999999999999"; approve_button_wrapper.innerHTML = /*html*/`
`; //button wrapper approve_button_wrapper.style.width = "auto"; approve_button_wrapper.style.display = "inline-block"; approve_button_wrapper.style.width = "100%"; if (approve_product.price_text_margin) approve_button_wrapper.style.margin = approve_product.price_text_margin; var approve_teaser_rate = approve_button_wrapper.querySelector('.teaser-rate'); var approve_teaser_text = approve_button_wrapper.querySelector('.approve-text-teaser'); approve_teaser_text.setAttribute('approve-model',approve_model); approve_teaser_text.setAttribute('approve-qty',1); approve_teaser_text.setAttribute('approve-price',approve_price); approve_teaser_text.setAttribute('approve-item-type',"new_product"); approve_teaser_rate.setAttribute('approve-total',approve_price); if (approve_price >= 500){ insert_ele.after(approve_button_wrapper); } } else { var approve_button_wrapper = document.querySelector(' .approve-price-teaser-container'); var approve_teaser_rate = approve_button_wrapper.querySelector('.teaser-rate'); var approve_teaser_text = approve_button_wrapper.querySelector('.approve-text-teaser'); approve_teaser_text.setAttribute('approve-model',approve_model); approve_teaser_text.setAttribute('approve-qty',1); approve_teaser_text.setAttribute('approve-price',approve_price); approve_teaser_text.setAttribute('approve-item-type',"new_product"); approve_teaser_rate.setAttribute('approve-total',approve_price); } window.kwipped_approve.core.activate_approve_teaser_rates(); } if (button_styling.button_color) approve_button.style.setProperty('--button_color', button_styling.button_color); if (button_styling.hover_color) approve_button.style.setProperty('--button_hover_color', button_styling.hover_color); if (button_styling.button_height) approve_button.style.setProperty('--button_height', button_styling.button_height); if (button_styling.button_border) approve_button.style.setProperty('--button_border', button_styling.button_border); if (button_styling.border_radius) approve_button.style.setProperty('--button_border_radius', button_styling.border_radius); if (button_styling.font) approve_button.style.setProperty('--button_font', button_styling.font); if (button_styling.font_color) approve_button.style.setProperty('--button_font_color', button_styling.font_color); if (button_styling.font_size) approve_button.style.setProperty('--button_font_size', button_styling.font_size); if (button_styling.text_transform) approve_button.style.setProperty('--button_text_transform', button_styling.text_transform); if (button_styling.font_hover_color) approve_button.style.setProperty('--button_font_hover_color', button_styling.font_hover_color); if (button_styling.background_gradient) approve_button.style.setProperty('--button_background_gradient', button_styling.background_gradient); if (button_styling.background_gradient_hover) approve_button.style.setProperty('--button_background_gradient_hover', button_styling.background_gradient_hover); if (button_styling.font_weight) approve_button.style.setProperty('--button_font_weight', button_styling.font_weight); if (button_styling.powered_by_color) approve_button.style.setProperty('--button_powered_by_color', button_styling.powered_by_color); if (button_styling.powered_by_size) approve_button.style.setProperty('--button_powered_by_size', button_styling.powered_by_size); // Removing a button if (approve_product.button_to_remove_ele){ var approve_button_to_remove = document.querySelector(approve_product.button_to_remove_ele); if (approve_button_to_remove){ approve_button_to_remove.style.display = "none"; } } // 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"); // If price is below $200, teaser rate breaks. Let's ensure that it's above $500. if ((parseFloat(approve_price) * parseInt(approve_qty)) < (minimum_threshold ? minimum_threshold : 500)){ approve_debug_log("Price is below $500."); approve_button.style.display = "none"; approve_button.closest('div').style.display = "none"; return; } } // ****************************************************************** // 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_product.inc_quantity_ele && approve_product.dec_quantity_ele){ activate_increase_and_decrease_buttons(0, approve_product.inc_quantity_ele, approve_product.dec_quantity_ele); } } 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}); } } if (approve_product.global_watcher) { var approve_global_watcher = document.querySelector(approve_product.global_watcher); if (approve_global_watcher){ // Add observer to price element const approve_global_observer = new MutationObserver(function() { init_approve_button(); }); approve_global_observer.observe(approve_global_watcher,{subtree: true, childList: true}); } } } // Cart page integration function approve_cart_page_integration() { button_styling.width = ""; button_styling.min_width = ""; button_styling.max_width = ""; var approve_cart_product = { wrapper_ele: '.page-content', item_ele: '.cart-list .cart-item', item_model_ele: '.cart-item-block.cart-item-title .cart-item-name', item_price_ele: '.cart-item-block.cart-item-info .cart-item-value', item_qty_ele: '.cart-item-block.cart-item-info.cart-item-quantity .form-increment .form-input--incrementTotal.cart-item-qty-input', sku: '', insert_btn_after_ele: '.cart-actions', global_watcher: '.page-content', model_option_ele: '', tax_price_ele: '', shipping_price_ele: '', wrapper_align: 'right', wrapper_margin: '30px 0px 10px 0px' }; // ****************************************************************** // CART PAGE ELEMENT SELECTION -- No Action Required // ****************************************************************** // Find data wrappers -- ideally with syntax so that there will not be a console error thrown on non-cart pages [ie. no multiple query selectors / finds]. function approve_initialize_cart(){ if (approve_cart_product.wrapper_ele){ var approve_cart_wrapper = document.querySelector(approve_cart_product.wrapper_ele); var approve_cart_item = document.querySelectorAll(approve_cart_product.item_ele); } if (!approve_cart_item){ if (approve_debug_mode){ console.log("no cart items found.") } return; } var approve_cart_items_array = []; approve_cart_item.forEach(function (item, index) { var approve_cart_item = {}; // Find model, price, and qty for each cart item. var approve_cart_item_model_wrapper = item.querySelector(approve_cart_product.item_model_ele); var approve_cart_item_qty_wrapper = item.querySelector(approve_cart_product.item_qty_ele); var approve_cart_item_price_wrapper = item.querySelector(approve_cart_product.item_price_ele); if (approve_cart_product.sku) var approve_cart_item_sku_wrapper = item.querySelector(approve_cart_product.sku); // check model if (!approve_cart_item_model_wrapper){ if (approve_debug_mode){ console.log("No approve_cart_item_model_wrapper found."); } return; } // var approve_cart_item_model_wrapper = approve_cart_item_model_wrapper; // set and encode cart item model. var approve_cart_item_model = approve_cart_item_model_wrapper.textContent.trim(); if (approve_cart_item_sku_wrapper) approve_cart_item_model += " (SKU: " + approve_cart_item_sku_wrapper.textContent.trim() + ")"; if (approve_cart_product.model_option_ele) { var options = item.querySelectorAll(approve_cart_product.model_option_ele); if (!options) { approve_debug_log("No approve cart item options found."); } else { options.forEach(function(item, index) { approve_cart_item_model += " | " + item.textContent; }); } } approve_cart_item.model = encodeURIComponent(approve_cart_item_model); // check price if (!approve_cart_item_price_wrapper){ if (approve_debug_mode){ console.log("No approve_cart_item_price_wrapper found."); } return; } var approve_price = approve_cart_item_price_wrapper.innerHTML.replace(/[^0-9.]/g, ''); var approve_cart_price = parseFloat(approve_price); // set price approve_cart_item.price = approve_cart_price; // check qty if (!approve_cart_item_qty_wrapper){ if (approve_debug_mode){ console.log("No approve_cart_item_qty_wrapper found. Qty is set to 1."); } var approve_qty = 1; } else { var approve_qty = approve_cart_item_qty_wrapper.value; } var approve_cart_qty = parseInt(approve_qty); // set qty approve_cart_item.qty = parseInt(approve_cart_qty); approve_cart_item.type = "new_product"; approve_cart_items_array.push(approve_cart_item); }); // 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_cart_button_id'); if (!approve_button){ // Find the element where we want to insert our button. var approve_cart_insert_after_ele = document.querySelector(approve_cart_product.insert_btn_after_ele); if(!approve_cart_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'); var approve_button_wrapper = document.createElement('div'); approve_button_wrapper.appendChild(approve_button); approve_button.id = "approve_cart_button_id"; if (approve_cart_product.wrapper_align) approve_button_wrapper.style.textAlign = approve_cart_product.wrapper_align; if (approve_cart_product.wrapper_margin) approve_button_wrapper.style.margin = approve_cart_product.wrapper_margin; if (button_styling.width) approve_button.style.width = button_styling.width; if (button_styling.min_width) approve_button.style.minWidth = button_styling.min_width; if (button_styling.max_width) approve_button.style.maxWidth = button_styling.max_width; approve_button.style.display="inline-block"; approve_button.setAttribute('application-type',"embedded_app"); approve_button.setAttribute('clear-cart-items',"true"); approve_cart_insert_after_ele.after(approve_button_wrapper); } if (button_styling.button_color) approve_button.style.setProperty('--button_color', button_styling.button_color); if (button_styling.hover_color) approve_button.style.setProperty('--button_hover_color', button_styling.hover_color); if (button_styling.button_height) approve_button.style.setProperty('--button_height', button_styling.button_height); if (button_styling.button_border) approve_button.style.setProperty('--button_border', button_styling.button_border); if (button_styling.border_radius) approve_button.style.setProperty('--button_border_radius', button_styling.border_radius); if (button_styling.font) approve_button.style.setProperty('--button_font', button_styling.font); if (button_styling.font_color) approve_button.style.setProperty('--button_font_color', button_styling.font_color); if (button_styling.font_size) approve_button.style.setProperty('--button_font_size', button_styling.font_size); if (button_styling.text_transform) approve_button.style.setProperty('--button_text_transform', button_styling.text_transform); if (button_styling.font_hover_color) approve_button.style.setProperty('--button_font_hover_color', button_styling.font_hover_color); if (button_styling.background_gradient) approve_button.style.setProperty('--button_background_gradient', button_styling.background_gradient); if (button_styling.background_gradient_hover) approve_button.style.setProperty('--button_background_gradient_hover', button_styling.background_gradient_hover); if (button_styling.font_weight) approve_button.style.setProperty('--button_font_weight', button_styling.font_weight); if (button_styling.powered_by_color) approve_button.style.setProperty('--button_powered_by_color', button_styling.powered_by_color); if (button_styling.powered_by_size) approve_button.style.setProperty('--button_powered_by_size', button_styling.powered_by_size); if (approve_cart_product.tax_price_ele){ var approve_tax_price_raw = document.querySelector(approve_cart_product.tax_price_ele); var approve_tax_price = approve_tax_price_raw.textContent.replace(/[^0-9.]/g, ''); approve_tax_price = parseFloat(approve_tax_price).toFixed(2); if (approve_tax_price && approve_tax_price > 0){ var approve_cart_shipping_item = {}; approve_cart_shipping_item.model = "Tax"; approve_cart_shipping_item.price = approve_tax_price; approve_cart_shipping_item.qty = 1; approve_cart_shipping_item.type = "new_product"; approve_cart_items_array.push(approve_cart_shipping_item); } } if (approve_cart_product.shipping_price_ele){ var approve_shipping_price_raw = document.querySelector(approve_cart_product.shipping_price_ele); var approve_shipping_price = approve_shipping_price_raw.textContent.replace(/[^0-9.]/g, ''); approve_shipping_price = parseFloat(approve_shipping_price).toFixed(2); if (approve_shipping_price && approve_shipping_price > 0){ var approve_cart_shipping_item = {}; approve_cart_shipping_item.model = "Shipping"; approve_cart_shipping_item.price = approve_shipping_price; approve_cart_shipping_item.qty = 1; approve_cart_shipping_item.type = "new_product"; approve_cart_items_array.push(approve_cart_shipping_item); } } approve_button.setAttribute('items',JSON.stringify(approve_cart_items_array)); } if (approve_cart_product.wrapper_ele){ approve_initialize_cart(); if (approve_cart_product.global_watcher) { var approve_global_watcher = document.querySelector(approve_cart_product.global_watcher); // Add observer to price element const approve_global_observer = new MutationObserver(function() { approve_initialize_cart(); }); approve_global_observer.observe(approve_global_watcher,{subtree: true, childList: true}); } } else { if (approve_debug_mode){ approve_debug_log("No approve_cart_wrapper found.", 1); } } } // Gallery page integration function approve_gallery_page_integration() { if (approve_gallery_wrapper_ele && document.querySelector(approve_gallery_wrapper_ele) ) { function init_approve_gallery_page_buttons(gallery_wrapper_ele_name) { // ****************************************************************** // PRODUCT GALLERY Variable Configuration // ****************************************************************** var approve_gallery_product = { page_check_ele_name: gallery_wrapper_ele_name, item_ele: gallery_wrapper_ele_name + ' li.product .card-body', item_model_ele_name: ' .card-title a', item_price_ele_name: ' .card-text[data-test-info-type="price"] .price-section:last-of-type .price', item_insert_after_ele_name: ' .card-text[data-test-info-type="price"]', // optional item_qty_ele_name: '', item_inc_quantity_ele_name: '', item_dec_quantity_ele_name: '', item_options_wrapper_ele_name: '', item_select_ele_name: '', item_button_to_remove_ele: '', item_button_display_style: 'block', item_button_display_margin: '10px 0px' }; // PRODUCT GALLERY // check for items, loop through and init button function with each var approve_product_gallery_page_check = (approve_gallery_product.page_check_ele_name ? document.querySelector(approve_gallery_product.page_check_ele_name) : null); if (approve_product_gallery_page_check){ var approve_gallery_product_array = document.querySelectorAll(approve_gallery_product.item_ele); approve_gallery_product_array.forEach(function(item, idx) { init_approve_product_gallery_item_button(item, idx); }); } function init_approve_product_gallery_item_button(item, idx) { // model var approve_product_gallery_item_model_ele = (approve_gallery_product.item_model_ele_name ? item.querySelector(approve_gallery_product.item_model_ele_name) : null); if (!approve_product_gallery_item_model_ele){ approve_debug_log("APPROVE: No dynamic approve_product_gallery_item_model_ele found.",0,1); return; } var approve_product_gallery_item_model = approve_product_gallery_item_model_ele.textContent // price var approve_product_gallery_item_price_ele = (approve_gallery_product.item_price_ele_name ? item.querySelector(approve_gallery_product.item_price_ele_name) : null); if (approve_gallery_product.item_price_ele_name && !approve_product_gallery_item_price_ele.innerHtml ) { var approve_product_gallery_item_price_eles = item.querySelectorAll(approve_gallery_product.item_price_ele_name); approve_product_gallery_item_price_eles = Array.from(approve_product_gallery_item_price_eles).filter(element => element.offsetParent !== null); if (approve_product_gallery_item_price_eles.length) { approve_product_gallery_item_price_ele = approve_product_gallery_item_price_eles[0]; } } if (!approve_product_gallery_item_price_ele){ approve_debug_log("APPROVE: No dynamic approve_product_gallery_item_price_ele found.",0,1); return; } var approve_product_gallery_item_price = approve_product_gallery_item_price_ele.innerHTML.replace(/[^0-9.]/g, ''); approve_product_gallery_item_price = parseFloat(approve_product_gallery_item_price); if (!approve_product_gallery_item_price || approve_product_gallery_item_price == 0){ approve_debug_log("APPROVE: approve_product_gallery_item_price not found (or is 0) after removing non-numerical characters.",1); } // qty var approve_product_gallery_item_qty_ele = (approve_gallery_product.item_qty_ele_name ? item.querySelector(approve_gallery_product.item_qty_ele_name) : null); var approve_product_gallery_item_qty = 1; if (!approve_product_gallery_item_qty_ele){ approve_debug_log("APPROVE: No dynamic approve_product_gallery_item_qty_ele found.",1); } else { approve_product_gallery_item_qty = approve_product_gallery_item_qty_ele.value; } approve_product_gallery_item_qty = parseInt(approve_product_gallery_item_qty); // element to insert after var approve_product_gallery_item_insert_after_ele = (approve_gallery_product.item_insert_after_ele_name ? item.querySelector(approve_gallery_product.item_insert_after_ele_name) : null); if (!approve_product_gallery_item_insert_after_ele){ approve_debug_log("APPROVE: No dynamic approve_product_gallery_item_insert_after_ele found.",0,1); return; } // Options if (approve_gallery_product.item_options_wrapper_ele_name){ var approve_product_gallery_item_options_wrapper_ele = document.querySelector(approve_gallery_product.item_options_wrapper_ele_name); if (!approve_product_gallery_item_options_wrapper_ele){ approve_debug_log("No approve_product_gallery_item_options_wrapper_ele found."); } else { // TRH - added else to prevent js error when options dont exist var approve_product_gallery_item_selected_options = approve_product_gallery_item_options_wrapper_ele.querySelectorAll(approve_gallery_product.item_select_ele_name); if (!approve_product_gallery_item_selected_options){ approve_debug_log("No approve_product_gallery_item_selected_options found."); } approve_product_gallery_item_selected_options.forEach(function (item, index) { // CONFIGURE OPTIONS HERE }); } } var approve_product_gallery_item_button = item.querySelector('#approve_product_gallery_item_button_'+idx); if (!approve_product_gallery_item_button){ // If event listener is needed for options, here is where it can be initiated. if (approve_gallery_product.item_options_wrapper_ele_name){ var approve_product_gallery_item_options_wrapper_ele = document.querySelector(approve_gallery_product.item_options_wrapper_ele_name); if (approve_product_gallery_item_options_wrapper_ele){ // If a change listener is needed for options, it is initliazed here. var approve_product_gallery_item_options1 = approve_product_gallery_item_options_wrapper_ele.querySelectorAll(approve_gallery_product.item_select_ele_name); approve_product_gallery_item_options1.forEach(function (opt_item) { opt_item.addEventListener('change',event => { init_approve_product_gallery_item_button(item, idx); }) }); } } // Insert Button var approve_product_gallery_item_button = document.createElement('approve-button'); approve_product_gallery_item_button.id = "approve_product_gallery_item_button_"+idx; // BUTTON CSS if (approve_gallery_product.item_button_display_style){ approve_product_gallery_item_button.style.display = approve_gallery_product.item_button_display_style; } if (approve_gallery_product.item_button_display_margin){ approve_product_gallery_item_button.style.margin = approve_gallery_product.item_button_display_margin; } approve_product_gallery_item_button.setAttribute('application-type',"embedded_app"); var approve_product_gallery_item_btn_wrapper = document.createElement("div"); approve_product_gallery_item_btn_wrapper.appendChild(approve_product_gallery_item_button); approve_product_gallery_item_insert_after_ele.after(approve_product_gallery_item_btn_wrapper); } // Removing a button if (approve_gallery_product.item_button_to_remove_ele){ var approve_product_gallery_item_button_to_remove = document.querySelector(approve_gallery_product.item_button_to_remove_ele); if (approve_product_gallery_item_button_to_remove){ approve_product_gallery_item_button_to_remove.style.display = "none"; } } if (button_styling.button_color) approve_product_gallery_item_button.style.setProperty('--button_color', button_styling.button_color); if (button_styling.hover_color) approve_product_gallery_item_button.style.setProperty('--button_hover_color', button_styling.hover_color); if (button_styling.button_height) approve_product_gallery_item_button.style.setProperty('--button_height', button_styling.button_height); if (button_styling.button_border) approve_product_gallery_item_button.style.setProperty('--button_border', button_styling.button_border); if (button_styling.border_radius) approve_product_gallery_item_button.style.setProperty('--button_border_radius', button_styling.border_radius); if (button_styling.font) approve_product_gallery_item_button.style.setProperty('--button_font', button_styling.font); if (button_styling.font_color) approve_product_gallery_item_button.style.setProperty('--button_font_color', button_styling.font_color); if (button_styling.font_size) approve_product_gallery_item_button.style.setProperty('--button_font_size', button_styling.font_size); if (button_styling.text_transform) approve_product_gallery_item_button.style.setProperty('--button_text_transform', button_styling.text_transform); if (button_styling.font_hover_color) approve_product_gallery_item_button.style.setProperty('--button_font_hover_color', button_styling.font_hover_color); if (button_styling.background_gradient) approve_product_gallery_item_button.style.setProperty('--button_background_gradient', button_styling.background_gradient); if (button_styling.background_gradient_hover) approve_product_gallery_item_button.style.setProperty('--button_background_gradient_hover', button_styling.background_gradient_hover); if (button_styling.font_weight) approve_product_gallery_item_button.style.setProperty('--button_font_weight', button_styling.font_weight); if (button_styling.powered_by_color) approve_product_gallery_item_button.style.setProperty('--button_powered_by_color', button_styling.powered_by_color); if (button_styling.powered_by_size) approve_product_gallery_item_button.style.setProperty('--button_powered_by_size', button_styling.powered_by_size); // If price is below $200, teaser rate breaks. Let's ensure that it's above $500. if ((parseFloat(approve_product_gallery_item_price) * parseInt(approve_product_gallery_item_qty)) < 500){ approve_debug_log("Price is below $500."); approve_product_gallery_item_button.style.display = "none"; return; } // Set approve button variables. approve_product_gallery_item_button.setAttribute('price',approve_product_gallery_item_price); approve_product_gallery_item_button.setAttribute('model',approve_product_gallery_item_model); approve_product_gallery_item_button.setAttribute('qty',approve_product_gallery_item_qty); approve_product_gallery_item_button.setAttribute('type',"new_product"); if (approve_product_gallery_item_qty_ele){ // Add change watcher to qty input approve_product_gallery_item_qty_ele.addEventListener('change',event => { var this_item = item; approve_product_gallery_item_update_qty(approve_product_gallery_item_qty_ele.value, this_item, idx); }) if (approve_gallery_product.item_inc_quantity_ele_name && approve_gallery_product.item_dec_quantity_ele_name ){ approve_product_gallery_item_activate_increase_and_decrease_buttons(item, idx); } } } // ****************************************************************** // Simple set button qty. Must pass the new QTY to the button. // ****************************************************************** function approve_product_gallery_item_update_qty(new_qty, item, idx){ var approve_product_gallery_item_button = item.querySelector('#approve_product_gallery_item_button_'+idx); if (approve_product_gallery_item_button){ var approve_product_gallery_item_btn_qty = parseInt(approve_product_gallery_item_button.getAttribute('qty')); if (new_qty != approve_product_gallery_item_btn_qty){ approve_product_gallery_item_button.setAttribute('qty',new_qty); } } } function approve_product_gallery_item_activate_increase_and_decrease_buttons(item, idx, number_of_tries=0) { number_of_tries++; // buttons that raise or lower the quantity. var approve_product_gallery_item_qty_btn_inc = item.querySelector(approve_gallery_product.item_inc_quantity_ele_name); var approve_product_gallery_item_qty_btn_dec = item.querySelector(approve_gallery_product.item_dec_quantity_ele_name); if ((!approve_product_gallery_item_qty_btn_inc || !approve_product_gallery_item_qty_btn_dec ) && number_of_tries < 10 ){ setTimeout(() => { approve_product_gallery_item_activate_increase_and_decrease_buttons(item, idx, number_of_tries) },500); return; } // Assign click events to these buttons so that qty is updated on click. if(approve_product_gallery_item_qty_btn_inc && approve_product_gallery_item_qty_btn_dec ){ approve_product_gallery_item_qty_btn_inc.addEventListener('click',event => { var this_item = item; // console.log("INSIDE CLICK EVENT FOR BUTTON ", item); var approve_product_gallery_item_button = item.querySelector('#approve_product_gallery_item_button_'+idx); if (approve_product_gallery_item_button){ var approve_product_gallery_item_btn_qty = parseInt(approve_product_gallery_item_button.getAttribute('qty')); approve_product_gallery_item_btn_qty = approve_product_gallery_item_btn_qty + 1; approve_product_gallery_item_update_qty(approve_product_gallery_item_btn_qty, item, idx); } }); approve_product_gallery_item_qty_btn_dec.addEventListener('click',event => { var approve_product_gallery_item_button = item.querySelector('#approve_product_gallery_item_button_'+idx); if (approve_product_gallery_item_button){ var approve_product_gallery_item_btn_qty = parseInt(approve_product_gallery_item_button.getAttribute('qty')); if (approve_product_gallery_item_btn_qty > 1){ approve_product_gallery_item_btn_qty = approve_product_gallery_item_btn_qty - 1; approve_product_gallery_item_update_qty(approve_product_gallery_item_btn_qty, item, idx); } } }); } else { approve_debug_log("APPROVE: No approve_qty_btn_inc or approve_qty_btn_dec found.",1); } } } function approve_gallery_url_changed() { setTimeout(() => {init_approve_gallery_page_buttons(approve_gallery_wrapper_ele);},1000); } // call approve_gallery_url_changed 1x initially to load buttons on gallery pages approve_gallery_url_changed(); // 1) Back/forward navigation window.addEventListener('popstate', () => { approve_debug_log("popstate"); approve_gallery_url_changed(); }); // 2) Hash-only changes (just in case) window.addEventListener('hashchange', () => { approve_debug_log("hashchange"); approve_gallery_url_changed(); }); // 3) pagination clicks (dont trigger hashchanges or popstate) document.addEventListener('click', (e) => { approve_debug_log("CLICKED"); const a = e.target.closest && e.target.closest('a[href]'); if (!a) return; if (/\bpage=\d+\b/i.test(a.search) || a.rel === 'next' || a.rel === 'prev' || a.closest?.('.pagination,[role="navigation"]')) { // Give the site a moment to call pushState/replaceState; then check approve_gallery_url_changed(); } }, true); } } // // Call this with your handler (or edit onUrlChange inline below) // (function attachUrlListener(onUrlChange = () => console.log('URL changed:', location.href)) { // const fire = (() => { // let last = location.href, t; // return () => { // // Debounce + dedupe by href // clearTimeout(t); // t = setTimeout(() => { // if (location.href !== last) { // last = location.href; // onUrlChange(); // } // }, 50); // }; // })(); // // 1) Back/forward navigation // window.addEventListener('popstate', fire); // // 2) Hash-only changes (just in case) // window.addEventListener('hashchange', fire); // // 3) Patch History API for SPA/AJAX updates // ['pushState', 'replaceState'].forEach((m) => { // const orig = history[m]; // history[m] = function (...args) { // const ret = orig.apply(this, args); // fire(); // return ret; // }; // }); // // 4) Last-resort polling (for sites that mutate DOM without touching history) // let pollId = null; // const startPoll = () => { // if (pollId) return; // pollId = setInterval(fire, 300); // }; // // Start a light poll; harmless if not needed // startPoll(); // // 5) Optional: catch clicks on likely pagination links to prompt early check // document.addEventListener('click', (e) => { // const a = e.target.closest && e.target.closest('a[href]'); // if (!a) return; // if (/\bpage=\d+\b/i.test(a.search) || a.rel === 'next' || a.rel === 'prev' || a.closest?.('.pagination,[role="navigation"]')) { // // Give the site a moment to call pushState/replaceState; then check // setTimeout(fire, 0); // } // }, true); // // Initial call if you care about page load state // // fire(); // })(); // APPROVE color debug log 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; "; } log = (typeof log == "object" ? JSON.stringify(log) : log); log = (typeof log == "array" ? JSON.stringify(log) : log); if (approve_debug_mode) console.log("%c"+log,css); } /** * 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, inc_quantity_ele, dec_quantity_ele){ number_of_tries++; // buttons that raise or lower the quantity. var approve_qty_btn_inc = document.querySelector(inc_quantity_ele); var approve_qty_btn_dec = document.querySelector(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, inc_quantity_ele, dec_quantity_ele)),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_update_text_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); approve_update_text_qty(approve_btn_qty); } } }); } else { approve_debug_log("APPROVE: No approve_qty_btn_inc or approve_qty_btn_dec found.",1); } } // Function for updating button quantity 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); } } } // Function for updating teaser text quantity function approve_update_text_qty(approve_qty){ var approve_button = document.querySelector('.approve-text-teaser'); if (approve_button){ var approve_btn_qty = parseInt(approve_button.getAttribute('approve-qty')); if (approve_qty != approve_btn_qty){ approve_button.setAttribute('approve-qty',approve_qty); var teaser = approve_button.querySelector('.teaser-rate'); teaser.setAttribute('approve-total', teaser.getAttribute('default-price') * approve_qty) window.kwipped_approve.core.activate_approve_teaser_rates(); } } } // Run integration start_approve_integration();