$(document).ready(function () { if ($('[new-arrivals]').length > 0) { $('[new-arrivals]').each(function () { getProductsAndAppend("/data/new-arrivals.json", $(this)); }); } if ($('[trending-products]').length > 0) { $('[trending-products]').each(function () { getProductsAndAppend("/data/trending.json", $(this)); }); } if ($('[discounted-products]').length > 0) { $('[discounted-products]').each(function () { getProductsAndAppend("/data/on-discount.json", $(this)); }); } if ($('[category-products]').length > 0) { $('[category-products]').each(function () { getProductsAndAppend("/data/products.json?facet=false&ajax=true&categoryId=" + $(this).attr("category"), $(this)); }); } if ($('[brand-products]').length > 0) { $('[brand-products]').each(function () { getProductsAndAppend("/data/products.json?facet=false&ajax=true&brandId=" + $(this).attr("brand"), $(this)); }); } if ($('[collection-products]').length > 0) { $('[collection-products]').each(function () { getProductsAndAppend("/data/products.json?facet=false&ajax=true&collectionId=" + $(this).attr("collection"), $(this)); }); } if ($('[recently-viewed-products]').length > 0) { $('[recently-viewed-products]').each(function () { var productIds = readCookie('recentlyViewed') && readCookie('recentlyViewed').length > 0 ? JSON.parse(decodeURIComponent(readCookie('recentlyViewed'))).productsArray : []; if(productIds.length > 0) getProductsAndAppend("/data/products.json?facet=false&productIds=" + productIds, $(this)); else $(this).remove(); }); } function getProductsAndAppend(url, strongThis) { if ($(strongThis).attr("limit") && $(strongThis).attr("limit").length > 0) { if (url.indexOf("?") > -1) url += "&"; else url += "?"; url += "limit=" + $(strongThis).attr("limit"); } if($(strongThis).attr("template") && $(strongThis).attr("template").length > 0) { if (url.indexOf("?") > -1) url += "&"; else url += "?"; url += "template=" + $(strongThis).attr("template") } $.ajax({ type: 'GET', url: url, success: function (data) { var productCards = $(data).find('.products'); $(productCards).addClass("dynamic-product"); if ($(strongThis)[0].hasAttribute('data-slick')) { productCards = $(productCards).removeClass(["col-lg-3", "col-lg-4", "col-lg-6", "col-lg-12","col-md-3", "col-md-4", "col-md-6", "col-md-12","col-sm-3", "col-sm-4", "col-sm-6", "col-sm-12","col-xs-3", "col-xs-4", "col-xs-6", "col-xs-12"].join(" ")); $(productCards).addClass("dynamic-product-item") } else { if ($(strongThis).attr("medium-device")) { productCards = $(productCards).removeClass(["col-md-3", "col-md-4", "col-md-6", "col-md-12"].join(" ")).addClass($(strongThis).attr("medium-device")); } if ($(strongThis).attr("small-device")) { productCards = $(productCards).removeClass(["col-sm-3", "col-sm-4", "col-sm-6", "col-sm-12"].join(" ")).addClass($(strongThis).attr("small-device")); } } var prTemplate = window.store.settings.website && window.store.settings.website.templates ? window.store.settings.website.templates.product : "default"; if($(strongThis).attr("template") && $(strongThis).attr("template").length > 0) { prTemplate = $(strongThis).attr("template"); } var productCSS = $(data).find('#' + prTemplate); $(strongThis).find('.template').remove(); $(strongThis).find('[product-loader]').remove(); $(productCSS).insertBefore(strongThis); $(strongThis).append(productCards); $(strongThis).addClass(prTemplate+"-grid"); if ($(strongThis)[0].hasAttribute('data-slick')) { $.getScript("//cdn.jsdelivr.net/gh/kenwheeler/slick@1.8.1/slick/slick.min.js", function () { $(strongThis).slick(); }); } $(strongThis).addClass('product-list-initialized'); }, dataType: "html" }); } function readCookie(name) { var nameEQ = name + "="; var ca = document.cookie.split(';'); for (var i = 0; i < ca.length; i++) { var c = ca[i]; while (c.charAt(0) == ' ') c = c.substring(1, c.length); if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length); } return null; } });