"+noConnectionTitle+" "); jqAthena(".athena-autocomplete").after("
"); jqAthena('.athena-search-autocomplete').addClass(layoutType); /* Show Autocomplete */ function showAutocompleteBox() { hideNoConnectionBox(); jqAthena(".athena-autocomplete").css({'display': 'block'}); searchForm.addClass('arrow'); } /* Hide Autocomplete */ function hideAutocompleteBox() { jqAthena(".athena-autocomplete").css({'display': 'none'}); } /* Show First Click Autocomplete */ function showFirstClickAutocompleteBox() { hideNoConnectionBox(); jqAthena(".athena-first-click").css({'display': 'block'}); searchForm.addClass('arrow'); } /* Hide First Click Autocomplete */ function hideFirstClickAutocompleteBox() { jqAthena(".athena-first-click").css({'display': 'none'}); } /* Show No Connection Box */ function showNoConnectionBox() { noConnection = true; jqAthena('.athena-search-autocomplete').hide(); jqAthena('#athena-no-connection').show(); searchForm.addClass('arrow'); } /* Hide No Connection Box */ function hideNoConnectionBox() { jqAthena('#athena-no-connection').hide(); searchForm.removeClass('arrow'); } /* Show "No Result" Section in Autocomplete */ function showNoResult(value) { jqAthena(".athena-no-result").show(); jqAthena("#athena-no-result-query").html('"' + value + '"'); } /* Hide "No Result" Section in Autocomplete */ function hideNoResult() { jqAthena(".athena-no-result").hide(); } /* Main Search Function */ var searchForClickProduct = ''; var currentRequest = null; jqAthena.ajax({ type: 'POST', data: {'token': websiteToken}, url: searchTabsUrl, cache: true, success: function (searchTabs) { if (searchTabs.data.length > 0) { jqAthena('
').prependTo(".athena-autocomplete"); } jqAthena.each(searchTabs.data, function (i, item) { if (!item.checked) { jqAthena('
'+item.title+'
').appendTo(".athena-search-tabs"); }else{ jqAthena('
'+item.title+'
').appendTo(".athena-search-tabs"); } }); } }); jqAthena(".main-header").on('change', '.athena-tabs-radio', function (e) { jqAthena(".athena-tabs-radio").prop('checked', false); jqAthena("."+this.classList[1]).prop('checked', true); jqAthena(".athena-categories-tabs").remove(); var checked = jqAthena(".athena-tabs-radio:checked").val(); if (checked != 'all') { var checkedArray = checked.split("_"); jqAthena.each(checkedArray, function (i, item) { var catId = i+1; jqAthena('').appendTo(".athena-search-tabs"); }); } search(); }); jqAthena('
').appendTo(".athena-autocomplete"); jqAthena('
').appendTo(".athena-wrapper"); jqAthena('
' + sorryTitle + '
').appendTo(".athena-first-click"); jqAthena('
').appendTo(".athena-first-click"); jqAthena('
').appendTo(".athena-wrapper-fc"); /* Function for first click */ function firstClick() { if (showAthenaLoader) { showNoConnectionBox(); return; } currentFocus = -1; jqAthena.get(athenaAutocompleteFirstClickUrl, { 'token': websiteToken, 'customer_group_id': customerGroupId }, function (data) { if (typeof data.data.results.product !== 'undefined' && typeof data.data.results.product.items !== 'undefined' && data.data.results.product.items.length) { let dataItems = data.data.results.product.items; let listId = "first_click"; let listName = "First Click"; let items = getAnalyticsData(dataItems, listId, listName); if (typeof window.dataLayer !== "undefined") { window.dataLayer.push({ecommerce: null}); window.dataLayer.push({ event: "view_item_list", ecommerce: { item_list_id: listId, item_list_name: listName, items: items } }); } } noConnection = false; var i_none = 0; jqAthena(".athena-ac-sec").remove(); if (jqAthena(".first-athena-flex-first-click").length === 0) { jqAthena('
').appendTo(".athena-flex"); } if (jqAthena(".second-athena-flex-first-click").length === 0) { jqAthena('
').appendTo(".athena-flex"); } jqAthena.each(data.data, function (i, item) { jqAthena.each(item, function (key, value) { var trackSelectorClass = ''; if (value.items) { i_none = 1; if (key === 'product') { trackSelectorClass = 'athena-product-item'; jqAthena('
').prependTo(".first-athena-flex-first-click"); } else { if(value.items.length) { jqAthena('
').appendTo(".second-athena-flex-first-click"); } } jqAthena('
' + value.title + ' ').appendTo(".athena-ac-section-" + key); } jqAthena.each(value.items, function (n, element) { var item_id_selector = key + element.id; if (element.image) { jqAthena('
' + element.name + ' ').appendTo(".athena-flex-fc .athena-ac-section-" + key); }else{ jqAthena('
' + element.name + ' ').appendTo(".athena-flex-fc .athena-ac-section-" + key); } if(key === 'product'){ if( element.price['custom_field_prices'] !== null ) { if( typeof element.price['custom_field_prices'].loyalty_price !== 'undefined'){ jqAthena(' ' + element.price.regular_price_with_currency + ' '+ element.price.loyalty_price_with_currency +' ').appendTo(".athena-el-2-" + item_id_selector); } else if(typeof element.price['custom_field_prices'].special_price !== 'undefined'){ jqAthena(' ' + element.price.regular_price_with_currency + ' ' + element.price.special_price_with_currency + ' ').appendTo(".athena-el-2-" + item_id_selector); } else { jqAthena(' ' + element.price.regular_price_with_currency + ' ').appendTo(".athena-el-2-" + item_id_selector); } } else { if(element.price.loyalty_price_with_currency !== null) { jqAthena(' ' + element.price.regular_price_with_currency + ' '+ element.price.loyalty_price_with_currency +' ').appendTo(".athena-el-2-" + item_id_selector); } else if(element.price.special_price_with_currency !== null){ jqAthena(' ' + element.price.regular_price_with_currency + ' ' + element.price.special_price_with_currency + ' ').appendTo(".athena-el-2-" + item_id_selector); } else { jqAthena(' ' + element.price.regular_price_with_currency + ' ').appendTo(".athena-el-2-" + item_id_selector); } } } }); }); if (i === 'banners') { if(item.top && !jqAthena('.athena-first-click .banner-top').length){ jqAthena.each(item.top, function (index, element) { jqAthena('').prependTo(".athena-first-click"); jqAthena(element.html).prependTo(".athena-first-click .banner-top"); }); } if(item.bottom && !jqAthena('.athena-first-click .banner-bottom').length){ jqAthena.each(item.bottom, function (index, element) { jqAthena('').appendTo(".athena-first-click"); jqAthena(element.html).prependTo(".athena-first-click .banner-bottom"); }); } } }); jqAthena("#numRes").val(i_none); if (i_none === 1) { showFirstClickAutocompleteBox(); hideNoResult(); } else { hideFirstClickAutocompleteBox(); searchForm.removeClass('arrow'); } hideAutocompleteBox(); }).fail(function(data) { showNoConnectionBox(); }); } /* Main search function for Autocomplete */ function search() { if (showAthenaLoader) { showNoConnectionBox(); return; } currentFocus = -1; let val = (jqAthena('#search').val().replace(/(<([^>]+)>)/ig,"")).trim(); if (typeof (val) == "undefined" || val.length === 0) { if (jqAthena(".athena-flex-fc").html().length > 0) { showFirstClickAutocompleteBox(); hideNoResult(); hideAutocompleteBox(); if (currentRequest != null) { currentRequest.abort(); } } else { firstClick(); } } else { var res = val.replace('<>', ''); searchForClickProduct += res + ','; var searchTab = jqAthena(".athena-tabs-radio:checked").val(); currentRequest = jqAthena.ajax( { type: 'GET', data: { 'token': websiteToken, 'q': val, 'customer_group_id': customerGroupId, 'autocomplete_tab': searchTab, 'customer': getCookie('athena_customer_cookie'), 'athenaUid': localStorage.getItem("_athena-uid") }, url: athenaAutocompleteUrl, cache: true, global: false, success: function (data) { if (typeof data.data.results.product !== 'undefined' && typeof data.data.results.product.items !== 'undefined' && data.data.results.product.items.length) { let dataItems = data.data.results.product.items; let listId = "search_autocomplete"; let listName = "Search autocomplete"; let items = getAnalyticsData(dataItems, listId, listName); if (typeof window.dataLayer !== "undefined") { window.dataLayer.push({ecommerce: null}); window.dataLayer.push({ event: "view_item_list", ecommerce: { item_list_id: listId, item_list_name: listName, items: items } }); } } noConnection = false; var i_none = 0; var onlyProduct = 0; jqAthena(".athena-flex-ac").empty(); jqAthena(".athena-ac-section-banner").remove(); jqAthena(".athena-banner-section-banner_top").remove(); jqAthena(".athena-banner-section-banner_bottom").remove(); jqAthena.each(data.data, function (i, item) { if(i !== 'true_autocomplete_keyword' && i !== 'zero_search_results_message') { jqAthena.each(item, function (key, value) { var trackSelectorClass = ''; if (key === 'product') { trackSelectorClass = 'athena-product-item'; if (!value.items) { jqAthena('.athena-search-autocomplete:first-of-type').addClass('no-product-content'); } else { jqAthena('.athena-search-autocomplete:first-of-type').removeClass('no-product-content'); } } if (value.items) { i_none = 1; if (key === 'product') { if (onlyProduct != 2) { onlyProduct = 1; } if(layoutType == 'athena-grid'){ jqAthena('').prependTo(".athena-flex-ac"); }else{ jqAthena('').appendTo(".athena-flex-ac"); } } else { if(value.items.length) { onlyProduct = 2; jqAthena('').appendTo(".athena-flex-ac"); } } jqAthena('
' + value.title + '
').appendTo(".athena-flex-ac .athena-ac-section-" + key); } jqAthena.each(value.items, function (n, element) { var item_id_selector = key + element.id; if (element.image) { jqAthena('
' + element.highlighted_name + ' ').appendTo(".athena-flex-ac .athena-ac-section-" + key); }else{ jqAthena('
' + element.highlighted_name + ' ').appendTo(".athena-flex-ac .athena-ac-section-" + key); } if(key === 'product'){ if( element.price['custom_field_prices'] !== null ) { if(typeof element.price['custom_field_prices'].loyalty_price !== 'undefined'){ jqAthena('
' + element.price.regular_price_with_currency + ' '+ element.price.loyalty_price_with_currency +' ').appendTo(".athena-autocomplete .athena-el-2-" + item_id_selector); } else if(typeof element.price['custom_field_prices'].special_price !== 'undefined' && element.price['custom_field_prices'].special_price !== null){ jqAthena(' ' + element.price.regular_price_with_currency + ' ' + element.price.special_price_with_currency + ' ').appendTo(".athena-autocomplete .athena-el-2-" + item_id_selector); } else { jqAthena(' ' + element.price.regular_price_with_currency + ' ').appendTo(".athena-autocomplete .athena-el-2-" + item_id_selector); } } else { if(element.price.loyalty_price_with_currency !== null) { jqAthena(' ' + element.price.regular_price_with_currency + ' '+ element.price.loyalty_price_with_currency +' ').appendTo(".athena-autocomplete .athena-el-2-" + item_id_selector); } else if(element.price.special_price_with_currency !== null){ jqAthena(' ' + element.price.regular_price_with_currency + ' ' + element.price.special_price_with_currency + ' ').appendTo(".athena-autocomplete .athena-el-2-" + item_id_selector); } else { jqAthena(' ' + element.price.regular_price_with_currency + ' ').appendTo(".athena-autocomplete .athena-el-2-" + item_id_selector); } } } }); }); if (i === 'banners') { if(item.top && !jqAthena('.athena-autocomplete .banner-top').length){ jqAthena.each(item.top, function (index, element) { jqAthena('').prependTo(".athena-autocomplete"); jqAthena(element.html).prependTo(".athena-autocomplete .banner-top"); }); } if(item.bottom && !jqAthena('.athena-autocomplete .banner-bottom').length){ jqAthena.each(item.bottom, function (index, element) { jqAthena('').appendTo(".athena-autocomplete"); jqAthena(element.html).prependTo(".athena-autocomplete .banner-bottom"); }); } } if (i === 'tabs') { jqAthena.each(item.count, function (n, element) { if (element === 0) { jqAthena(".athena-tabs-category-"+n).hide(); } else { jqAthena(".athena-tabs-category-"+n).show(); } }); } }else if(i === 'zero_search_results_message'){ i_none = 0; } }); var maximumData = null; jqAthena('#search_autocomplete .athena-flex .athena').each(function() { var valueData = jqAthena(this).attr('data-order'); maximumData = (valueData > maximumData) ? valueData : maximumData; }); jqAthena('#search_autocomplete .athena-flex .athena').each(function() { if(maximumData == jqAthena(this).attr('data-order')){ jqAthena(this).removeClass('last-search-result') jqAthena(this).addClass('last-search-result') } }); if (onlyProduct == 1) { jqAthena('.athena-search-autocomplete:first-of-type').addClass('only-product'); jqAthena('#search_autocomplete').addClass('only-product'); jqAthena(".athena-search-tabs").addClass('only-product-tabs'); }else{ jqAthena('.athena-search-autocomplete:first-of-type').removeClass('only-product'); jqAthena('#search_autocomplete').removeClass('only-product'); jqAthena(".athena-search-tabs").removeClass('only-product-tabs'); } if (i_none === 1) { showAutocompleteBox(); hideFirstClickAutocompleteBox(); hideNoResult(); } else { hideAutocompleteBox(); showFirstClickAutocompleteBox(); showNoResult(val); } jqAthena("#numRes").val(i_none); }, error: function(data) { if (data.status !== 0) { showFirstClickAutocompleteBox(); showNoResult(val); } } }); } } /* Delay Function */ function delay(callback, ms) { let timer = 0; return function () { let context = this, args = arguments; clearTimeout(timer); timer = setTimeout(function () { callback.apply(context, args); }, ms || 0); }; } /* Event KeyUp */ jqAthena('#search').keyup(delay(function (e) { if (e.keyCode == 13 || e.keyCode == 37 || e.keyCode == 38 || e.keyCode == 39 || e.keyCode == 40) { if (e.keyCode == 13) { if (currentRequest != null) { currentRequest.abort(); } hideAutocompleteBox(); hideFirstClickAutocompleteBox(); hideNoConnectionBox(); } return false; } search(); }, 500)); /* Disable on Keyword Enter click on autocomplete items */ jqAthena(window).keydown(function (e) { if (e.keyCode == 13) { jqAthena("#currentFocus").val(-1); } }); /* Get Cookie by key */ function getCookie(key) { var keyValue = document.cookie.match('(^|;) ?' + key + '=([^;]*)(;|$)'); return keyValue ? keyValue[2] : null; } /* Search History */ function searchHistory(productId) { var date = Date.now() + 86400; var searchHistory = localStorage.getItem('athenaSearchHistory'); if (searchHistory) { searchHistory = JSON.parse(searchHistory); if (searchHistory[productId] && searchHistory[productId]['token'] === websiteToken) { searchHistory[productId]['token'] = websiteToken; searchHistory[productId]['expiration'] = date; searchHistory[productId]['oid'] = productId; localStorage.setItem('athenaSearchHistory', JSON.stringify(searchHistory)); } else { searchHistory[productId] = {}; searchHistory[productId]['token'] = websiteToken; searchHistory[productId]['expiration'] = date; searchHistory[productId]['oid'] = productId; localStorage.setItem('athenaSearchHistory', JSON.stringify(searchHistory)); } } else { const searchHistory = {}; searchHistory[productId] = {}; searchHistory[productId]['token'] = websiteToken; searchHistory[productId]['expiration'] = date; searchHistory[productId]['oid'] = productId; localStorage.setItem('athenaSearchHistory', JSON.stringify(searchHistory)); } return date; } /* Get User Token */ function userToken() { var token = getCookie("_athena"); if (!token) { token = 'anonymous-'+Math.random().toString(36).substr(2, 9)+'-'+Math.random().toString(26).substr(2, 9); jqAthena.cookie("_athena", token, { path: '/' }); } return token; } /* Get User Unique ID */ function userUniqueId() { var token = localStorage.getItem("_athena-uid"); if (!token) { token = Math.random().toString(36).substr(2, 9) + '-' + Math.random().toString(26).substr(2, 9) + '-' + Math.random().toString(26).substr(2, 9) + '-' + Math.random().toString(26).substr(2, 9); localStorage.setItem("_athena-uid", token); } return token; } /* Send Clicked Product to Athena */ function productClick(e, type) { var currentTarget = e; var searchKeywords = ""; if (typeof currentTarget.dataset.elementId != 'undefined') { var productId = currentTarget.dataset.elementId; var productSku = currentTarget.dataset.elementSku; var productElement = jqAthena("#product"+productId); searchKeywords = "First Click,"; searchHistory(productId); if (productElement.length) { if (typeof productElement[0].dataset.terms !== 'undefined') { searchKeywords = productElement[0].dataset.terms; } } if (productSku && window.dataLayer !== 'undefined') { let item; for (let i = 0; i < window.dataLayer.length; i++) { let currentLayer = window.dataLayer[i]; if (currentLayer.event === 'view_item_list' && typeof currentLayer.ecommerce !== 'undefined' && typeof currentLayer.ecommerce.items !== 'undefined') { item = currentLayer.ecommerce.items.find(currentItem => currentItem.item_id === productSku); if (typeof item !== 'undefined') { let listId = "first_click"; let listName = "First Click"; if (!searchKeywords.includes('First Click,')) { listId = "search_autocomplete"; listName = "Search autocomplete"; } item.item_list_id = listId; item.item_list_name = listName; window.dataLayer.push({ecommerce: null}); window.dataLayer.push({ event: "select_item", ecommerce: { item_list_id: listId, item_list_name: listName, items: item } }); break; } } } } jqAthena.ajax({ type: 'POST', cache: true, data: { 'productId': productId, 'type': type, 'token': websiteToken, 'searchKeywords': searchKeywords, 'customer': getCookie('athena_customer_cookie'), 'athenaUid': localStorage.getItem("_athena-uid") }, url: productClickApiUrl, dataType: "json", beforeSend: function () { if (currentRequest != null) { currentRequest.abort(); } }, error: function (request, error) { }, success: function (data) { } }); } } /* Disable button after submit search form */ jqAthena('#search_mini_form').submit(function (e) { var val1 = jqAthena('#search').val().replace(/(<([^>]+)>)/ig,""); let currentFocus = jqAthena("#currentFocus").val(); if (val1.trim() == "" && parseInt(currentFocus) == -1) { e.preventDefault(); } else { jqAthena(".action-search").prop('disabled', true); } if (currentRequest != null) { currentRequest.abort(); } hideAutocompleteBox(); hideFirstClickAutocompleteBox(); hideNoConnectionBox(); if (currentFocus != "-1") { /*If the ENTER key is pressed*/ let array = jqAthena(".athena-ac-element"); let element_id = array[currentFocus].id; let id_selector = jqAthena("#" + element_id); if (id_selector.data("type")) { jqAthena(".athena-search-autocomplete").css({'display': 'none'}); let id = id_selector.data("id"); let terms = id_selector.data("terms"); let type = id_selector.data("type"); let url = id_selector.data("url"); jqAthena("#athena_id").val(id); jqAthena("#athena_terms").val(terms); jqAthena("#athena_url").val(url); jqAthena("#athena_type").val(type); } let select_val = id_selector.children("input").val().replace(/(<([^>]+)>)/ig,""); jqAthena("#search").val(select_val); if (id_selector.data("type") == 'popular' || id_selector.data("type") == 'first-popular') { jqAthena("#search").val(id_selector.data("name").replace(/(<([^>]+)>)/ig,"")); } } }); /* Event OnClick */ jqAthena(document).on('click', function (event) { if (jqAthena('#search').length) { let val = jqAthena('#search').val().replace(/(<([^>]+)>)/ig,""); let res = jqAthena("#numRes").val().replace(/(<([^>]+)>)/ig,""); currentFocus = -1; jqAthena(".syncit-autocomplete-focus").removeClass('syncit-autocomplete-focus'); if (event.target.closest(domSelector)) { productClick(event.target.closest(domSelector), 'autocomplete'); } if (typeof event.target.closest(".search-form") === 'undefined' || event.target.closest(".search-form") === null || event.target.closest(".close-athena-autocomplete")) { jqAthena(".search-close").removeClass("active"); jqAthena(".search-form").removeClass("active overlay"); jqAthena("body").removeClass('athena-search'); hideAutocompleteBox(); hideFirstClickAutocompleteBox(); hideNoConnectionBox(); searchForm.removeClass('arrow'); jqAthena("html, body").removeClass('noscroll'); } else if (jqAthena(event.target).hasClass('overlay')) { jqAthena(".search-close").removeClass("active"); jqAthena(".search-form").removeClass("active overlay"); jqAthena("body").removeClass('athena-search'); hideAutocompleteBox(); hideFirstClickAutocompleteBox(); hideNoConnectionBox(); searchForm.removeClass('arrow'); jqAthena("html, body").removeClass('noscroll'); } else { jqAthena(".search-form").addClass('overlay'); jqAthena("body").addClass('athena-search'); if (!noConnection) { if (val.trim() === "") { if (jqAthena(".athena-flex-fc").html().length > 0) { showFirstClickAutocompleteBox(); hideAutocompleteBox(); } else { firstClick(); } } else if (res == "0") { jqAthena(".search-close").addClass("active"); jqAthena(".search-form").addClass("active"); jqAthena("#search").focus(); hideAutocompleteBox(); showFirstClickAutocompleteBox(); } else { jqAthena(".search-close").addClass("active"); jqAthena(".search-form").addClass("active"); showAutocompleteBox(); jqAthena("#search").focus(); hideFirstClickAutocompleteBox(); showAutocompleteBox(); } } else { jqAthena(".search-close").addClass("active"); jqAthena(".search-form").addClass("active"); showNoConnectionBox(); } } } }); jqAthena(document).mouseup(function (event) { if (event.target === jqAthena('html')[0] && event.clientX >= document.documentElement.offsetWidth) { if (window.innerWidth >= 992) { hideAutocompleteBox(); hideFirstClickAutocompleteBox(); hideNoConnectionBox(); searchForm.removeClass('arrow'); jqAthena(".search-form").removeClass("overlay"); jqAthena("body").removeClass('athena-search'); } } }); jqAthena(document).on("touchstart", ".athena-wrapper", function () { jqAthena("#search").blur(); }); jqAthena(document).on("touchmove", function () { if (!jqAthena('.athena-autocomplete').is(':visible') && !jqAthena('.athena-first-click').is(':visible')) { jqAthena(".search-close").removeClass("active"); jqAthena(".search-form").removeClass("active overlay"); jqAthena("body").removeClass('athena-search'); hideAutocompleteBox(); hideFirstClickAutocompleteBox(); hideNoConnectionBox(); searchForm.removeClass('arrow'); jqAthena("html, body").removeClass('noscroll'); jqAthena("#search").blur(); } }); /* Clear Search */ jqAthena("#search").val(""); /* Search Focus */ let inp = jqAthena("#search"); let currentFocus = -1; inp.on("keydown", function (e) { let keyCode = e.keyCode; let total_num_search = jqAthena(".athena-ac-element").length; jqAthena(".syncit-autocomplete-focus").removeClass('syncit-autocomplete-focus'); if (keyCode == 40) { /*If the arrow DOWN key is pressed*/ currentFocus++; if (currentFocus === total_num_search) { currentFocus = 0; } let array = jqAthena(".athena-ac-element"); let element_id = array[currentFocus].id; let id_selector = jqAthena("#" + element_id); id_selector.addClass('syncit-autocomplete-focus'); } else if (keyCode == 38) { /*If the arrow UP key is pressed*/ let array = jqAthena(".athena-ac-element"); currentFocus--; if (currentFocus < 0) { currentFocus = total_num_search - 1; } let element_id = array[currentFocus].id; let id_selector = jqAthena("#" + element_id); id_selector.addClass('syncit-autocomplete-focus'); } else if (keyCode == 37) { currentFocus = -1; jqAthena("#currentFocus").val(currentFocus); } else if (keyCode == 39) { currentFocus = -1; jqAthena("#currentFocus").val(currentFocus); } else { jqAthena("#athena_type").val('search'); } jqAthena("#currentFocus").val(currentFocus); }); jqAthena('.block-search').mouseleave(function () { currentFocus = -1; jqAthena("#currentFocus").val(currentFocus); jqAthena(".syncit-autocomplete-focus").removeClass('syncit-autocomplete-focus'); }); function getAnalyticsData(dataItems, listId, listName) { let items = []; for (let i = 0; i < dataItems.length; i++) { let sku = dataItems[i].sku ? dataItems[i].sku : ""; let name = dataItems[i].name ? dataItems[i].name : ""; let price = dataItems[i].price.loyalty_price ? dataItems[i].price.loyalty_price.toFixed(2) : dataItems[i].price.special_price ? dataItems[i].price.special_price.toFixed(2) : dataItems[i].price.regular_price.toFixed(2); let discount = ""; if (typeof dataItems[i].price.discount_percentage !== 'undefined') { discount = dataItems[i].price.discount_percentage; } let brand = ""; if (typeof dataItems[i].brand !== 'undefined' && typeof dataItems[i].brand.label !== 'undefined') { brand = dataItems[i].brand.label; } if (typeof dataItems[i].category_names !== 'undefined') { let categories = dataItems[i].category_names; if (categories.length) { let count = (categories.length < 5) ? categories.length : 5; for (let j = 0; j < count; j++) { if (j === 0) { items[i] = { "item_category": categories[j] } } else { let no = j + 1; items[i] = { ...items[i], [`item_category${no}`]: categories[j] } } } } } else { items[i] = { "item_category": "" } } items[i] = { "item_id": sku, "item_name": name, "affiliation": "Fashion&Friends - Online prodavnica", "coupon": "", "discount": discount, "item_brand": brand, ...items[i], "item_list_id": listId, "item_list_name": listName, "price": price, "quantity": 1 } } return items; } }); });