// ****************************************************************** // ****************************************************************** // PRODUCT GALLERY WITH BUTTONS // ****************************************************************** // ****************************************************************** var approve_debug_mode = true; // gallery wrapper // INTEGRATION BUTTON STYLING var custom_button_color = "black"; var custom_button_hover_color = ""; var custom_button_height = ""; var custom_button_border = ""; var custom_button_border_radius = "4px"; var custom_background_gradient = ""; var custom_background_gradient_hover = ""; var custom_font = "Tahoma"; 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; var approve_gallery_wrapper_check_ele_name = " .wpsm_panel-group"; 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 // ****************************************************************** // 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 = approve_product_gallery_page_check_ele_name+' .wpsm_panel-body'; // Model for each item relative to the item var approve_product_gallery_item_model_ele_name = ' strong u'; // Price for each item relative to the item var approve_product_gallery_item_price_ele_name = ' strong:nth-child(3)'; // Insert button after relative to the item var approve_product_gallery_item_insert_after_ele_name = ' .maxbutton-finance-contact-us'; // 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); 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) { // 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); console.log(approve_product_gallery_item_model_ele); 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); console.log(approve_product_gallery_item_price_ele); 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; approve_product_gallery_item_button.style.width = "65.5%"; // 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); } if (custom_button_color) approve_product_gallery_item_button.style.setProperty('--button_color', custom_button_color); if (custom_button_hover_color) approve_product_gallery_item_button.style.setProperty('--button_hover_color', custom_button_hover_color); if (custom_button_height) approve_product_gallery_item_button.style.setProperty('--button_height', custom_button_height); if (custom_button_border) approve_product_gallery_item_button.style.setProperty('--button_border', custom_button_border); if (custom_button_border_radius) approve_product_gallery_item_button.style.setProperty('--button_border_radius', custom_button_border_radius); if (custom_font) approve_product_gallery_item_button.style.setProperty('--button_font', custom_font); if (custom_font_color) approve_product_gallery_item_button.style.setProperty('--button_font_color', custom_font_color); if (custom_font_size) approve_product_gallery_item_button.style.setProperty('--button_font_size', custom_font_size); if (custom_text_transform) approve_product_gallery_item_button.style.setProperty('--button_text_transform', custom_text_transform); if (custom_font_hover_color) approve_product_gallery_item_button.style.setProperty('--button_font_hover_color', custom_font_hover_color); if (custom_background_gradient) approve_product_gallery_item_button.style.setProperty('--button_background_gradient', custom_background_gradient); if (custom_background_gradient_hover) approve_product_gallery_item_button.style.setProperty('--button_background_gradient_hover', custom_background_gradient_hover); if (custom_font_weight) approve_product_gallery_item_button.style.setProperty('--button_font_weight', custom_font_weight); if (custom_powered_by_color) approve_product_gallery_item_button.style.setProperty('--button_powered_by_color', custom_powered_by_color); if (custom_powered_by_size) approve_product_gallery_item_button.style.setProperty('--button_powered_by_size', custom_powered_by_size); // 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); var accordian_buttons = document.querySelectorAll('.wpsm_panel-heading'); accordian_buttons.forEach(function (item, index) { item.addEventListener('click',event => { console.log('EVENT LISTENED'); 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); }