if (window.location.host == "cartoliinstruments.com") { var approve_debug_mode = true; approve_debug_log("APPROVE SCRIPT LOADED"); // console.log("PRODUCT PAGE? ", document.querySelector("#maincontent.product-main-full-width .container .product-info-main")); // ****************************************************************** // ****************************************************************** // PRODUCT // ****************************************************************** // ****************************************************************** // ONLY if product page or more specifically NOT cart page and has product info if ((window.location.pathname.indexOf("checkout/cart") == -1 || window.location.pathname.indexOf("checkout/cart/configure") != -1 ) && document.querySelector("#maincontent.product-main-full-width .container .product-info-main") ) { // this is a product page and not a product popup/modal etc function init_approve_product_page_button() { // ****************************************************************** // ****************************************************************** // 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 = '#maincontent.product-main-full-width .container .product-info-main'; var approve_product_wrapper = document.querySelector(approve_product_wrapper_ele); approve_debug_log("APPROVE PRODUCT SCRIPT LOADED"); // Must ensure we are on a product page before attempting to select the remaining elements. // console.log("PRODUCT PAGE? ", approve_product_wrapper); 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+' .product-info-block > div > h1'; var approve_price_ele_name = approve_product_wrapper_ele+' .order-2 .price-box .price-container .final-price .price-wrapper .price'; var approve_qty_ele_name = approve_product_wrapper_ele+' [x-data="initQtyField()"] input[name="qty"] '; // 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+' .order-2 .price-box .price-container'; // 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+' [x-data="initQtyField()"] button:nth-of-type(2)'; var approve_dec_quantity_ele = approve_product_wrapper_ele+' [x-data="initQtyField()"] button:nth-of-type(1)'; // console.log("INC ELEMENT ",document.querySelector(approve_inc_quantity_ele)); // OPTIONS -- All of the following is optional. Options are be fairly custom // Wrapper for the ENTIRE options section var approve_options_wrapper_ele_name = ''; // 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_select_ele_name = ''; // APPROVE BUTTON STYLING var approve_button_display_style = 'inline-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){ approve_debug_log("APPROVE: No initial approve_model_ele found.",1); } // price var approve_price_ele = document.querySelector(approve_price_ele_name); if (!approve_price_ele){ approve_debug_log("APPROVE: No initial approve_price_ele found.",1); } // qty if (approve_qty_ele_name){ var approve_qty_ele = document.querySelector(approve_qty_ele_name); if (!approve_qty_ele){ approve_debug_log("APPROVE: No initial approve_qty_ele found.",1); } } // element to insert after var approve_insert_after_ele = document.querySelector(approve_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 // ****************************************************************** // ****************************************************************** // 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){ approve_debug_log("APPROVE: No dynamic approve_model_ele found.",0,1); return; } var approve_model = approve_model_ele.textContent // price var approve_price_ele = document.querySelector(approve_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_qty_ele_name){ var approve_qty_ele = document.querySelector(approve_qty_ele_name); } if (!approve_qty_ele){ approve_debug_log("APPROVE: No dynamic approve_qty_ele found.",1); 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){ approve_debug_log("APPROVE: No dynamic approve_insert_after_ele found.",0,1); 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){ approve_debug_log("No approve_options_wrapper_ele found."); } else { // TRH - added else to prevent js error when options dont exist var approve_selected_options = approve_options_wrapper_ele.querySelectorAll(approve_select_ele_name); if (!approve_selected_options){ approve_debug_log("No approve_selected_options found."); } approve_selected_options.forEach(function (item, index) { // CONFIGURE OPTIONS HERE }); } } // ****************************************************************** // 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_options_wrapper_ele_name){ var approve_options_wrapper_ele = document.querySelector(approve_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_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.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){ approve_debug_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 { approve_debug_log("APPROVE: approve_product_wrapper not found.",1) } /** * * 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')); var approve_button_max = (document.querySelector(approve_qty_ele_name) ? parseInt(document.querySelector(approve_qty_ele_name).getAttribute('max')) : null) if (!approve_button_max || approve_btn_qty < approve_button_max) { 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 // ****************************************************************** // ****************************************************************** } init_approve_product_page_button(); } // ****************************************************************** // ****************************************************************** // CART // ****************************************************************** // ****************************************************************** // ONLY on cart page // console.log("ON CART PAGE => ", window.location.pathname.indexOf("checkout/cart")); if (window.location.pathname.indexOf("checkout/cart") != -1 && window.location.pathname.indexOf("checkout/cart/configure") == -1 ) { // cart page function init_approve_cart_page_button() { // ****************************************************************** // ****************************************************************** // CART PAGE // ****************************************************************** // ****************************************************************** // ****************************************************************** // CART PAGE Variable Configuration // ****************************************************************** approve_debug_log("APPROVE CART SCRIPT LOADED"); // Element to know we are on the cart page -- ideally a wrapper var approve_cart_wrapper_ele = "#maincontent"; // Element for each line item var approve_cart_item_ele = ' #shopping-cart-table tbody.cart.item tr.item-info'; // Total cart price (optional -- watcher for updating the cart) var approve_cart_total_price_ele = '#cart-totals > div:nth-of-type(5) > div > div:nth-of-type(2)' // Item variables var approve_cart_item_model_ele = 'td:nth-of-type(1) .product-item-name '; var approve_cart_item_price_ele = 'td:nth-of-type(3) .price-excluding-tax .cart-price .price'; var approve_cart_item_qty_ele = 'td:nth-of-type(2) input.qty'; // Insert approve button after this element var approve_cart_insert_btn_after_ele = '#cart-totals > div:nth-of-type(5)'; // Element for TAX on cart (optional) var approve_cart_tax_price_ele = '#cart-totals > div:nth-of-type(4) > div > div > div:nth-of-type(2)'; if (!document.querySelector(approve_cart_tax_price_ele)) { approve_cart_tax_price_ele = ""; } // Element for SHIPPING on cart (optional) var approve_cart_shipping_price_ele = '#cart-totals > div:nth-of-type(3) > div > div:nth-of-type(2)'; var approve_cart_shipping_price_text_ele = '#cart-totals > div:nth-of-type(3) > div > div:nth-of-type(1)'; if (!document.querySelector(approve_cart_shipping_price_ele)) { approve_cart_shipping_price_ele = ""; approve_cart_shipping_price_text_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]. 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); } function approve_initialize_cart(){ if (!approve_cart_item){ approve_debug_log("no cart items found.",1) 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); // check model if (!approve_cart_item_model_wrapper){ approve_debug_log("No approve_cart_item_model_wrapper found.",1); 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(); approve_cart_item.model = encodeURIComponent(approve_cart_item_model); // check price if (!approve_cart_item_price_wrapper){ approve_debug_log("No approve_cart_item_price_wrapper found.",1); 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){ approve_debug_log("No approve_cart_item_qty_wrapper found. Qty is set to 1.",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_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){ approve_debug_log("Element to insert approve button after not found.",1); return; } var approve_button = document.createElement('approve-button'); approve_button.id = "approve_button_id"; approve_button.style.display="block"; approve_button.style.textAlign="center"; approve_button.setAttribute('application-type',"embedded_app"); approve_button.setAttribute('clear-cart-items',"true"); approve_cart_insert_after_ele.after(approve_button); } 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); var approve_shipping_price_text = document.querySelector(approve_cart_shipping_price_text_ele); if (approve_shipping_price && approve_shipping_price > 0){ var approve_cart_shipping_item = {}; approve_cart_shipping_item.model = "Shipping"+(approve_shipping_price_text ? ": "+approve_shipping_price_text.textContent : ""); // console.log("approve_cart_shipping_item.model", approve_cart_shipping_item.model); 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(); var approve_price_watcher = document.querySelector(approve_cart_total_price_ele); // Add observer to price element const approve_price_observer = new MutationObserver(function() { // console.log("PRICE CHANGE OBSERVED"); setTimeout(() => { init_approve_cart_page_button(); },100); }); if (approve_price_watcher) { approve_price_observer.observe(approve_price_watcher,{subtree: true, childList: true}); } } else { approve_debug_log("No approve_cart_wrapper found."); } // ****************************************************************** // ****************************************************************** // END CART PAGE // ****************************************************************** // ****************************************************************** } init_approve_cart_page_button(); } // ****************************************************************** // ****************************************************************** // MINI CART // ****************************************************************** // ****************************************************************** var approve_mini_cart_modal_ele_name = "#cart-drawer"; var approve_mini_cart_modal_ele = document.querySelector(approve_mini_cart_modal_ele_name); var init_count = 0; // console.log("MENU CART", approve_mini_cart_modal_ele); if (approve_mini_cart_modal_ele) { var approve_mini_cart_content_check_ele_name = 'div[role="dialog"] > div:nth-of-type(2)'; var approve_mini_cart_watcher = approve_mini_cart_modal_ele; const approve_mini_cart_observer = new MutationObserver(function() { init_count++; // check for dialog being opened and have cart items var approve_mini_cart_content_check = approve_mini_cart_modal_ele.querySelector(approve_mini_cart_content_check_ele_name); if (approve_mini_cart_content_check && (!approve_mini_cart_content_check.style.display || approve_mini_cart_content_check.style.display != "none" ) && !approve_mini_cart_content_check.querySelector("#approve_mini_cart_button_id") ) { var approve_mini_cart_wrapper_ele_name = approve_mini_cart_modal_ele_name+" "+approve_mini_cart_content_check_ele_name+' > div:nth-of-type(2) '; // mini cart is open and button has not been added so we need to initialize the mini cart init_approve_mini_cart_button(approve_mini_cart_wrapper_ele_name); // add in watcher for the mini cart content var approve_mini_cart_content_changed = true; var approve_mini_cart_content_watcher = document.querySelector(approve_mini_cart_wrapper_ele_name); var approve_mini_cart_content_observer = new MutationObserver(function() { if (approve_mini_cart_content_changed && approve_mini_cart_content_check.querySelector("#approve_mini_cart_button_id") ) { approve_mini_cart_content_check.querySelector("#approve_mini_cart_button_id").remove(); } approve_mini_cart_content_changed = false; }); approve_mini_cart_content_observer.observe(approve_mini_cart_content_watcher,{subtree: true, childList: true, attributes: true}); } }); approve_mini_cart_observer.observe(approve_mini_cart_watcher,{subtree: true, childList: true, attributes: true}); function init_approve_mini_cart_button(mini_cart_wrapper_ele_name) { // ****************************************************************** // ****************************************************************** // MINI CART BUTTON // ****************************************************************** // ****************************************************************** approve_debug_log("APPROVE MINI CART SCRIPT LOADED"); // ****************************************************************** // MINI CART Variable Configuration // ****************************************************************** // Element to know we are on the cart page -- ideally a wrapper var approve_mini_cart_wrapper_ele = mini_cart_wrapper_ele_name; // Element for each line item var approve_mini_cart_item_ele = approve_mini_cart_wrapper_ele+' > div:nth-of-type(1) > div '; // Total cart price (optional -- watcher for updating the cart) var approve_mini_cart_total_price_ele = 'div:nth-of-type(3) > div:nth-of-type(3) > p > span[x-html="cart.grand_total"]' // Item variables var approve_mini_cart_item_model_ele = 'div > div:nth-of-type(1) > a > p > span[x-html="item.product_name"]'; var approve_mini_cart_item_price_ele = 'div > div:nth-of-type(2) > p > .price-excluding-tax > .price-excluding-tax .minicart-price .price '; var approve_mini_cart_item_qty_ele = 'div > div:nth-of-type(2) > div > input[x-model="item.qty"]'; // Insert approve button after this element var approve_mini_cart_insert_btn_after_ele = 'div:nth-of-type(3) > div:nth-of-type(3)'; // Element for TAX on cart (optional) var approve_mini_cart_tax_price_ele = ''; // Element for SHIPPING on cart (optional) var approve_mini_cart_shipping_price_ele = 'div:nth-of-type(3) > div:nth-of-type(2) '; var approve_mini_cart_shipping_price_text_ele = ''; if (approve_mini_cart_shipping_price_ele && !document.querySelector(approve_mini_cart_shipping_price_ele) ) { approve_mini_cart_shipping_price_ele = ""; approve_mini_cart_shipping_price_text_ele = ""; } // ****************************************************************** // END MINI CART Variable Configuration // ****************************************************************** // ****************************************************************** // MINI CART 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]. if (approve_mini_cart_wrapper_ele){ var approve_mini_cart_wrapper = (approve_mini_cart_wrapper_ele ? document.querySelector(approve_mini_cart_wrapper_ele) : null); var approve_mini_cart_item = (approve_mini_cart_item_ele ? document.querySelectorAll(approve_mini_cart_item_ele) : null); } // console.log("MINI CART ITEMS => ", approve_mini_cart_item); function approve_initialize_mini_cart(){ if (!approve_mini_cart_item){ approve_debug_log("no cart items found.",1); return; } var approve_mini_cart_items_array = []; approve_mini_cart_item.forEach(function (item, index) { var approve_mini_cart_item = {}; // Find model, price, and qty for each cart item. var approve_mini_cart_item_model_wrapper = item.querySelector(approve_mini_cart_item_model_ele); var approve_mini_cart_item_qty_wrapper = item.querySelector(approve_mini_cart_item_qty_ele); var approve_mini_cart_item_price_wrapper = item.querySelector(approve_mini_cart_item_price_ele); // check model if (!approve_mini_cart_item_model_wrapper){ approve_debug_log("No approve_mini_cart_item_model_wrapper found.",1); return; } // var approve_cart_item_model_wrapper = approve_cart_item_model_wrapper; // set and encode cart item model. var approve_mini_cart_item_model = approve_mini_cart_item_model_wrapper.textContent.trim(); approve_mini_cart_item.model = encodeURIComponent(approve_mini_cart_item_model); // check price if (!approve_mini_cart_item_price_wrapper){ approve_debug_log("No approve_mini_cart_item_price_wrapper found.",1); return; } var approve_mini_cart_price = parseFloat(approve_mini_cart_item_price_wrapper.innerHTML.replace(/[^0-9.]/g, '')); // set price approve_mini_cart_item.price = approve_mini_cart_price; // check qty var approve_mini_cart_qty = 1; // set default val if (!approve_mini_cart_item_qty_wrapper){ approve_debug_log("No approve_mini_cart_item_qty_wrapper found. Qty is set to 1.",1); } else { approve_mini_cart_qty = approve_mini_cart_item_qty_wrapper.value; } approve_mini_cart_qty = parseInt(approve_mini_cart_qty); // set qty approve_mini_cart_item.qty = parseInt(approve_mini_cart_qty); approve_mini_cart_item.type = "new_product"; approve_mini_cart_items_array.push(approve_mini_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_mini_cart_button = document.getElementById('approve_mini_cart_button_id'); if (!approve_mini_cart_button){ // Find the element where we want to insert our button. var approve_mini_cart_insert_after_ele = document.querySelector(approve_mini_cart_insert_btn_after_ele); if(!approve_mini_cart_insert_after_ele){ approve_debug_log("Element to insert approve mini cart button after not found."); return; } var approve_mini_cart_button = document.createElement('approve-button'); approve_mini_cart_button.id = "approve_mini_cart_button_id"; approve_mini_cart_button.style.display="block"; approve_mini_cart_button.style.textAlign="center"; approve_mini_cart_button.setAttribute('application-type',"embedded_app"); approve_mini_cart_button.setAttribute('clear-cart-items',"true"); approve_mini_cart_insert_after_ele.after(approve_mini_cart_button); } if (approve_mini_cart_tax_price_ele){ var approve_mini_cart_tax_price_raw = document.querySelector(approve_mini_cart_tax_price_ele); var approve_mini_cart_tax_price = approve_mini_cart_tax_price_raw.textContent.replace(/[^0-9.]/g, ''); approve_mini_cart_tax_price = parseFloat(approve_mini_cart_tax_price).toFixed(2); if (approve_mini_cart_tax_price && approve_mini_cart_tax_price > 0){ var approve_mini_cart_shipping_item = {}; approve_mini_cart_shipping_item.model = "Tax"; approve_mini_cart_shipping_item.price = approve_mini_cart_tax_price; approve_mini_cart_shipping_item.qty = 1; approve_mini_cart_shipping_item.type = "new_product"; approve_mini_cart_items_array.push(approve_mini_cart_shipping_item); } } if (approve_mini_cart_shipping_price_ele){ var approve_mini_cart_shipping_price_raw = document.querySelector(approve_mini_cart_shipping_price_ele); var approve_mini_cart_shipping_price = approve_mini_cart_shipping_price_raw.textContent.replace(/[^0-9.]/g, ''); approve_mini_cart_shipping_price = parseFloat(approve_mini_cart_shipping_price).toFixed(2); var approve_mini_cart_shipping_price_text = (approve_mini_cart_shipping_price_text_ele ? document.querySelector(approve_mini_cart_shipping_price_text_ele) : ""); if (approve_mini_cart_shipping_price && approve_mini_cart_shipping_price > 0){ var approve_mini_cart_shipping_item = {}; approve_mini_cart_shipping_item.model = "Shipping"+(approve_mini_cart_shipping_price_text ? ": "+approve_mini_cart_shipping_price_text.textContent : ""); // console.log("approve_mini_cart_shipping_item.model", approve_mini_cart_shipping_item.model); approve_mini_cart_shipping_item.price = approve_mini_cart_shipping_price; approve_mini_cart_shipping_item.qty = 1; approve_mini_cart_shipping_item.type = "new_product"; approve_mini_cart_items_array.push(approve_mini_cart_shipping_item); } } approve_mini_cart_button.setAttribute('items',JSON.stringify(approve_mini_cart_items_array)); } if (approve_mini_cart_wrapper_ele && approve_mini_cart_total_price_ele ){ approve_initialize_mini_cart(); // var approve_mini_cart_price_watcher = document.querySelector(approve_mini_cart_total_price_ele); // Add observer to price element // const approve_mini_cart_price_observer = new MutationObserver(function() { // // console.log("PRICE CHANGE OBSERVED"); // setTimeout(() => { // init_approve_mini_cart_button(); // },100); // }); // approve_mini_cart_price_observer.observe(approve_mini_cart_price_watcher,{subtree: true, childList: true}); } else { approve_debug_log("No approve_mini_cart_wrapper or approve_mini_cart_total_price_ele found.",1); } // ****************************************************************** // ****************************************************************** // END CART PAGE // ****************************************************************** // ****************************************************************** } } // ****************************************************************** // ****************************************************************** // PRODUCT GALLERY WITH BUTTONS // ****************************************************************** // ****************************************************************** var approve_gallery_wrapper_check_ele_name = ".container .columns .column.main #m-navigation-product-list-wrapper section .products > div "; // console.log("LOOKING FOR GALLERY CONTENT ", document.querySelector(approve_gallery_wrapper_check_ele_name)); // ONLY IF NOT on cart page and has gallery list of products if (window.location.pathname.indexOf("checkout/cart") == -1 && document.querySelector(approve_gallery_wrapper_check_ele_name) ) { function init_approve_gallery_page_buttons(gallery_wrapper_ele_name) { // approve_debug_log("HAS GALLERY ITEMS!! ",0,1); // ".container .columns .column.main #m-navigation-product-list-wrapper section .products > div form.product-item.product_addtocart_form" approve_debug_log("APPROVE PRODUCT GALLERY SCRIPT LOADED"); // ****************************************************************** // PRODUCT GALLERY Variable Configuration // ****************************************************************** // 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 var approve_product_gallery_item_ele = approve_product_gallery_page_check_ele_name+' form.product-item.product_addtocart_form'; // Price for each item var approve_product_gallery_item_model_ele_name = ' div:nth-of-type(1) > div:nth-of-type(1) > div:nth-of-type(1) > div:nth-of-type(1) > div:nth-of-type(1) > a'; var approve_product_gallery_item_price_ele_name = ' div:nth-of-type(1) > div:nth-of-type(1) > div:nth-of-type(2) > div:nth-of-type(1) > span:nth-of-type(1) '; // Insert button after var approve_product_gallery_item_insert_after_ele_name = ' div:nth-of-type(1) > div:nth-of-type(1) > div:nth-of-type(2) > div:nth-of-type(1) > div:nth-of-type(2)'; var approve_product_gallery_item_qty_ele_name = 'div:nth-of-type(1) > div:nth-of-type(1) > div:nth-of-type(1) > div:nth-of-type(1) > div:nth-of-type(4) > div:nth-of-type(2) > input.qty-default'; var approve_product_gallery_item_inc_quantity_ele_name = 'div:nth-of-type(1) > div:nth-of-type(1) > div:nth-of-type(1) > div:nth-of-type(1) > div:nth-of-type(4) > div:nth-of-type(2) > input[value="+"]'; var approve_product_gallery_item_dec_quantity_ele_name = 'div:nth-of-type(1) > div:nth-of-type(1) > div:nth-of-type(1) > div:nth-of-type(1) > div:nth-of-type(4) > div:nth-of-type(2) > input[value="-"]'; var approve_product_gallery_item_options_wrapper_ele_name = ''; var approve_product_gallery_item_select_ele_name = ''; 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); if (approve_product_gallery_page_check){ var approve_gallery_product_array = document.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) { 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; } // at this point we should have /* approve_product_gallery_item_model approve_product_gallery_item_price approve_product_gallery_item_qty and approve_product_gallery_item_insert_after_ele // need to add possible select options and inc dec buttons */ // 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_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; 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)) < 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_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); } } // 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); }