Understand the Product Review Schema Markup
Before diving into the implementation process, it's essential to familiarize yourself with the Product Review Schema markup. This schema provides search engines with specific details about your product, including its name, description, average rating, and reviewer information. By structuring this information in a standardized format, you can help search engines understand and display your product reviews more effectively in search results.
Step 1: Add Schema Markup to the Template File
Once you've located the relevant template file, open it using a text editor. Insert the necessary Product Review Schema markup within the appropriate HTML tags
Step 2: Validate Your Schema Markup
After adding the Product Review Schema markup to your template file, it's crucial to validate it to ensure it follows the correct structure and syntax. Utilize the Structured Data Testing Tool provided by Google or other schema markup validators available online. This step helps identify any errors or warnings that need to be addressed.
add_action( 'wp_footer', 'add_rating_ld_json_markup' );
function add_rating_ld_json_markup() {
global $product;
if ( ! wc_review_ratings_enabled() || ! is_a( $product, 'WC_Product' ) ) {
return;
}
$product_id = $product->get_id();
$rating_count = get_post_meta( $product_id, '_wc_review_count', true );
$average = get_post_meta( $product_id, '_wc_average_rating', true );
if ( $rating_count > 0 ) {
$ld_json_markup = array(
'@context' => 'https://schema.org/',
'@type' => 'Product',
'name' => get_the_title(),
'description' => get_the_excerpt(),
'aggregateRating' => array(
'@type' => 'AggregateRating',
'ratingValue' => $average,
'reviewCount' => $rating_count
)
);
// Output the ld+json markup
echo '';
}
}