Add Schema.org Markup to WooCommerce Products

WooCommerce & Schema.org Is Awesome

Adding schema.org markup to a well coded WordPress theme is relatively straight forward and doesn’t take very long to get setup.

I covered how to add schema.org markup to your WordPress theme in a previous post, but I recently needed to apply schema.org markup to an e-commerce site using WooCommerce.

It’s surprisingly easy to do. You’ll need to be using a child theme for the steps that follow.

1. Setup the necessary function in the functions.php file for your theme

Add the following to your functions.php file. It creates a custom function, schema_org_markup.

function schema_org_markup() {
    $schema = 'http://schema.org/';
    // Is single post
    if ( function_exists(is_woocommerce) && is_woocommerce() ) {
      $type = 'Product';
    }
    elseif ( is_single() ) {
        $type = "Article";
    } 
    else {
        if ( is_page( 644 ) ) { // Contact form page ID
            $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';
        }
        else {
            $type = 'WebPage';
        }
    }
    echo 'itemscope="itemscope" itemtype="' . $schema . $type . '"';
}

2. Call schema_org_markup() In Your Header

Open up the header.php file for your child theme and find the html tag, usually towards the top. You’ll want to call the schema_org_markup function inside that html tag, like so:

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

3. Create a WooCommerce template file in your child theme

Create a directory in your child theme folder named woocommerce. Inside the woocommerce folder, create another new folder named single-product. Inside the single-product folder, create a file named price.php. The contents of your price.php file should look like this:

<?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>

4. All Done

That’s all that’s required to add schema.org markup to individual WooCommerce product pages. Pretty simple.

If you run into any issues or it doesn’t seem to be working for you, let me know. I’ve only tested this with two themes, Vantage and Virtue. Remember, this only works with well-crafted WordPress themes. Doing this with purchased themes from ThemeForest or other paid theme marketplaces can be significantly more difficult.

Comments are open so let me know if you have any issues, additions, questions, or suggestions.

  • Vivek Padhye

    Just what I was looking for! Can we add schema for reviews as well?

  • Michael Christensen

    Tyler, thanks for the walk-through.

  • Nice – could make an ez plugin for this…

  • Omar

    Hi thanks for the trick, but I still not have tag name on structured data, my site is http://www.herbashop.it

    • Hi Omar,

      How did you add the schema markup? I don’t see any schema markup on your site at all (unless you’ve since removed it).

      If you’d like help setting it up, email me at tyler@longren.io or tlongren@gmail.com.

  • Hi there!

    I’d be happy to lend a hand. Would take some additional modifications to the code in this tutorial, but Schema.org has many properties available for use with reviews.

    Email me at tyler@longren.io or tlongren@gmail.com if you want help or have more questions.

  • Eoin Bern

    Parse error: syntax error, unexpected ‘function’ (T_FUNCTION), expecting ‘{‘ in /home/digilzcr/public_html/wp-content/themes/vantage/functions.php on line 451

    What does this mean? I used the exact code as above in point 1

  • Thank-you sir!

  • Evan Henry

    How would these work for adding markup to product variations?

  • Little Tiger Gifts

    Hi Tyler, great article, one of only a few out there so thank you. I have added the above code to my Woocommerce site which is using WooThemes Canvas theme but for some reason the itemtype for all my test products is being set to ‘Article’ rather than ‘Product’. Category and Shop pages are being set to correctly Product itemtype. Example product url is http://www littletigertest co uk/product/les-papoum-elephant-soft-toy-moulin-roty-copy/

    I know you must be busy so I am just chancing my luck for a reply 🙂

    Thanks Steve.

    • Hey Steve, ever get it working? If not I’d be happy to have a look. Let me know.

      • Little Tiger Gifts

        Hi Tyler, thanks for responding, the WooThemes Canvas theme I am using does this out of the box. Adding the extra code simply broke it!

        I should have checked first before assuming it did not.

        Thanks

        • Good to know! Seems like more and more themes are integrating schema.org markup, especially the WooThemes folks.

  • Slight correction to your code required as it’s defaulting WooCommerce products to articles.

    Otherwise working great.

    function schema_org_markup() {
        $schema = 'http://schema.org/';
        // Is single post
        if ( function_exists(is_woocommerce) && is_woocommerce() ) {
          $type = 'Product';
        }
        elseif ( is_single() ) {
            $type = "Article";
        } 
        else {
            if ( is_page( 644 ) ) { // Contact form page ID
                $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';
            }
            else {
                $type = 'WebPage';
            }
        }
        echo 'itemscope="itemscope" itemtype="' . $schema . $type . '"';
    }
    
    
    • Interesting!!

      Really appreciate you bringing this up.

      I’ll have a major revision ready within a few weeks hopefully that will let you fill woocommerce with all the microdata you could want (that may be a bit of an exaggeration, but I’ll be taking feature requests).

      I may have said too much already, keep an eye out.

      Martin, I’ll update the post with your properly working code momentarily.

      • LOL… Your mistake was “I’ll be taking feature requests” (watches the inbox explode!) 😉

        Glad it was useful and definitely be interested to see what developments are in the wind. I’m looking at how to incorporate in volume discount data (max/min price) and multi-currency information based on the Aelia currency switcher plugin so got a bit list of “would be nice” to incorporate in..

        Let me know if you need a critical eye to cast over any dev code…

  • Fernando De Los Santos

    Can this code be adapted to work with WP E-Commerce? or is there a plugin or script that you can recommend for WPEC? Thank you!

    • Absolutely. There’s no plugin I can recommend for WPEC. I’d be willing to develop something. I’d need access to a demo WPEC site though for testing, would just make things so much faster if I didn’t have to setup WPEC with products and everything.

      Do you happen to have a site using WPEC that I could have access to? Or would you be willing to set one up?

  • Why can’t you just filter the default schema like you can in Genesis?

  • Martha van Berkel

    Tyler, love that you have enabled people to add this themselves. For people who just want it done, and comprehensively so you get the best results from your search results, check out the WordPress Plugin made by Schema Apps – https://www.schemaapp.com/product/schema-app-woocommerce/

  • Tyler. Thanks for this information as it is very informative. I have a small question. I currently have a site that is using WooCommerce and the theme from WooThemes seems to have most of the schema.org product tags. However, it does not have “itemcondition”. Is there as way you can think of that I can simply add only the itemcondition schema?
    7.27 New

    • David Changaris

      did we ever get a fix for itemcondition and woocommerce for google merchant validation?

  • Александр Левицкий

    It works perfectly! Thank you so much! This article really made my day! Thank you thank you thank you!

  • Pingback: Tips for selling digital products with WooCommerce - ReadySpace Indonesia()

  • Iain Henderson

    Hi, many thanks for the easy walk through! I am getting an error from Google Webmaster though as all my category pages are being marked up as ‘product’ pages with no name. I’m using the Storefront theme. Any ideas what needs to be changed? Thanks, Iain

  • Pingback: Schema for WooCommerce()

  • Hey

    I found your blog through woocommerce, and their post about selling digital goods.
    If you could help me solve a problem, or suggest a solution, it would be amazing.

    I have a website built on WordPress, and Woocommerce.
    It sells a digital / printed magazine. Whihc means, that the same magazine issue can be bought in printed and online versions.
    But, we want to sell the digital version in “Zinio” format”, which means, that the licence a person bought, would allow them to read the magazine online, on their pc, laptop, smartphone, tablet, without dowloading it to their computer. To prevent them from sharing the pdf file of the magazine.

    is this possible on WooCommerce?

    With best wishes, thank you, Martyna

  • I’d like to display our products as Rich Cards on Googles search result pages but for that you need more structured data. My products are missing the values for “brand” and the “MPN”. Do you know a solution how to echo such values when you use an additional product attribute “brand” in Woocommerce?
    Here you find more informations about the markup for rich cards: https://developers.google.com/search/docs/guides/search-gallery

  • Pingback: Googlen kuvahaku tarjoaa kuvaan liittyviä tuotteita - SEOSEON.COM()