If you ever got a warning from Google Webmaster Tools regarding the products erorrs on your website about these missing fields, then you are at the right place.
Why does this happen?
By default, WooCommerce generates structured data for your products. But it leaves some fields empty due to various reasons.
Missing field "brand": By default, WooCommerce doesn't put a brand name unless you get a separate extension like WooCommerce Brands.
Missing field "review": When you launch your products for the first time, they don't have any reviews. So this field goes empty. When reviews start flowing in, it should go away on your own. Alternatively, you can review all your products once as well.
No global identifier provided: This is for barcodes or any other identifier that a company has provided for the products. There doesn't seem to be any way to add it built into WooCommerce.
Missing field "aggregateRating": This is another error that will go away once someone submits a review and rates your products.
The Solution
Update May 2021: It no longer works, WooCommerce changed something behind the scenes, I am still working on how to fix it again.
Like most problems in WordPress, this issue can also be fixed by a simple code snippet that you can add to either "functions.php" or the Code Snippets plugin.
<?php
function priyan_sh_woocommerce_structured_data_product( $markup, $product ) {
if (empty($markup['mpn']))
$markup['mpn'] = $markup['sku'];
if (empty($markup['brand']))
$markup['brand'] = $product->get_name();
if (empty($markup['brand']))
$markup['brand'] = 'unknown';
if (empty($markup['aggregateRating']))
$markup['aggregateRating'] = array(
'@type' => 'AggregateRating',
'ratingValue' => 5,
'reviewCount' => 1,
);
if (empty($markup['review']))
$markup['review'] = array(
'@type'=> 'Review',
'reviewRating'=> [
'@type'=> 'Rating',
'ratingValue'=> '5',
'bestRating'=> '5'
],
'author'=> [
'@type'=> 'Person',
'name'=> ''
]
);
return $markup;
};
add_filter( 'woocommerce_structured_data_product', 'priyan_sh_woocommerce_structured_data_product', 10, 2 );
The above snippet will fill any missing fields in the structured data with the following:
brand: The name of the particular product will be set as brand.
review and aggregateRating: It will raise the number of reviews to 1 and rate the product 5 stars.
global identifier: The SKU of your product will be assigned as the MPN.
Now, you can test your URL with Google's Structured Data Tool, you will find that all the errors are gone.
I hope this solves your problem. If you are still facing any issues, you can hit me up anytime!
Update (thanks to irfanmiral):
$markup['brand'] = array(
'@type' => 'Brand',
'name' => $product->get_name();
);
$markup['brand'] = array(
'@type' => 'Brand',
'name' => 'Unknown'
);