Hello,
Thank you for contacting Rank Math today.
Yes, it’s perfectly possible to extract those values into the Schema with the help of the following filter and some custom code inside it:
add_filter( 'rank_math/snippet/rich_snippet_product_entity', function( $entity ) {
return $entity;
});
The default way to retrieve a field from the post meta table is the following:
get_postmeta('id_of_page_post_product', 'field_name', true);
Hope this helps clarify your doubts.
Don’t hesitate to get in touch if you have any other questions.
Hi Miguel,
Thank you for the fast response.
How would I go about filling in Schema data for Variable products?
Here is an example product:
https://discountchemist.com.au/product/endura-sports-energy-gel-20-x-35g-sachets/
That is 8 products so naturally they all have different GTINs and I would like Google to know that I sell 8 different products but under the same link.
Hello,
You can further expand the filter and follow the example code I shared below:
add_filter( 'rank_math/snippet/rich_snippet_product_entity', function( $entity ) {
$product = new WC_Product( get_the_ID() );
if( $product->is_type( 'simple' ) ){
$gtin_value = get_post_meta($product->get_id(),'_woosea_gtin', true);
$entity['gtin13'] = $gtin_value;
}elseif($product->is_type( 'variable' )){
$variation = new WC_Product_Variation(get_the_ID());
$gtin_value = get_post_meta($variation->get_id(), '_woosea_gtin', true);
$entity['gtin13'] = $gtin_value;
}
return $entity;
});
Kindly modify the code as per requirement.
Hope that helps and please do not hesitate to let us know if you need our assistance with anything else.
Hi Jeremy,
According to my rich results test it’s saying that there is only 1 product in that link. Do I need the PRO version to have variations included?
Hello,
Thank you for contacting us and sorry for any inconvenience that might have been caused due to that.
To control the Schema markup for the offers in the variations you need to edit the following filter:
add_filter( 'rank_math/snippet/rich_snippet_product_entity', function( $entity ) {
if ( ! is_product() ) {
return $entity;
}
$product = wc_get_product( get_the_ID() );
if ( ! $product->is_type( 'variable' ) ) {
return $entity;
}
$variations = $product->get_available_variations();
if ( ! empty( $variations ) ) {
$offers = [];
foreach ( $variations as $variation ) {
$price_valid_until = get_post_meta( $variation['variation_id'], '_sale_price_dates_to', true );
$offers[] = [
'@type' => 'Offer',
'description' => strip_tags( $variation['variation_description'] ),
'price' => $variation['display_price'],
'priceCurrency' => get_woocommerce_currency(),
'availability' => $variation['is_in_stock'] ? 'https://schema.org/InStock' : 'https://schema.org/OutOfStock',
'itemCondition' => 'NewCondition',
'priceValidUntil' => $price_valid_until ? date_i18n( 'Y-m-d', $price_valid_until ) : '2025-12-31',
'url' => $product->get_permalink(),
];
}
}
$entity['offers'] = $offers;
return $entity;
} );
You can add or remove properties from the offers as you see fit and make the Schema markup exactly as you want.
Hope this helps solve your issues.
Don’t hesitate to get in touch if you have any other questions.
Hello,
Since we did not hear back from you for 15 days, we are assuming that you found the solution. We are closing this support ticket.
If you still need assistance or any other help, please feel free to open a new support ticket, and we will be more than happy to assist.
Thank you.