How to fix missing review on product schema (Rank Math)

Add code to file functions.php of theme or child theme:

function codetot_update_missing_product_schema( $entity ) {
    $stampedDBData = get_post_meta(get_queried_object_id(), 'stamped_io_product_reviews_new', true);

    if (isset($stampedDBData) && $stampedDBData['count'] > 0 ) {
        $entity['aggregateRating']['@type'] = "AggregateRating";
        $entity['aggregateRating']['ratingValue'] = $stampedDBData['rating'];
        $entity['aggregateRating']['ratingCount'] = $stampedDBData['count'];
    } else {
      $entity['aggregateRating'] = [
        '@type' => 'AggregateRating',
        'ratingValue' => '5',
        'ratingCount' => 0
      ];
    }
    return $entity;
}

add_filter( 'rank_math/snippet/rich_snippet_product_entity', 'codetot_update_missing_product_schema');

Leave a Reply

Your email address will not be published. Required fields are marked *