Advertisement
jaideep06

Untitled

Apr 28th, 2021
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.25 KB | None | 0 0
  1. add_filter( 'rank_math/snippet/rich_snippet_product_entity', function( $entity ) {
  2.     if ( ! is_product() ) {
  3.         return $entity;
  4.     }
  5.  
  6.     $product = wc_get_product( get_the_ID() );
  7.     if ( ! $product->is_type( 'variable' ) ) {
  8.         $entity['gtin13'] = get_post_meta( get_the_ID(), 'hwp_var_gtin', true );
  9.         return $entity;
  10.     }
  11.  
  12.     $variations = $product->get_available_variations();
  13.     if ( ! empty( $variations ) ) {
  14.         $entity['offers'] = [];
  15.         foreach ( $variations as $variation ) {
  16.             $price_valid_until = get_post_meta( $variation['variation_id'], '_sale_price_dates_to', true );
  17.             $entity['offers'][] = [
  18.                 '@type'           => 'Offer',
  19.                 'description'     => strip_tags( $variation['variation_description'] ),
  20.                 'price'           => $variation['display_price'],
  21.                 'priceCurrency'   => get_woocommerce_currency(),
  22.                 'availability'    => $variation['is_in_stock'] ? 'https://schema.org/InStock' : 'https://schema.org/OutOfStock',
  23.                 'itemCondition'   => 'NewCondition',
  24.                 'priceValidUntil' => $price_valid_until ? date_i18n( 'Y-m-d', $price_valid_until ) : '2025-12-31',
  25.                 'url'             => $product->get_permalink(),
  26.                 'sku'             => get_post_meta($variation['variation_id'],'variable_sku', true ),
  27.             ];
  28.         }
  29.     }
  30.  
  31.     return $entity;
  32. } );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement