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
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
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.
Ok but I have 1 error with the value for the name field.