I am using this snippet to display ean value for gtin in Woocommerce’s product schema:
add_filter( 'woocommerce_structured_data_product', 'filter_woocommerce_structured_data_product', 10, 2 );
function filter_woocommerce_structured_data_product( $markup, $product ) {
if ( empty( $markup[ 'gtin8' ] ) ) {
$markup[ 'gtin8' ] = get_post_meta( $product->get_id(), 'ean', true );
}
return $markup;
}
This works but I need to set "identifier_exists" markup to products that don’t have the custom field ean set. How can I modify my snippet to show the ean value in the markup if it exists, and add the identifier_exists attribute = false to products that don’t have an ean?
2
Answers
Try the following:
Code goes in functions.php file of the active child theme (or active theme).
The
gtin
field must be set based on its length.Here you will find the complete documentation with all available fields.
Finally, the
gtin
field is optional. If your product does not have the EAN code (or any other identifier) you can simply not set thegtin
.Even if you set the
identifier_exists
field to no or false you will still see the "No global identifier provided (e.g., gtin, mpn, isbn) (optional)" warning. You can just ignore it. In fact, it is not reported in the documentation.You can do some tests here: https://search.google.com/test/rich-results
The code has been tested and works. Add it to your active theme’s functions.php.
Related answer: