Advertisement
Guest User

Untitled

a guest
Oct 3rd, 2019
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.96 KB | None | 0 0
  1. add_action('wp_footer', function() {
  2. global $product;
  3.  
  4. if( ! avia_woocommerce_version_check( '3.0.0') )
  5. {
  6. return;
  7. }
  8.  
  9. if( ! is_product() || ! $product instanceof WC_Product )
  10. {
  11. return;
  12. }
  13.  
  14. if( get_post_meta( $product->get_id(), '_aviaLayoutBuilderCleanData', true ) ) {
  15. $data = avia_generate_product_data();
  16.  
  17. if ( $data ) {
  18. echo '<script type="application/ld+json">' . wc_esc_json( wp_json_encode( $data ), true ) . '</script>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
  19. }
  20. }
  21. }, 10);
  22.  
  23. function avia_generate_product_data( $product = null ) {
  24. if ( ! is_object( $product ) ) {
  25. global $product;
  26. }
  27.  
  28. if ( ! is_a( $product, 'WC_Product' ) ) {
  29. return;
  30. }
  31.  
  32. $shop_name = get_bloginfo( 'name' );
  33. $shop_url = home_url();
  34. $currency = get_woocommerce_currency();
  35. $permalink = get_permalink( $product->get_id() );
  36.  
  37. $markup = array(
  38. '@type' => 'Product',
  39. '@id' => $permalink . '#product', // Append '#product' to differentiate between this @id and the @id generated for the Breadcrumblist.
  40. 'name' => $product->get_name(),
  41. 'url' => $permalink,
  42. 'image' => wp_get_attachment_url( $product->get_image_id() ),
  43. 'description' => wp_strip_all_tags( do_shortcode( $product->get_short_description() ? $product->get_short_description() : $product->get_description() ) ),
  44. );
  45.  
  46. // Declare SKU or fallback to ID.
  47. if ( $product->get_sku() ) {
  48. $markup['sku'] = $product->get_sku();
  49. } else {
  50. $markup['sku'] = $product->get_id();
  51. }
  52.  
  53. if ( '' !== $product->get_price() ) {
  54. // Assume prices will be valid until the end of next year, unless on sale and there is an end date.
  55. $price_valid_until = date( 'Y-12-31', current_time( 'timestamp', true ) + YEAR_IN_SECONDS );
  56.  
  57. if ( $product->is_type( 'variable' ) ) {
  58. $lowest = $product->get_variation_price( 'min', false );
  59. $highest = $product->get_variation_price( 'max', false );
  60.  
  61. if ( $lowest === $highest ) {
  62. $markup_offer = array(
  63. '@type' => 'Offer',
  64. 'price' => wc_format_decimal( $lowest, wc_get_price_decimals() ),
  65. 'priceValidUntil' => $price_valid_until,
  66. 'priceSpecification' => array(
  67. 'price' => wc_format_decimal( $lowest, wc_get_price_decimals() ),
  68. 'priceCurrency' => $currency,
  69. 'valueAddedTaxIncluded' => wc_prices_include_tax() ? 'true' : 'false',
  70. ),
  71. );
  72. } else {
  73. $markup_offer = array(
  74. '@type' => 'AggregateOffer',
  75. 'lowPrice' => wc_format_decimal( $lowest, wc_get_price_decimals() ),
  76. 'highPrice' => wc_format_decimal( $highest, wc_get_price_decimals() ),
  77. 'offerCount' => count( $product->get_children() ),
  78. );
  79. }
  80. } else {
  81. if ( $product->is_on_sale() && $product->get_date_on_sale_to() ) {
  82. $price_valid_until = date( 'Y-m-d', $product->get_date_on_sale_to()->getTimestamp() );
  83. }
  84. $markup_offer = array(
  85. '@type' => 'Offer',
  86. 'price' => wc_format_decimal( $product->get_price(), wc_get_price_decimals() ),
  87. 'priceValidUntil' => $price_valid_until,
  88. 'priceSpecification' => array(
  89. 'price' => wc_format_decimal( $product->get_price(), wc_get_price_decimals() ),
  90. 'priceCurrency' => $currency,
  91. 'valueAddedTaxIncluded' => wc_prices_include_tax() ? 'true' : 'false',
  92. ),
  93. );
  94. }
  95.  
  96. $markup_offer += array(
  97. 'priceCurrency' => $currency,
  98. 'availability' => 'http://schema.org/' . ( $product->is_in_stock() ? 'InStock' : 'OutOfStock' ),
  99. 'url' => $permalink,
  100. 'seller' => array(
  101. '@type' => 'Organization',
  102. 'name' => $shop_name,
  103. 'url' => $shop_url,
  104. ),
  105. );
  106.  
  107. $markup['offers'] = array( apply_filters( 'woocommerce_structured_data_product_offer', $markup_offer, $product ) );
  108. }
  109.  
  110. if ( $product->get_rating_count() && wc_review_ratings_enabled() ) {
  111. $markup['aggregateRating'] = array(
  112. '@type' => 'AggregateRating',
  113. 'ratingValue' => $product->get_average_rating(),
  114. 'reviewCount' => $product->get_review_count(),
  115. );
  116.  
  117. // Markup most recent rating/review.
  118. $comments = get_comments(
  119. array(
  120. 'number' => 1,
  121. 'post_id' => $product->get_id(),
  122. 'status' => 'approve',
  123. 'post_status' => 'publish',
  124. 'post_type' => 'product',
  125. 'parent' => 0,
  126. 'meta_key' => 'rating',
  127. 'orderby' => 'meta_value_num',
  128. )
  129. );
  130.  
  131. if ( $comments ) {
  132. foreach ( $comments as $comment ) {
  133. $rating = get_comment_meta( $comment->comment_ID, 'rating', true );
  134.  
  135. if ( ! $rating ) {
  136. continue;
  137. }
  138.  
  139. $markup['review'] = array(
  140. '@type' => 'Review',
  141. 'reviewRating' => array(
  142. '@type' => 'Rating',
  143. 'ratingValue' => $rating,
  144. ),
  145. 'author' => array(
  146. '@type' => 'Person',
  147. 'name' => get_comment_author( $comment->comment_ID ),
  148. ),
  149. );
  150. }
  151. }
  152. }
  153.  
  154. return $markup;
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement