* WooCommerce product embed
* @package WooCommerce\Classes\Embed
use Automattic\WooCommerce\Enums\ProductType;
if ( ! defined( 'ABSPATH' ) ) {
* Embed Class which handles any WooCommerce Products that are embedded on this site or another site.
public static function init() {
// Filter all of the content that's going to be embedded.
add_filter( 'the_excerpt_embed', array( __CLASS__, 'the_excerpt' ), 10 );
// Make sure no comments display. Doesn't make sense for products.
add_action( 'embed_content_meta', array( __CLASS__, 'remove_comments_button' ), 5 );
// In the comments place let's display the product rating.
add_action( 'embed_content_meta', array( __CLASS__, 'get_ratings' ), 5 );
// Add some basic styles.
add_action( 'embed_head', array( __CLASS__, 'print_embed_styles' ) );
* Remove comments button on product embeds.
public static function remove_comments_button() {
if ( self::is_embedded_product() ) {
remove_action( 'embed_content_meta', 'print_embed_comments_button' );
* Check if this is an embedded product - to make sure we don't mess up regular posts.
public static function is_embedded_product() {
if ( function_exists( 'is_embed' ) && is_embed() && is_product() ) {
* Create the excerpt for embedded products - we want to add the buy button to it.
* @param string $excerpt Embed short description.
public static function the_excerpt( $excerpt ) {
$_product = wc_get_product( get_the_ID() );
// Make sure we're only affecting embedded products.
if ( self::is_embedded_product() ) {
echo '<p><span class="wc-embed-price">' . $_product->get_price_html() . '</span></p>'; // WPCS: XSS ok.
if ( ! empty( $post->post_excerpt ) ) {
woocommerce_template_single_excerpt();
$excerpt = ob_get_clean();
$excerpt .= self::product_buttons();
* Create the button to go to the product page for embedded products.
public static function product_buttons() {
$_product = wc_get_product( get_the_ID() );
$button = '<a href="%s" class="wp-embed-more wc-embed-button">%s</a>';
if ( $_product->is_type( ProductType::SIMPLE ) && $_product->is_purchasable() && $_product->is_in_stock() ) {
$buttons[] = sprintf( $button, esc_url( add_query_arg( 'add-to-cart', get_the_ID(), wc_get_cart_url() ) ), esc_html__( 'Buy now', 'woocommerce' ) );
$buttons[] = sprintf( $button, get_the_permalink(), esc_html__( 'Read more', 'woocommerce' ) );
return '<p>' . implode( ' ', $buttons ) . '</p>';
* Prints the markup for the rating stars.
public static function get_ratings() {
// Make sure we're only affecting embedded products.
if ( ! self::is_embedded_product() ) {
$_product = wc_get_product( get_the_ID() );
if ( $_product && $_product->get_average_rating() > 0 ) {
<div class="wc-embed-rating">
/* translators: %s: average rating */
esc_html__( 'Rated %s out of 5', 'woocommerce' ),
esc_html( $_product->get_average_rating() )
public static function print_embed_styles() {
if ( ! self::is_embedded_product() ) {
box-shadow: 0px 1px 0 0px rgba(0, 0, 0, 0.05);
a.wc-embed-button:hover, a.wc-embed-button:focus {
box-shadow: 0px 1px 0 0px rgba(0, 0, 0, 0.1);