Web Design. Web Development. Web Hosting.
0 items

Add Schema to WooCommerce Products

Need some help? We are here for you!

We have a very friendly service - Come and chat to us and let us know what you need, we work for an hourly fee and can also provide you a no obligation quote and begin work immediately in most cases. Click "Request Support" or use our Live Chat.

Request support

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

0 0 votes
Article Rating
Need some help? We are here for you!

We have a very friendly service - Come and chat to us and let us know what you need, we work for an hourly fee and can also provide you a no obligation quote and begin work immediately in most cases. Click "Request Support" or use our Live Chat.

Request support
Author: Josh BurleyOver 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.


Subscribe
Notify of
2 Comments
Inline Feedbacks
View all comments
Rudi
8 years ago

Hi,

How do I add the “name” attribute into that single_product.php file? Google’s structured data testing tool says my products are missing the “name” field.

LAbDesign80
7 years ago

Ok but I have 1 error with the value for the name field.



Share this page..

Let's begin your journey...

Get in touch us now to begin your journey to having a beautifully crafted mobile responsive website. If you're still unsure what we could do for you then take a look at our why us page which includes reviews, or take a look at our portfolio.

We can work on improving existing websites or craft a new stylish website making your business stand out.

You could always catch us by email, telephone, Skype or live chat to us below.


    2
    0
    Would love your thoughts, please comment.x
    ()
    x