storecategorylabel

<script>
window.addEventListener('DOMContentLoaded',async()=>{const config={isLink:!0,delay:300,batchSize:5,cacheExpiry:3600000};const bodyClasses=document.body.classList;if(!bodyClasses.contains('collection-type-products')||bodyClasses.contains('view-item'))return;const sleep=(ms)=>new Promise(resolve=>setTimeout(resolve,ms));const cacheManager={get:(key)=>{const cached=localStorage.getItem(key);if(!cached)return null;const data=JSON.parse(cached);if(Date.now()>data.expiry){localStorage.removeItem(key);return null}
return data.value},set:(key,value)=>{const data={value:value,expiry:Date.now()+config.cacheExpiry};try{localStorage.setItem(key,JSON.stringify(data))}catch(e){console.warn('Cache storage failed:',e)}}};const fetchJSON=async(path)=>{const cacheKey=`category_${path}`;const cached=cacheManager.get(cacheKey);if(cached){console.log('Using cached data for:',path);return cached}
const queryParams='?format=json'+'&'+Date.now();const response=await fetch(path+queryParams);const jsonData=await response.json();cacheManager.set(cacheKey,jsonData);return jsonData};const processProduct=async(item,collectionUrl)=>{try{const productJsonData=await fetchJSON(item.fullUrl);if(!productJsonData.item||!productJsonData.item.categoryIds||productJsonData.item.categoryIds.length===0)return;if(!productJsonData.nestedCategories||!productJsonData.nestedCategories.itemCategories)return;const productItem=document.querySelector(`[href="${item.fullUrl}"]`);if(!productItem)return;const productTitle=productItem.querySelector('.product-list-item-title');if(!productTitle)return;const existingCategories=productItem.querySelector('.categories-product-labels');if(existingCategories)return;const categoriesContainer=document.createElement('div');categoriesContainer.className='categories-product-labels';productJsonData.nestedCategories.itemCategories.forEach(category=>{const categoryElement=document.createElement(config.isLink?'a':'div');categoryElement.className='category-label';categoryElement.setAttribute('data-category-name',category.displayName);categoryElement.setAttribute('data-category-slug',category.shortSlug);categoryElement.textContent=category.displayName;if(config.isLink){categoryElement.href=category.fullUrl}
categoriesContainer.appendChild(categoryElement)});productTitle.insertAdjacentElement('beforebegin',categoriesContainer)}catch(error){console.log('Error fetching product data:',error)}};const processBatch=async(batch,collectionUrl)=>{const promises=batch.map((item,index)=>sleep(index*config.delay).then(()=>processProduct(item,collectionUrl)));await Promise.all(promises)};const collectionJsonData=await fetchJSON(window.location.pathname);if(!collectionJsonData.items)return;const collectionUrl=collectionJsonData.collection.fullUrl;const items=collectionJsonData.items;for(let i=0;i<items.length;i+=config.batchSize){const batch=items.slice(i,i+config.batchSize);console.log(`Processing batch ${Math.floor(i/config.batchSize) + 1} of ${Math.ceil(items.length/config.batchSize)}`);await processBatch(batch,collectionUrl);if(i+config.batchSize<items.length){await sleep(config.delay*2)}}
console.log('All products processed!')})
</script>

 

Buy me a coffee