Skip to content

Instantly share code, notes, and snippets.

@khoipro
Last active August 31, 2023 01:18
Show Gist options
  • Save khoipro/c0595ea175e32653ab4ddfd907b524f0 to your computer and use it in GitHub Desktop.
Save khoipro/c0595ea175e32653ab4ddfd907b524f0 to your computer and use it in GitHub Desktop.
Fix Rank Math product schema (single product)
<?php
/**
* Fix missing review on product schema (Rank Math)
*
* @author codetot
**/
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');
@khoipro
Copy link
Author

khoipro commented Jul 28, 2023

Anh em đi qua thì nói cảm ơn một câu nếu xài code nha ^^ Code test xịn đó.

@Nguyenhieploi
Copy link

hay quá a

@TuyenNguyen297
Copy link

Tuyệt vời. Cảm ơn bạn

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment