Hello,
Thank you for contacting Rank Math and bringing your concern to our attention. I’m sorry for any inconvenience this issue may have caused you.
You can use and customize this filter to add the shipping_weight tag to your schema:
https://rankmath.com/kb/filters-hooks-api-developer/#extend-json-ld-data
And here’s a sample filter you can use to change the unit value in case you wanted to use the data directly from your WooCommerce setup (kg):
add_filter( "rank_math/snippet/rich_snippet_product_entity", function( $entity ) {
global $product;
if (is_product()) {
$entity["shipping_weight"] = $product->get_weight()." kg";
return $entity;
}
return $entity;
});
Hope that helps.
Thank you.
Hi, thank you Reinelle for the tip ! Don’t worry, actually rank math is much more a solution than a problem 🙂
I have tested your solution, and it seems to work fine, but only in case of normal products. In the case of variable products, the weight and shipping_weight fields are empty. My problem is that each variation has its own weight. Do you have any idee of how I can deal with that ?
thank you very much
Xavier
Hello,
I have modified the filter to work for variable products. Please replace the previous filter with the one below:
add_filter( "rank_math/snippet/rich_snippet_product_entity", function( $entity ) {
global $product;
if (is_product()) {
if( $product->is_type( 'variable' ) ){
foreach ( $product->get_visible_children() as $variation_id ) {
$variation = wc_get_product( $variation_id ); // Get the product variation object
$weight = $variation->get_weight(); // Get weight from variation
$entity["shipping_weight"] = $weight." kg";
}
}
$entity["shipping_weight"] = $product->get_weight()." kg";
return $entity;
}
return $entity;
});
Hope that helps. Please let us know if you have questions.
Ok thanks a lot, it works fine. I just put the line “$entity[“shipping_weight”] = $product->get_weight().” kg”;” before “if( $product->is_type( ‘variable’ ) )” because the result was overwritten in case of variable products.
Have a great day.
Xavier
Hello,
We are super happy that this resolved your issue. If you have any other questions in the future, know that we are here to help you.
If you don’t mind me asking, could you please leave us a review (if you haven’t already) on https://wordpress.org/support/plugin/seo-by-rank-math/reviews/#new-post about your overall experience with Rank Math? We appreciate your time and patience.
If you do have another question in the future, please feel free to create a new forum topic, and it will be our pleasure to assist you again.
Thank you.