Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 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 ),
- 'sku' => get_post_meta($variation['variation_id'],'variable_sku', true ),
- ];
- }
- }
- return $entity;
- } );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement