Advertisement
jaideep06

Variations

May 21st, 2021
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.96 KB | None | 0 0
  1. // GTIN données structurées
  2.  
  3. add_filter( 'rank_math/snippet/rich_snippet_product_entity', function( $entity ) {
  4.     if ( ! is_product() ) {
  5.         return $entity;
  6.     }
  7.    
  8.     $product = wc_get_product( get_the_ID() );
  9.     if ( ! $product->is_type( 'variable' ) ) {
  10.         $entity['gtin13'] = get_post_meta( get_the_ID(), 'hwp_var_gtin', true );
  11.         return $entity;
  12.     }
  13.     $variations = $product->get_available_variations();
  14.    
  15.     if ( ! empty( $variations ) ) {
  16.         $entity['gtin13'] = get_post_meta( $variations[0]['variation_id'], 'hwp_var_gtin', true );
  17.     }
  18.  
  19.     return $entity;
  20. } );
  21.  
  22. add_filter( 'rank_math/snippet/rich_snippet_product_entity', function( $entity ) {
  23.     if ( ! is_product() ) {
  24.         return $entity;
  25.     }
  26.  
  27.     $product = wc_get_product( get_the_ID() );
  28.     if ( ! $product->is_type( 'variable' ) ) {
  29.         $entity['gtin13'] = get_post_meta( get_the_ID(), 'hwp_var_gtin', true );
  30.         return $entity;
  31.     }
  32.  
  33.     $variations = $product->get_available_variations();
  34.     if ( ! empty( $variations ) ) {
  35.         $entity['offers'] = [];
  36.         foreach ( $variations as $variation ) {
  37.             $price_valid_until = get_post_meta( $variation['variation_id'], '_sale_price_dates_to', true );
  38.             $entity['offers'][] = [
  39.                 '@type'           => 'Offer',
  40.                 'description'     => strip_tags( $variation['variation_description'] ),
  41.                 'price'           => $variation['display_price'],
  42.                 'priceCurrency'   => get_woocommerce_currency(),
  43.                 'availability'    => $variation['is_in_stock'] ? 'https://schema.org/InStock' : 'https://schema.org/OutOfStock',
  44.                 'itemCondition'   => 'NewCondition',
  45.                 'priceValidUntil' => $price_valid_until ? date_i18n( 'Y-m-d', $price_valid_until ) : '2025-12-31',
  46.                 'url'             => $product->get_permalink(),
  47.                 'gtin13'          => get_post_meta($variation['variation_id'],'hwp_var_gtin', true ),
  48.                 'sku'             => get_post_meta($variation['variation_id'],'variable_sku', true ),
  49.             ];
  50.         }
  51.     }
  52.  
  53.     return $entity;
  54. } );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement