Advertisement
Guest User

Untitled

a guest
May 20th, 2022
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 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. return $entity;
  9. }
  10.  
  11. $variations = $product->get_available_variations();
  12. if ( ! empty( $variations ) ) {
  13. $offers = [];
  14. foreach ( $variations as $variation ) {
  15. $price_valid_until = get_post_meta( $variation['variation_id'], '_sale_price_dates_to', true );
  16. $offers[] = [
  17. '@type' => 'Offer',
  18. 'description' => strip_tags( $variation['variation_description'] ),
  19. 'price' => $variation['display_price'],
  20. 'priceCurrency' => get_woocommerce_currency(),
  21. 'availability' => $variation['is_in_stock'] ? 'https://schema.org/InStock' : 'https://schema.org/OutOfStock',
  22. 'itemCondition' => 'NewCondition',
  23. 'priceValidUntil' => $price_valid_until ? date_i18n( 'Y-m-d', $price_valid_until ) : '2025-12-31',
  24. 'url' => $product->get_permalink(),
  25. 'sku' => $variation['sku'],
  26. 'color' => $variation['attributes']['attribute_pa_color'], //name of the attribute
  27. 'size' => $variation['attributes']['attribute_pa_size'] //name of the attribute
  28. ];
  29. }
  30. }
  31.  
  32. $entity['offers'] = $offers;
  33.  
  34. return $entity;
  35. } );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement