Skip to content

Instantly share code, notes, and snippets.

@khoipro
Created July 28, 2023 07:42
Show Gist options
  • Save khoipro/469a00a2565b9750c123a25ea4e2d0b9 to your computer and use it in GitHub Desktop.
Save khoipro/469a00a2565b9750c123a25ea4e2d0b9 to your computer and use it in GitHub Desktop.
Fix Rankmath pro - product category schema
<?php
add_filter('rank_math/json_ld', 'codetot_product_rich_snippet_schema', 100);
/**
* Fix RankMath missing `AggregateRating` on product category schema
*
* @author codetot
*/
function codetot_product_rich_snippet_schema( $data ) {
if ( !is_tax( 'product_cat' ) ) {
return $data;
}
if ( isset( $data['ProductsPage']) ) {
$product_schemas = array_map(function($product) {
if ( empty($product['aggregateRating'] ) ) {
$aggregateRating = [
'@type' => 'AggregateRating',
'ratingValue' => 5,
'ratingCount' => 1
];
$product['aggregateRating'] = $aggregateRating;
}
return $product;
}, $data['ProductsPage']['@graph'] );
$data['ProductsPage']['@graph'] = $product_schemas;
}
return $data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment