Add Google Merchant Structured Data to WooCommerce (JSON-LD & Microdata)

Add Google Merchant Structured Data to WooCommerce (JSON-LD & Microdata)

Easily add the correct structured data using JSON-LD or Microdata for Google Merchant, this includes the missing microdata for condition. Solutions found on other tutorials where you need to add in the price.php file is not suitable as most themes will have already partial json-ld or microdata added. Google always reads and uses the first set visible. Therefore we need to add the structured data in the header so that Google reads the correct set first.

Step 1

Locate the file header.php in your theme, this will be located under [mywebsite]/wp-content/themes/[your theme]/header.php

Step 2

Before editing header.php, always make a backup so that you can roll back if something goes wrong.

At the bottom of the header, simple add the code below

JSON LD

<!-- created by FeedArmy -->
<?php
if (isset($product)){
$meta = get_post_meta(get_the_ID());
$_product = new WC_Product(get_the_ID());
if ($_product->regular_price!=NULL){
	$price = $_product->regular_price;
}elseif ($_product->price!=NULL){
	$price = $_product->price;
}
if ( ($_product->price > $_product->sale_price) && ($_product->sale_price!=NULL) ){
	$price = $_product->sale_price;
}
$_image = wp_get_attachment_image_src( get_post_thumbnail_id( $loop->post->ID ), 'single-post-thumbnail' );
?>
<script type="application/ld+json">
{
  "@context": "http://schema.org/",
  "@type": "Product",
	"name": "<?php echo get_the_title(get_the_ID()); ?>",
  "description": "<?php echo $_product->description; ?>",
  "sku": "<?php echo $_product->get_sku(); ?>",
  "image": "<?php echo $_image[0]; ?>",
	"offers": {
	"@type": "Offer",
	"priceCurrency": "<?php echo get_woocommerce_currency(); ?>",
	"price": "<?php echo $price; ?>",
	"itemCondition" : "http://schema.org/NewCondition",
	"availability" : "http://schema.org/<?php echo $meta['_stock_status'][0] ? 'InStock' : 'OutOfStock'; ?>",
  "url" : "<?php echo get_permalink(get_the_ID()); ?>"
  }
}
</script>
<?php } ?>
<!-- created by FeedArmy -->

Microdata

<!-- created by FeedArmy -->
<?php
$meta = get_post_meta(get_the_ID());
$_product = new WC_Product(get_the_ID());
if ($_product->regular_price!=NULL){
	$price = $_product->regular_price;
}elseif ($_product->price!=NULL){
	$price = $_product->price;
}
if ( ($_product->price > $_product->sale_price) && ($_product->sale_price!=NULL) ){
	$price = $_product->sale_price;
}
if (isset($product)){ ?>
<div itemscope itemtype="http://schema.org/Product">
  <meta itemprop="name" content="<?php echo get_the_title(get_the_ID()); ?>">
  <meta itemprop="description" content="<?php echo $_product->description; ?>">
  <meta itemprop="sku" content="<?php echo $_product->get_sku(); ?>">
  <meta itemprop="image" content="<?php echo $_image[0]; ?>">
	<meta itemprop="productID" content="<?php echo get_the_ID(); ?>">
	<div itemprop="offers" itemscope itemtype="http://schema.org/Offer">
		<meta itemprop="price" content="<?php echo $price; ?>" />
		<meta itemprop="priceCurrency" content="<?php echo get_woocommerce_currency(); ?>" />
		<link itemprop="availability" href="http://schema.org/<?php echo $meta['_stock_status'][0] ? 'InStock' : 'OutOfStock'; ?>" />
		<meta itemprop="itemCondition" itemtype="http://schema.org/OfferItemCondition" content="http://schema.org/NewCondition" />
		<link itemprop="url" href="<?php echo get_permalink(get_the_ID()); ?>" />
  </div>
</div>
<?php } ?>
<!-- created by FeedArmy -->

In the image below is an example of how I have added it.

WordPress Header

Step 3

Verify that you have done it correctly by checking the page with Google’s Structured Data Testing Tool

As of writing, you will see 4 warnings, these are :

  • priceValidUntil
  • aggregateRating
  • review
  • brand

WooCommerce has no default coding that can be used to fill these fields in. So simply ignore these.

Google Structured Data Testing Result

Tip

If you want to improve ranking, editing your titles is extremely important, If you want the ability to edit your titles for Google Shopping but do not want to change the website, why not use FeedArmy for publishing your products to Google Shopping. You can also export variants!

Need Help?