// // Version: 1.2 | Author: Zach | Date: 6/26/2024 // // THIS TEMPLATE IS MEANT TO BE USED WITH BUTTON #90 // // DEBUG MODE -- Set to true if you want console logs to show. // var approve_debug_mode = true; // approve_debug_log("APPROVE SCRIPT LOADED"); // // IS MOBILE CHECK // var mobile = false; // if ( window.innerWidth <= 600 ) { // mobile = true; // } // // INTEGRATION BUTTON STYLING // var custom_button_color = ""; // var custom_button_hover_color = ""; // var custom_button_height = ""; // var custom_button_border = ""; // var custom_button_border_radius = ""; // var custom_background_gradient = ""; // var custom_background_gradient_hover = ""; // var custom_font = ""; // var custom_font_color = ""; // var custom_font_size = ""; // var custom_text_transform = ""; // var custom_font_hover_color = ""; // var custom_powered_by_color = ""; // var custom_powered_by_size = ""; // var custom_font_weight = ""; // var button_max_width = ""; // var button_min_width = ""; // var button_width = "100%"; // var minimum_threshold = null; // // GENERAL PRODUCT WRAPPER // // var approve_product_wrapper_ele = ''; // var cart_url_pathname = ''; // // ****************************************************************** // // GALLERY PAGE // // ****************************************************************** // // gallery wrapper // var approve_gallery_wrapper_check_ele_name = " .dmContent"; // if (approve_gallery_wrapper_check_ele_name // && document.querySelector(approve_gallery_wrapper_check_ele_name) // ) { // function init_approve_gallery_page_buttons(gallery_wrapper_ele_name) { // // ****************************************************************** // // PRODUCT GALLERY Variable Configuration // // ****************************************************************** // var approve_gallery_iframe_ele_name = ' .dmRespRow iframe'; // // Check to make sure that we are on a product gallery page // var approve_product_gallery_page_check_ele_name = gallery_wrapper_ele_name; // // Wrapper for each item relative to document with approve_product_gallery_page_check_ele_name // var approve_product_gallery_item_ele = ' .has-image.shopify-buy__layout-vertical.shopify-buy__product'; // // Model for each item relative to the item // var approve_product_gallery_item_model_ele_name = ' [data-element="product.title"]'; // // Price for each item relative to the item // var approve_product_gallery_item_price_ele_name = ' .shopify-buy__product__actual-price '; // // Insert button after relative to the item // var approve_product_gallery_item_insert_after_ele_name = ' .shopify-buy__btn'; // // Qty button relative to the item (optional) // var approve_product_gallery_item_qty_ele_name = ''; // var approve_product_gallery_item_inc_quantity_ele_name = ''; // var approve_product_gallery_item_dec_quantity_ele_name = ''; // // Options relative to the item (optional) (Watcher and event change not programmed) // var approve_product_gallery_item_options_wrapper_ele_name = ''; // var approve_product_gallery_item_select_ele_name = ''; // // CSS for button // var approve_product_gallery_item_button_display_style = 'block'; // var approve_product_gallery_item_button_display_margin = '10px 0px'; // var approve_product_gallery_item_button_to_remove_ele = ''; // // PRODUCT GALLERY // // check for items, loop through and init button function with each // var approve_product_gallery_page_check = (approve_product_gallery_page_check_ele_name ? document.querySelector(approve_product_gallery_page_check_ele_name) : null); // var approve_gallery_iframe = document.querySelectorAll(approve_gallery_iframe_ele_name); // approve_gallery_iframe.forEach(function (iframe){ // if (approve_product_gallery_page_check){ // var approve_gallery_product_array = iframe.contentDocument.querySelectorAll(approve_product_gallery_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_product_gallery_item_model_ele_name ? item.querySelector(approve_product_gallery_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_product_gallery_item_price_ele_name ? item.querySelector(approve_product_gallery_item_price_ele_name) : null); // 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_product_gallery_item_qty_ele_name ? item.querySelector(approve_product_gallery_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_product_gallery_item_insert_after_ele_name ? item.querySelector(approve_product_gallery_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_product_gallery_item_options_wrapper_ele_name){ // var approve_product_gallery_item_options_wrapper_ele = document.querySelector(approve_product_gallery_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_product_gallery_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 // }); // } // } // // console.log("CHECK FOR ITEM BUTTON ", item, item.querySelector('#approve_product_gallery_item_button_'+idx)) // 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_product_gallery_item_options_wrapper_ele_name){ // var approve_product_gallery_item_options_wrapper_ele = document.querySelector(approve_product_gallery_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_product_gallery_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_product_gallery_item_button_display_style){ // approve_product_gallery_item_button.style.display = approve_product_gallery_item_button_display_style; // } // if (approve_product_gallery_item_button_display_margin){ // approve_product_gallery_item_button.style.margin = approve_product_gallery_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_product_gallery_item_button_to_remove_ele){ // var approve_product_gallery_item_button_to_remove = document.querySelector(approve_product_gallery_item_button_to_remove_ele); // if (approve_product_gallery_item_button_to_remove){ // approve_product_gallery_item_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_product_gallery_item_price) * parseInt(approve_product_gallery_item_qty)) < 1500){ // approve_debug_log("Price is below $1500."); // 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_product_gallery_item_inc_quantity_ele_name // && approve_product_gallery_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) { // // console.log("INSIDE FUNCTION approve_product_gallery_item_activate_increase_and_decrease_buttons ", item, idx, number_of_tries); // number_of_tries++; // // buttons that raise or lower the quantity. // var approve_product_gallery_item_qty_btn_inc = item.querySelector(approve_product_gallery_item_inc_quantity_ele_name); // var approve_product_gallery_item_qty_btn_dec = item.querySelector(approve_product_gallery_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); // } // } // } // init_approve_gallery_page_buttons(approve_gallery_wrapper_check_ele_name); // } // /** // * 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 { // approve_debug_log("APPROVE: No approve_qty_btn_inc or approve_qty_btn_dec found.",1); // } // } // // ****************************************************************** // // ****************************************************************** // // END PRODUCT PAGE // // ****************************************************************** // // ****************************************************************** // // ****************************************************************** // // CART PAGE // // ****************************************************************** // if (window.location.pathname == cart_url_pathname) { // var approve_wrapper_align = ""; // var approve_wrapper_margin = "0px 0px 0px 0px"; // var button_width = ""; // var button_min_width = ""; // var button_max_width = ""; // // ****************************************************************** // // CART PAGE Variable Configuration // // ****************************************************************** // // Element to know we are on the cart page -- ideally a wrapper // var approve_cart_wrapper_ele = ""; // // Element for each line item // var approve_cart_item_ele = ''; // // Total cart price (optional -- watcher for updating the cart) // var approve_cart_total_price_ele = '' // // Item variables // var approve_cart_item_model_ele = ''; // var approve_cart_item_price_ele = ''; // var approve_cart_item_qty_ele = ''; // var approve_cart_sku = ''; // // Insert approve button after this element // var approve_cart_insert_btn_after_ele = ''; // var approve_cart_global_watcher = ''; // // Element for cart item options // var approve_cart_model_option_ele = ''; // // Element for TAX on cart (optional) // var approve_cart_tax_price_ele = ''; // // Element for SHIPPING on cart (optional) // var approve_cart_shipping_price_ele = ''; // // ****************************************************************** // // END CART PAGE Variable Configuration // // ****************************************************************** // // ****************************************************************** // // 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_wrapper_ele){ // var approve_cart_wrapper = document.querySelector(approve_cart_wrapper_ele); // var approve_cart_item = document.querySelectorAll(approve_cart_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_item_model_ele); // var approve_cart_item_qty_wrapper = item.querySelector(approve_cart_item_qty_ele); // var approve_cart_item_price_wrapper = item.querySelector(approve_cart_item_price_ele); // if (approve_cart_sku) var approve_cart_item_sku_wrapper = item.querySelector(approve_cart_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_model_option_ele) { // var options = item.querySelectorAll(approve_cart_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_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_wrapper_align) approve_button_wrapper.style.textAlign = approve_wrapper_align; // if (approve_wrapper_margin) approve_button_wrapper.style.margin = approve_wrapper_margin; // if (button_width) approve_button.style.width = button_width; // if (button_min_width) approve_button.style.minWidth = button_min_width; // if (button_max_width) approve_button.style.maxWidth = button_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 (custom_button_color) approve_button.style.setProperty('--button_color', custom_button_color); // if (custom_button_hover_color) approve_button.style.setProperty('--button_hover_color', custom_button_hover_color); // if (custom_button_height) approve_button.style.setProperty('--button_height', custom_button_height); // if (custom_button_border) approve_button.style.setProperty('--button_border', custom_button_border); // if (custom_button_border_radius) approve_button.style.setProperty('--button_border_radius', custom_button_border_radius); // if (custom_font) approve_button.style.setProperty('--button_font', custom_font); // if (custom_font_color) approve_button.style.setProperty('--button_font_color', custom_font_color); // if (custom_font_size) approve_button.style.setProperty('--button_font_size', custom_font_size); // if (custom_text_transform) approve_button.style.setProperty('--button_text_transform', custom_text_transform); // if (custom_font_hover_color) approve_button.style.setProperty('--button_font_hover_color', custom_font_hover_color); // if (custom_font_weight) approve_button.style.setProperty('--button_font_weight', custom_font_weight); // if (custom_powered_by_color) approve_button.style.setProperty('--button_powered_by_color', custom_powered_by_color); // if (approve_cart_tax_price_ele){ // var approve_tax_price_raw = document.querySelector(approve_cart_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_shipping_price_ele){ // var approve_shipping_price_raw = document.querySelector(approve_cart_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_wrapper_ele){ // approve_initialize_cart(); // if (approve_cart_global_watcher) { // var approve_global_watcher = document.querySelector(approve_cart_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); // } // } // // ****************************************************************** // // ****************************************************************** // // END CART PAGE // // ****************************************************************** // // ****************************************************************** // } // // ****************************************************************** // // ****************************************************************** // // END CART PAGE // // ****************************************************************** // // ****************************************************************** // // IF YOU WANT TO USE A LOG WITH COLORS YOU CAN USE // 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; "; // } // if (approve_debug_mode) console.log("%c"+log,css); // }