Add Category ID to Product Detail Page

Description: add Category ID to Product Detail Page

By default on Product Detail Page, only Tag ID appears, like this.

With this code, we can make Category ID appears. Like this.

Just use this code to Code Injection > Footer (or Store Page Header Injection)

<script>window.addEventListener('DOMContentLoaded',async()=>{const bodyClasses=document.body.classList;if(!bodyClasses.contains('view-item'))return;const fetchJSON=async(path)=>{const queryParams='?format=json'+'&'+Date.now();const response=await fetch(path+queryParams);const jsonData=await response.json();return jsonData};try{const productJsonData=await fetchJSON(window.location.pathname);if(!productJsonData.item||!productJsonData.item.categoryIds||productJsonData.item.categoryIds.length===0)return;if(!productJsonData.nestedCategories||!productJsonData.nestedCategories.itemCategories)return;const productDetail=document.querySelector('.product-detail');if(!productDetail)return;productJsonData.nestedCategories.itemCategories.forEach(category=>{const categorySlug=category.shortSlug||category.displayName.toLowerCase().replace(/\s+/g,'-');productDetail.classList.add(`category-${categorySlug}`)})}catch(error){console.log('Error fetching product data:',error)}});</script>

 

Buy me a coffee