Product Structured Data Markup for Woocommerce Product

July 9, 2020

Anne Allen

Add this to the head of your single-product.php page in the woocommerce directory of your theme. This will enable a rich result on Google, Rich pin on Pinterest etc

add_action( 'wp_head', 'ns_tc_product_json_ld' );
function ns_tc_product_json_ld(){
    global $product;
    ?>
    <script type="application/ld+json">
        {
        "@context": "http://schema.org/",
        "@type": "Product",
        "name": "<?php echo $product->get_name();?>",
        "image": 
            "<?php echo genesis_get_image(
                array(
                    'format' => 'url',
                    'size' => 'full',
                    
                )
                );?>",
        
        "description": "<?php echo get_post_meta(get_the_ID(), '_yoast_wpseo_metadesc', true);?>",
        "mpn": "<?php echo $product->get_sku();?>",
        "brand": {
            "@type": "Brand",
            "name": "My Brand"
        },
        <?php
        if ($product->get_review_count() > 0) { ?>
      "aggregateRating": {
        "@type": "AggregateRating",
        "ratingValue": "<?php echo $product->get_average_rating();?>",
        "reviewCount": "<?php echo $product->get_review_count();?>"
      },
      <?php } ?>
      "offers": {
        "@type": "Offer",
        "url": "<?php echo get_permalink();?>",
        "priceCurrency": "GBP",
        "price": "<?php echo $product->get_price();?>",
        "priceValidUntil": "2020-12-31",
        "itemCondition": "https://schema.org/NewCondition",
        "availability": "https://schema.org/InStock",
        "seller": {
          "@type": "Organization",
          "name": "My Organization"
        }
      },
      "sku": "<?php echo $product->get_sku();?>"
    }
</script>
<?php
}

Anne Allen

About the author

Hi, I’m Anne Allen. I’ve spent the last 15 years living and breathing WordPress. I’m passionate about helping business owners demystify their websites—whether that means keeping your site secure with proper maintenance, setting up complex Gravity Forms, or ensuring your content is accessible through ADA compliance. Let’s make your site work for you.