Untitled
unknown
plain_text
2 years ago
1.7 kB
11
Indexable
add_filter( 'aioseo_schema_output', 'product_schema_aggregate_reviews' );
function product_schema_aggregate_reviews( $schema ) {
foreach ( $schema as &$schemaItem ) {
if ( 'Product' === $schemaItem['@type'] ) {
$schemaItem['aggregateRating']['ratingValue'] = 0; // getJudgeMeRating( get_the_ID() );
$schemaItem['aggregateRating']['reviewCount'] = 0; // getJudgeMeCount( get_the_ID() );
}
}
return $schema;
}
add_action( 'wp_head', function () { ?>
<script>
document.addEventListener('DOMContentLoaded', () => {
if (document.querySelector('.jdgm-rev-widg') && (document.querySelector('.jdgm-rev-widg').dataset.numberOfReviews !== 0)) {
// Select the script tag by class name
let scriptTag = document.querySelector('.aioseo-schema');
// Extract the JSON string
let jsonString = scriptTag.textContent;
// Parse the JSON string to a JavaScript object
let data = JSON.parse(jsonString);
// Iterate over the '@graph' array to find the 'Product' object
let numRev = document.querySelector(".jdgm-rev-widg").dataset.numberOfReviews;
let avgRat = document.querySelector(".jdgm-rev-widg").dataset.averageRating;
data['@graph'].forEach((item) => {
if (item['@type'] === 'Product') {
// Check if the 'Product' object has an 'aggregateRating' property
if (item.hasOwnProperty('aggregateRating')) {
// Add 'ratingValue' and 'ratingCount' properties
item['aggregateRating']['ratingValue'] = avgRat;
item['aggregateRating']['ratingCount'] = numRev;
}
}
});
// Convert the modified object back to a JSON string
let modifiedJsonString = JSON.stringify(data);
// Update the script tag content
scriptTag.textContent = modifiedJsonString;
}
});
</script>
<?php } );Editor is loading...