'; if(data.quick_view){ addQBTableAtPosition(data.quick_view.el,table); } else{ addQBTableAtPosition(data.el,table); } } } function detectBrowserURLChange(config,callback){ var changeDetector = waioSelectors.variantsChangeSelectorClass; var triggerMethod = "change"; if("undefined" !== typeof config){ if(config.changeSelector){ changeDetector = config.changeSelector; } if(config.triggerMethod){ triggerMethod = config.triggerMethod; } } var oldURL = ""; $(document).on(triggerMethod, changeDetector ,function(e){ var currentURL = window.location.href; if(currentURL != oldURL){ oldURL = currentURL; callback({ urlChanged: true, time: new Date().getTime() },config); } else{ oldURL = currentURL; callback({ urlChanged: false, time: new Date().getTime() },config); } }); } // Quantity Controls functions function quantity_controller(config, callback){ if("undefined"===typeof config)var config={}; null===config&&(config={}); var productId = config.productId || current_product.id; var variantId = config.variantId || selected_variant_id; var collectionId = config.collectionId || collection_id; var custom_message = config.message || null; var qc_variants = []; var qc_products = []; var qc_collections = []; var qc_entireStore = []; for(var iter = 0; iter -1){ tagFound = true; break; } } if(!tagFound) continue; } getAllRelevantQC({ rule: rule, variantId: variantId, productId: productId, collectionId: collectionId, qc_variants: qc_variants, qc_products: qc_products, qc_collections: qc_collections, qc_entireStore: qc_entireStore }, function(results){ log("results",results); var pushData = { variantId: variantId, productId: productId, collectionId: collectionId, customer_group: rule.customer_group, internal_name: rule.internal_name, default_quantity: rule.default_quantity, maximum_quantity: rule.maximum_quantity, minimum_quantity: rule.minimum_quantity, quantity_increment: rule.quantity_increment, applies_to: rule.applies_to, message: custom_message || rule.message, selector_for_individual_variant: rule.selector_for_individual_variant, minimum_quantity_method: rule.minimum_quantity_method }; if(results.type === "variants" && results.status === "approved"){ pushData.variants = rule.variants; qc_variants.push(pushData); } if(results.type === "products" && results.status === "approved"){ pushData.products = rule.products; qc_products.push(pushData); } if(results.type === "collections" && results.status === "approved"){ pushData.collections = rule.collections; qc_collections.push(pushData); } if(results.type === "entire_store" && results.status === "approved"){ pushData.entire_store = true; qc_entireStore.push(pushData); } }); }//end forloop log("qc_variants", qc_variants); log("qc_products", qc_products); log("qc_collections", qc_collections); log("qc_entireStore", qc_entireStore); var variants_final = []; var products_final = {}; var collections_final = {}; var entireStore_final = {}; for(var iter = 0; iter -1){ status = "approved"; } } else{ log("WAIO Warning!","Variant id not found for Quantity controller applies to variants","warning"); } if(productId){ var find = rule.variants.findIndex(function(x){ return x.product_id == productId }); if(find > -1){ status = "approved"; } } break; case "products": if(productId){ var find = rule.products.findIndex(function(x){ return x.id == productId }); if(find > -1){ status = "approved"; } } else{ log("WAIO Warning!","Product id not found for Quantity controller applies to products","warning"); } if(variantId){ try{ for(var iter = 0; iter -1){ status = "approved"; break; } } }catch(e){log(e);} } break; case "collections": if(collectionId){ if(rule.collections.findIndex(function(x){return x.id == collectionId }) > -1){ status = "approved"; } } else if(productId){ WholesaleAIO.collectionsOfProduct(productId, function(collectionIds){ log("collectionIds",collectionIds); for(var iter = 0; iter -1){ status = "approved"; break; } } }); } else if(collection_handle == "all"){ if(qc_collections.length == 0){ status = "approved"; } log("WAIO message: All collections rendered!") } else{ log("WAIO message: Current collection's id not found!","warning") } break; case "entire_store": if(WholesaleAIO.template !== "product"){ if(qc_collections.length === 0 && qc_products.length === 0 && qc_variants.length === 0){ status = "approved"; } } else{ if(qc_variants.length === 0){ status = "approved"; } } break; default: break; } callback({type:rule.applies_to,status:status}); } function quantityControls__cart(config, callback){ if("undefined"===typeof config)var config={}; null===config&&(config={}); var elementSelector = config.elementSelector || "[data-variant-id]"; var variantIdAttribute = config.variantIdAttribute || "data-variant-id"; var productIdAttribute = config.productIdAttribute || "data-product-id"; if($(elementSelector).length > 0){ $(elementSelector).each(function(e,a){ var $variantId = $(this).attr(variantIdAttribute); var $productId = $(this).attr(productIdAttribute); config.variantId = $variantId; if($productId){ config.productId = $productId; } WholesaleAllInOne.quantityController(config, function(e,quantity_controls){ if(e){ log(e); callback(e, null); } else{ callback(null, { quantity_controls: quantity_controls, elementSelector: "["+variantIdAttribute+"='"+$variantId+"']" }); }; }); }); } else{ callback("elementSelector could not found any nodeElement. Config elementSelector for Quantity controller to work on cart template.", null); } } // End Quantity Controls functions WholesaleAllInOne.productTemplate = function(config){ if(config.productId){ WholesaleAIO.productInCollections(config.productId); } if(!WholesaleAIO.template){ WholesaleAIO.template = "product"; } if(WholesaleAIO.template !== "product"){ WholesaleAIO.template = "product"; } discountGroup__product(config); quantityBreak__product(config); } WholesaleAllInOne.cartDiscount = function(config){ preDiscount__cart(config); initCheckout(config); } WholesaleAllInOne.cartDrawer = function(config){ preDiscount__cart(config); initCheckout(config); } WholesaleAllInOne.HCSRPTemplate = function(config){ if(!WholesaleAIO.template){ WholesaleAIO.template = "collection"; } if(WholesaleAIO.template.indexOf(".") > -1){ log("WAIO LOG!", "Your current template is "+WholesaleAIO.template); WholesaleAIO.template = WholesaleAIO.template.split(".")[0]; log("WAIO Warning!", "This is "+WholesaleAIO.template+" template. If it is not, please provide template parameter when initializing the function. Example: WholesaleAllInOne.HCSRPTemplate({template:'collection'})","warning") } discountGroup__hcsrp(config); //quantityBreak__hcsrp(config); } WholesaleAllInOne.relatedProducts = function(config){ WholesaleAIO.template = "collection"; discountGroup__hcsrp(config); //quantityBreak__hcsrp(config); } WholesaleAllInOne.couponCodeForm = function(config){ additionalCouponCodeForm(config); } WholesaleAllInOne.variantChanged = function(config){ detectBrowserURLChange(config, function(result,config){ if("undefined"===typeof config)var config={}; null===config&&(config={}); setTimeout(function(config){ if(result){ if(result.urlChanged){ var oldV = selected_variant_id; selected_variant_id = getOptURLValue("variant"); if(!selected_variant_id || selected_variant_id === null){ selected_variant_id = $(this).closest('form').find('[name="id"]').val(); } if(!selected_variant_id){ log("WAIO Error:","Variant changed, but variant id not found.","warning"); } else{ log("selected_variant_id was changed to from "+oldV,"to "+selected_variant_id); } if("undefined" === typeof config) var config = {}; config.variantId = selected_variant_id; WholesaleAllInOne.productTemplate(config); } } }, config.timeOut || 500, config); }); } WholesaleAllInOne.quickView = function(config){ WholesaleAIO.template = "product"; if(config.productHandle){ WholesaleAIO.getProductByHandle(config.productHandle, function(error,product){ if(product){ current_product = product; // selected_variant_id = product.variants[0].id; WholesaleAIO.productInCollections(product.id);//prepare for collection discount WholesaleAllInOne.productTemplate(config); } if(error){ log("WAIO Warning! ",error,"warning"); } }); } else{ if(config.quickViewButton){ var findHandle = $(config.quickViewButton).closest("a").attr("href"); if(config.quickViewHandleAttr){ findHandle = $(config.quickViewButton).closest("a").attr(config.quickViewHandleAttr); } if(findHandle && findHandle.lenth > 0){ var handle = findHandle; if(findHandle.indexOf("/") > -1){ var handleArray = findHandle.split("/"); handle = handleArray[handleArray.length-1]; if(handle || handle.trim() !== ""){ handle = handle; } else{ handle = handleArray[handleArray.length-2]; } } log("WAIO LOG!", "Handle found for product is "+handle); WholesaleAIO.getProductByHandle(handle, function(error,product){ if(product){ current_product = product; // selected_variant_id = product.variants[0].id; WholesaleAIO.productInCollections(product.id);//prepare for collection discount WholesaleAllInOne.productTemplate(config); } if(error){ log("WAIO Warning! ",error,"warning"); } }); } else{ log("WAIO warning! ","Product handle was not found!", "warning"); WholesaleAllInOne.productTemplate(config); } } else{ log("WAIO log! ","Quick view button's class or id not provided, we cannot get the handle of the product on quick view.","warning"); WholesaleAllInOne.productTemplate(config); } } } WholesaleAIO.getProductByHandle = function(handle, callback){ if(!handle) callback(null); $.ajax({ type: 'GET', url: '/products/'+handle+'.js', dataType: 'json', success: function(product){ callback(null,product); }, error: function(error){ log("WAIO Warning! ","AJAX Error for get product by handle","warning"); callback(error,null); } }); } WholesaleAIO.productInCollections = function(product_id){ if(!product_id) { return "Product id is missing"; } var inCollects = []; if(WholesaleAIO.productInCollects){ try{ var indexOfPC = WholesaleAIO.productInCollects.findIndex(function(x){return x.product_id==product_id}); var pc = -1 0){ try{ var indexOfPC = WholesaleAIO.productInCollects.findIndex(function(x){return x.product_id==product_id}); var pc = -1 0){ for(var iter = 0; iter