Dynamic Product Schema for WooCommerce. Can be placed inside the themes functions.php file.
Please Note: If reviews/ratings do not exist, a conditional statement is needed to check for:
$product->get_reviews_allowed == true && $product->get_review_count() => 0
function woo_product_schema() { global $woocommerce; if(is_product()) { global $product; $brand=""; $brands = wp_get_post_terms( $product->get_id(), 'product_brand', array("fields" => "all") ); foreach( $brands as $brand ) { $brand= $brand->name; break; } $image = wp_get_attachment_image_src( get_post_thumbnail_id( $product->get_id() ), 'single-post-thumbnail' ); ?> <script type="application/ld+json"> { "@context": "https://schema.org/", "@type": "Product", "name": "<?php echo $product->get_title(); ?>", "image": [ "<?php echo $image[0]; ?>" ], "description": "<?php echo strip_tags(get_post($product->get_id())->post_content); ?>", "sku": "<?php echo $product->get_sku(); ?>", "mpn": "<?php echo $product->get_sku(); ?>", "brand": { "@type": "Thing", "name": "<?php echo $brand; ?>" }, "aggregateRating": { "@type": "AggregateRating", "ratingValue": "<?php echo $product->average_rating[0]; ?>", "reviewCount": "<?php echo $product->get_review_count(); ?>" }, "offers": { "@type": "Offer", "url": "<?php echo get_permalink($product->get_id()); ?>", "priceCurrency": "EUR", "price": "<?php echo $product->get_price(); ?>", "priceValidUntil": "2030-11-05", "itemCondition": "https://schema.org/NewCondition", "availability": "https://schema.org/InStock", "seller": { "@type": "Organization", "name": "Aus Property Professionals" } } } </script> <?php } } add_action( 'wp_head', 'woo_product_schema' );