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="name" content="<?php echo $product->get_name(); ?>" /> 
    <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.

3+

Well, now what?

Work with Me

I'm available for hire and always taking new clients, big and small. Got a project or an idea you'd like to discuss? Startup plan but no developer to make it happen? Just get in touch, I'd love to see if I can help you out!

Leave some Feedback

Got a question or some updated information releavant to this post? Please, leave a comment! The comments are a great way to get help, I read them all and reply to nearly every comment. Let's talk. šŸ˜€

Longren.io is proudly hosted by DigitalOcean

DigitalOcean

46 thoughts on “Add Schema.org Markup to WooCommerce Products

  1. 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

    0
  2. 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.

    0
      1. 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

        0
  3. 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 . '"';
    }
    
    
    1+
    1. 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.

      0
      1. 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…

        1+
  4. 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!

    0
    1. 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?

      1+
  5. 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

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

    0
  7. 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

    0
  8. 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

    0
  9. 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

    0
  10. Greetings!
    I’ve been looking for a long time for a working code, so I finally found it.
    There is nothing suitable on the Russian Internet.
    Thank you!

    0
  11. Thank you for this post Tyler. I am not sure if you read these comments any longer, but I am hoping one day you might.
    Our website was created with WordPress/Woocommerce and up to July last year, it had product schema markup showing on Google searches, with the price of the products and also “in stock”. IT showed in all google organic searches, it was fabulous.
    Then it vanished. No more price showing, just a standard search listing. This has definitely had a bad effect on our sales, so I have been ever since trying to figure out what to do to bring the rich snippets back.
    I spent many sleepless nights researching and trying, I paid 2 developers and nobody could never do it.
    Then I found this post and followed all the necessary steps.
    This was about 4 days ago.I now can see the structure mark up on some products when I do a search, like 4 products out of 200, but at least I can see it there. I have no idea why that is, why is it not showing on all products when they come up on searches? And why is it showing on these? It is bizarre.
    I would be happy to hire your services for a few hours to try and figure this out and fix it once and for all.
    You can contact me via our website https://ww.shop4safety.com.au

    Thanks again,

    Luciana

    0
  12. Thanks for the info. I was looking at how to add in product schema like brands without installing a plugin and trying to code it in my Wordprress Woocommerce website. My website using twenty twenty theme. Will your schema code apply? My website is https://theholythead.com

    0

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.