add_filter('woocommerce_structured_data_product', 'add_missing_structured_data_fields', 10, 2);
function add_missing_structured_data_fields($markup, $product) {
$reviews = get_comments(array(
'post_id' => $product->get_id(),
'status' => 'approve',
'type' => 'review'
));
if ($reviews) {
$markup['review'] = array();
foreach ($reviews as $review) {
$rating = get_comment_meta($review->comment_ID, 'rating', true);
$markup['review'][] = array(
'@type' => 'Review',
'author' => array(
'@type' => 'Person',
'name' => get_comment_author($review->comment_ID)
),
'datePublished' => get_comment_date('c', $review->comment_ID),
'description' => get_comment_text($review->comment_ID),
'reviewRating' => array(
'@type' => 'Rating',
'ratingValue' => $rating ? $rating : 5,
'bestRating' => 5,
'worstRating' => 1
)
);
}
}
if ($product->get_rating_count() > 0) {
$markup['aggregateRating'] = array(
'@type' => 'AggregateRating',
'ratingValue' => $product->get_average_rating(),
'reviewCount' => $product->get_review_count(),
'bestRating' => '5',
'worstRating' => '1'
);
}
$markup['shippingDetails'] = array(
'@type' => 'OfferShippingDetails',
'shippingRate' => array(
'@type' => 'MonetaryAmount',
'value' => '0',
'currency' => get_woocommerce_currency()
),
'shippingDestination' => array(
'@type' => 'DefinedRegion',
'addressCountry' => 'RU'
)
);
$markup['hasMerchantReturnPolicy'] = array(
'@type' => 'MerchantReturnPolicy',
'applicableCountry' => 'RU',
'returnPolicyCategory' => 'https://schema.org/MerchantReturnFiniteReturnWindow',
'merchantReturnDays' => 14,
'returnMethod' => 'https://schema.org/ReturnByMail',
'returnFees' => 'https://schema.org/FreeReturn'
);
return $markup;
}