Add Schema to WooCommerce Products

Add Schema to WooCommerce Products
There is a mobile optimized version of this page, view AMP Version.

WooCommerce & Schema.org/Product

To add product schema to your products in WordPress using WooCommerce is relatively straight forward, providing you follow these instruction closely and that the theme you’re using has been coded properly and within the ‘normal’ methods for a WordPress theme. Now lets get started and add schema to WooCommerce products

Step 1. Add schema_org_markup function

Open the file in

wp-content/themes/[your_theme]/functions.php

and then in here add the following function:

<?php
/**
* Custom Function to add product schema
**/
function schema_org_markup() {
	$schema = 'http://schema.org/';
	// Is single post
	if ( is_single() ) {
		$type = "Article";
	} // Contact form page ID
	else {
		if ( is_page( 1 ) ) {
			$type = 'ContactPage';
		} // Is author page
		elseif ( is_author() ) {
			$type = 'ProfilePage';
		} // Is search results page
		elseif ( is_search() ) {
			$type = 'SearchResultsPage';
		} // Is of movie post type
		elseif ( is_singular( 'movies' ) ) {
			$type = 'Movie';
		} // Is of book post type
		elseif ( is_singular( 'books' ) ) {
			$type = 'Book';
		}
    elseif ( function_exists(is_woocommerce) && is_woocommerce() ) {
      $type = 'Product';
    } else {
			$type = 'WebPage';
		}
	}
	echo 'itemscope="itemscope" itemtype="' . $schema . $type . '"';
}
?>

Step 2. Call the function in your header

Open the file in

wp-content/themes/[your_theme]/header.php

and then replace:

<html <?php language_attributes(); ?>>

With:

<html <?php schema_org_markup(); ?> <?php language_attributes(); ?>>

Step 3. Finally, Create a WooCommerce template

Create the following directories first:

wp-content/themes/[your_theme]/woocommerce<span id="mce_marker" data-mce-type="bookmark" data-mce-fragment="1">​</span>
wp-content/themes/[your_theme]/woocommerce/single-product<span id="mce_marker" data-mce-type="bookmark" data-mce-fragment="1">​</span>

Now in this final directory please create a new file called price.php and add the following code:

<?php
/**
 * Single Product Price, including microdata for SEO
 *
 * @author 	WooThemes
 * @package 	WooCommerce/Templates
 * @version     1.6.4
 */

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

global $post, $product;
?>
<div itemprop="offers" itemscope itemtype="http://schema.org/Offer">
 
	<p class="price"><?php echo $product->get_price_html(); ?></p>
 
	<meta itemprop="price" content="<?php echo $product->get_price(); ?>" /> 
	<meta itemprop="priceCurrency" content="<?php echo get_woocommerce_currency(); ?>" />
	<link itemprop="availability" href="http://schema.org/<?php echo $product->is_in_stock() ? 'InStock' : 'OutOfStock'; ?>" />
</div>
?>

Thats it. You now have your product schema set up on your WooCommerce products

Author: Josh Burley

Over 10 years software and website development, more recently moving into app development. Became a founding member and managing partner of WebDesires in late 2014 early 2015. Now dedicate most of my time to the company and helping us grow into the industry.


2 comment(s) on “Add Schema to WooCommerce Products

Leave a Reply

Your email address will not be published. Required fields are marked *