import { withErrorHandling } from "./sentry"; function detectProductIdFromWooCommerce(): string | undefined { if (typeof window === "undefined" || typeof document === "undefined") return undefined; const productElement = document.querySelector('.single-product .product'); if (!productElement) return undefined; const classMatch = productElement.className.match(/post-(\d+)/); if (classMatch) return classMatch[1]; const productIdMeta = document.querySelector('meta[property="product:retailer_item_id"]'); if (productIdMeta) { const content = productIdMeta.getAttribute('content'); if (content) return content; } return undefined; } function detectProductIdFromURL(): string | undefined { if (typeof window === "undefined") return undefined; const path = window.location.pathname; const productMatch = path.match(/\/product\/[^/]+\/?$/); if (!productMatch) return undefined; const bodyClasses = document.body.classList; for (const className of bodyClasses) { const match = className.match(/postid-(\d+)/); if (match) return match[1]; } return undefined; } function detectProductIdFromDOM(): string | undefined { if (typeof window === "undefined" || typeof document === "undefined") return undefined; const cartForms = document.querySelectorAll('form.cart'); for (const form of cartForms) { const input = form.querySelector('input[name="product_id"]'); if (input?.value) return input.value; const variationInput = form.querySelector('input[name="variation_id"]'); if (variationInput?.value && variationInput.value !== "0") { return variationInput.value; } } const addToCartButtons = document.querySelectorAll('[data-product_id]'); for (const button of addToCartButtons) { const productId = button.getAttribute('data-product_id'); if (productId) return productId; } return undefined; } function detectGenericPostId(): string | undefined { if (typeof window === "undefined" || typeof document === "undefined") return undefined; if (document.body.classList.contains('home') || document.body.classList.contains('front-page') || document.body.classList.contains('archive') || document.body.classList.contains('search')) { return undefined; } const bodyClasses = document.body.classList; for (const className of bodyClasses) { const match = className.match(/postid-(\d+)/); if (match) return match[1]; } const articleElement = document.querySelector('article[id^="post-"]'); if (articleElement) { const match = articleElement.id.match(/post-(\d+)/); if (match) return match[1]; } return undefined; } export const getProductId = withErrorHandling("getProductId", (): string | undefined => { // Check server-side detected product ID first (from WordPress) if (typeof window !== "undefined" && (window as any).chatbotData?.productID) { return (window as any).chatbotData.productID.toString(); } // Fall back to client-side detection strategies for non-WordPress installations const strategies = [ detectProductIdFromWooCommerce, detectProductIdFromURL, detectProductIdFromDOM, detectGenericPostId ]; for (const strategy of strategies) { const productId = strategy(); if (productId) return productId; } return undefined; }); export const isProductPage = withErrorHandling("isProductPage", (): boolean => { return getProductId() !== undefined; });