WC_Embed

Embed Class which handles any WooCommerce Products that are embedded on this site or another site.

Defined (1)

The class is defined in the following location(s).

/includes/class-wc-embed.php  
  1. class WC_Embed { 
  2.  
  3. /** 
  4. * Init embed class. 
  5. * @since 2.4.11 
  6. */ 
  7. public static function init() { 
  8.  
  9. // Filter all of the content that's going to be embedded. 
  10. add_filter( 'the_excerpt_embed', array( __CLASS__, 'the_excerpt' ), 10 ); 
  11.  
  12. // Make sure no comments display. Doesn't make sense for products. 
  13. add_action( 'embed_content_meta', array( __CLASS__, 'remove_comments_button' ), 5 ); 
  14.  
  15. // In the comments place let's display the product rating. 
  16. add_action( 'embed_content_meta', array( __CLASS__, 'get_ratings' ), 5 ); 
  17.  
  18. // Add some basic styles. 
  19. add_action( 'embed_head', array( __CLASS__, 'print_embed_styles' ) ); 
  20.  
  21. /** 
  22. * Remove comments button on product embeds. 
  23. * @since 2.6.0 
  24. */ 
  25. public static function remove_comments_button() { 
  26. if ( self::is_embedded_product() ) { 
  27. remove_action( 'embed_content_meta', 'print_embed_comments_button' ); 
  28.  
  29. /** 
  30. * Check if this is an embedded product - to make sure we don't mess up regular posts. 
  31. * @since 2.4.11 
  32. * @return bool 
  33. */ 
  34. public static function is_embedded_product() { 
  35. if ( function_exists( 'is_embed' ) && is_embed() && is_product() ) { 
  36. return true; 
  37. return false; 
  38.  
  39. /** 
  40. * Create the excerpt for embedded products - we want to add the buy button to it. 
  41. * @since 2.4.11 
  42. * @param string $excerpt Embed short description. 
  43. * @return string 
  44. */ 
  45. public static function the_excerpt( $excerpt ) { 
  46. global $post; 
  47.  
  48. // Get product. 
  49. $_product = wc_get_product( get_the_ID() ); 
  50.  
  51. // Make sure we're only affecting embedded products. 
  52. if ( self::is_embedded_product() ) { 
  53. echo '<p><span class="wc-embed-price">' . $_product->get_price_html() . '</span></p>'; 
  54.  
  55. if ( ! empty( $post->post_excerpt ) ) { 
  56. ob_start(); 
  57. woocommerce_template_single_excerpt(); 
  58. $excerpt = ob_get_clean(); 
  59.  
  60. // Add the button. 
  61. $excerpt .= self::product_buttons(); 
  62. return $excerpt; 
  63.  
  64. /** 
  65. * Create the button to go to the product page for embedded products. 
  66. * @since 2.4.11 
  67. * @return string 
  68. */ 
  69. public static function product_buttons() { 
  70. $_product = wc_get_product( get_the_ID() ); 
  71. $buttons = array(); 
  72. $button = '<a href="%s" class="wp-embed-more wc-embed-button">%s</a>'; 
  73.  
  74. if ( $_product->is_type( 'simple' ) && $_product->is_purchasable() && $_product->is_in_stock() ) { 
  75. $buttons[] = sprintf( $button, esc_url( add_query_arg( 'add-to-cart', get_the_ID(), wc_get_cart_url() ) ), esc_html__( 'Buy now', 'woocommerce' ) ); 
  76.  
  77. $buttons[] = sprintf( $button, get_the_permalink(), esc_html__( 'Read more', 'woocommerce' ) ); 
  78.  
  79. return '<p>' . implode( ' ', $buttons ) . '</p>'; 
  80.  
  81. /** 
  82. * Prints the markup for the rating stars. 
  83. * @since 2.4.11 
  84. */ 
  85. public static function get_ratings() { 
  86. // Make sure we're only affecting embedded products. 
  87. if ( self::is_embedded_product() && ( $_product = wc_get_product( get_the_ID() ) ) && $_product->get_average_rating() > 0 ) { 
  88. ?> 
  89. <div class="wc-embed-rating"> 
  90. <?php 
  91. /** translators: %s: average rating */ 
  92. printf( 
  93. esc_html_( 'Rated %s out of 5', 'woocommerce' ),  
  94. esc_html( $_product->get_average_rating() ) 
  95. ); 
  96. ?> 
  97. </div> 
  98. <?php 
  99.  
  100. /** 
  101. * Basic styling. 
  102. */ 
  103. public static function print_embed_styles() { 
  104. if ( ! self::is_embedded_product() ) { 
  105. return; 
  106. ?> 
  107. <style type="text/css"> 
  108. a.wc-embed-button { 
  109. border-radius: 4px; 
  110. border: 1px solid #ddd; 
  111. box-shadow: 0px 1px 0 0px rgba(0, 0, 0, 0.05); 
  112. display:inline-block; 
  113. padding: .5em; 
  114. a.wc-embed-button:hover, a.wc-embed-button:focus { 
  115. border: 1px solid #ccc; 
  116. box-shadow: 0px 1px 0 0px rgba(0, 0, 0, 0.1); 
  117. color: #999; 
  118. text-decoration: none; 
  119. .wp-embed-excerpt p { 
  120. margin: 0 0 1em; 
  121. .wc-embed-price { 
  122. display: block; 
  123. opacity: .75; 
  124. font-weight: 700; 
  125. margin-top: -.75em; 
  126. .wc-embed-rating { 
  127. display: inline-block; 
  128. </style> 
  129. <?php