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

WordPress header.php location

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

<?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;
}
?>
<script type="application/ld+json">
{
  "@context": "http://schema.org/",
  "@type": "Product",
	"name": "<?php echo get_the_title(get_the_ID()); ?>",
	"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'; ?>"
  }
}
</script>
<?php } ?>

Microdata

<?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="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" />
  </div>
</div>
<?php } ?>

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

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?