-
Hello,
I am using the following code to otpimize my structured data. I use offer since all of my products are variable products.
// GTIN données structurées 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' ) ) { $entity['gtin13'] = get_post_meta( get_the_ID(), 'hwp_var_gtin', true ); return $entity; } $variations = $product->get_available_variations(); if ( ! empty( $variations ) ) { $entity['gtin13'] = get_post_meta( $variations[0]['variation_id'], 'hwp_var_gtin', true ); } return $entity; } ); 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' ) ) { $entity['gtin13'] = get_post_meta( get_the_ID(), 'hwp_var_gtin', true ); return $entity; } $variations = $product->get_available_variations(); if ( ! empty( $variations ) ) { $entity['offers'] = []; foreach ( $variations as $variation ) { $price_valid_until = get_post_meta( $variation['variation_id'], '_sale_price_dates_to', true ); $entity['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(), 'gtin13' => get_post_meta($variation['variation_id'],'hwp_var_gtin', true ), ]; } } return $entity; } );
As you can see by testing on this url: https://lerepaireduchef.fr/barbecue-kamado/
I do have a GTIN code for each offer and therefore each variation. Likewise, I would like to have a SKU code for each of the offers and variations. Everything is already well filled in woocommerce.
Thanks in advance
Viewing 6 replies - 1 through 6 (of 6 total)
Viewing 6 replies - 1 through 6 (of 6 total)
The ticket ‘SKU Offer for each variation’ is closed to new replies.