Showing all 3 results
add_action('wp_head', function () { if (!function_exists('is_product') || !is_product()) return; // Get product ID $product_id = get_the_ID(); // Get product title $name = get_the_title($product_id); // Clean name for JSON $name = str_replace(array('"', '\\'), array("'", ""), $name); // Get rating and review count from post meta (replace meta keys if needed) $rating = get_post_meta($product_id, '_wc_average_rating', true); $count = get_post_meta($product_id, '_wc_review_count', true); // Get price and currency $price = get_post_meta($product_id, '_price', true); $currency = get_woocommerce_currency(); // returns USD, EUR, etc. // Get availability $stock_status = get_post_meta($product_id, '_stock_status', true); if ($stock_status == 'instock') { $availability = 'InStock'; } else { $availability = 'OutOfStock'; } // Get product image URL $image_id = get_post_thumbnail_id($product_id); $image_url = wp_get_attachment_url($image_id); // Build JSON-LD string $json_ld = ' { "@context": "https://schema.org", "@type": "Product", "name": "' . $name . '", "image": "' . $image_url . '", "aggregateRating": { "@type": "AggregateRating", "ratingValue": "' . $rating . '", "reviewCount": "' . $count . '" }, "offers": { "@type": "Offer", "price": "' . $price . '", "priceCurrency": "' . $currency . '", "availability": "http://schema.org/' . $availability . '" } }'; echo ''; });
Showing all 3 results