Blog

2019.11.30

How to edit structured data in WooCommerce without a plugin. Adding brand, review and aggregateRating.

Just came back from a new project of mine. I signed it up for Google Search Console (ex Webmaster Tools) and today got an email that began with “Products issues detected on..” and they gave me a list of these structured data warnings I should take care of:

  • No global identifier provided (e.g. gtin, mpn, isbn)
  • Missing field ‘brand’
  • Missing field ‘review’
  • Missing field ‘aggregateRating’

A bit later I learned that luckily they all were related to product’s structured data which is not a huge error but rather a nice suggestion from Google that I should fix them to make my products look and work better in the search results.

What is structured data and why it matters

There is so much content and information on the internet. Search engine bots require additional information to understand what the page is about, to categorize it and know how to display it. This is where structured data comes into play. With this information we are able to explicitly describe the things we sell or display on the internet. This data is like a clue for search engines that tells that this page has a product, this is its title, here is the price and these are its reviews, etc.. It’s basically a standard of providing information.

Structured data actually allows to describe a lot of different types of information, not just products but also books, articles, events, places, organizations, people etc. You can learn more about schema here.

Product data in WooCommerce

We have looked at this data now and can get back to my issue. So Google said that I have issues with my product’s structured data which means that my WooCommerce product hasn’t got all of the information that there should be.

WooCommerce is amazing – it automatically adds this data to each product you are selling and in most case you don’t have to worry about it. However in my particular case the product had ratings and reviews which were pulled in from another platform. That’s why WooCommerce was having hard time automatically adding ‘review’ and ‘aggregateRating’ values to the product schema. Also I wasn’t able to find if WooCommerce has an option to add ‘brand’ to the product schema therefore I had to find a way to integrate this information into my products.

How to add or edit structured data in WooCommerce

I must admit I spent a lot of time looking where and how could I hook into WooCommerce to filter the structured data. At one point was even almost calling it quits but in the end after digging deep in the documentation managed to find a solution. Here is a snippet you can add to your functions.php to add the missing fields:

//Adding additional data to Product's schema required for search engines
add_filter( 'woocommerce_structured_data_product', function( $schema, $product ) {
    $schema['brand'] = 'CartBounty';
    $schema['aggregateRating'] = array(
    	'reviewCount' => 10,
    	'ratingValue' => 5
    );
    $schema['review'] = array(
    	'@type' => 'Review',
    	'author' => 'Marcelcowan',
    	'datePublished' => '2019-10-04',
    	'name' => 'Perfect plugin, best one around',
    	'description' => 'Perfect plugin for Abandon Cart, easy to set-up and by far the best one around, and we looked at them all!',
    	'reviewRating' => array(
    		'@type' => 'Rating',
    		'bestRating' => 5,
    		'worstRating' => 1,
    		'ratingValue' => 5
    	),
    );
    $schema['review'] = array(
    	'@type' => 'Review',
    	'author' => 'Name of the author here',
    	'datePublished' => '2019-10-04',
    	'name' => 'Review title here',
    	'description' => 'Description of our review here',
    	'reviewRating' => array(
    		'@type' => 'Rating',
    		'bestRating' => 5,
    		'worstRating' => 1,
    		'ratingValue' => 5
    	),
    );
    return $schema;
}, $priority = 20, $accepted_args = 2 );

If you would like to edit existing structured data fields, the best option would be to use WooCommerce admin panel and edit the data of your product however if for some reason this is not an option, you could just use the same function above and add the field you would like to change there like so:

//Edit existing structured data fields in WooCommerce
add_filter( 'woocommerce_structured_data_product', function( $schema, $product ) {
    $schema['name'] = 'Your new products name here';
    $schema['description'] = 'Your new products description here';
    $schema['image'] = 'Link to your new product image here';
    return $schema;
}, $priority = 20, $accepted_args = 2 );

And as easy as that – you have taken care of your product data without using any fancy-shmancy plugins.

Bonus tip – how to disable / remove default WooCommerce structured data

In case you are looking to use a WooCommerce plugin for your data or in any other case if you would like to remove or disable JSON-LD structured data at all – you could use this snippet in your functions.php file.

// Remove default WooCommerce structured data
function remove_wc_structured_data() {
    remove_action( 'wp_footer', array( WC()->structured_data, 'output_structured_data' ), 10 );
}
add_action( 'init', 'remove_wc_structured_data' );

Hope this helps to up your search engine and structured data game in WooCommerce. Please do let me know in the comments below if you have any additional questions or would like to share whatever it is you would like to share :)

In categories: Website development, Woocommerce, Wordpress

Nauris Kolāts

Nauris is a freelance designer / developer who loves to dig into the UX as much as in the ground for fishing worms. And fishing is just one amongst the long list of his active lifestyle hobbies.

Other posts

Your thoughts

Join the discussion

  1. Sebastian Velandia

    Holy shit this is amazing thank you !!

  2. Soenarto

    You wrote very well and the four warnings that you found were exactly the problem I experienced. But, how can the code above work for different products?

    Thank you in advance

    • Hi Soenarto, thanks for your question. The code above should work for all kinds, categories and types of products in WooCommerce.

  3. Angel

    Thank you very much for sharing it, I have been looking for this problem for a long time and you did not find it. 3 problems were solved for me (brand, review and aggregateRating) but there is another one that cannot solve it and I don’t know how to add it, it is No global identifier is provided (for example, gtin, mpn, isbn). Would you have the solution? Thank you

    • Hi, thanks for your comment.
      Since ISBN, MPN and GTIN all are custom fields and WooCommerce doesn’t support them by default, I’m afraid you will require to find a custom development solution for this.

  4. coolernew

    Hello
    Thank you very much
    I was really looking for these function codes on the web.

  5. Paul C

    Thank you so much! After checking the Google search console I discovered brands were recommended for the products but are not present in the Woocommerce generated data. I can now go and add the brands! Brilliant!

  6. Anh Tuan

    it’s not work for me, it’s still show Missing field “aggregateRating” (optional)

    • Sorry to hear that. You should double check your syntax and try this out on a default WooCommerce theme like Storefront. You might even try to disable all plugins except WooCommerce as you might be having issues with some plugins you have installed.

  7. Dimora degli Angeli

    Complimenti! Grazie al primo codice che postato ho risolto un grande problema sui dati strutturati mancanti, Grazie!

  8. Gaurav Blaggan

    Thank you for sharing the process to alter the default structed data of Woocommerce. You saved my day!

Latest work