Skip to content Skip to sidebar Skip to footer

Add Star Rating From A Product Id On Some Pages In Woocommerce

Is there a way to call to the star rating of a specific product on a custom page, at custom locations? In other words, can I add a star rating of a specific tshirt to appear under

Solution 1:

For a specific product ID you can use some dedicated WC_Product methods:

// Get an instance of the WC_Product Object (from a product ID)$product = wc_get_product( $product_id);

// The product rating count (number of reviews by rating )$rating_count = $product->get_rating_counts(); // Multidimensional array// The product average rating (or how many stars this product has)$average_rating = $product->get_average_rating();

// Testing Outputecho'<p>Rating average: '.$average_rating.' stars</p>';

To display the product "stars" average rating:

You can use the dedicated function wc_get_rating_html() with get_average_rating()WC_Product method. So the necessary code will be:

// Get an instance of the WC_Product Object (from a product ID)$product = wc_get_product( $product_id);

// The product average rating (or how many stars this product has)$average_rating = $product->get_average_rating();

// The product stars average rating html formatted.$average_rating_html = wc_get_rating_html($average_rating);

// Display stars average rating htmlecho$average_rating_html;

Tested and works.

An interesting answer:Rating is showing numbers instead of stars


A shortcode to display a product star rating everywhere - 2 code versions:

1) The best way based on wc_get_rating_html() function (for Woocommerce 3+):

add_shortcode( 'product_rating', 'display_the_product_rating' );
functiondisplay_the_product_rating($atts) {
    // Shortcode attributes$atts = shortcode_atts( array(
        'id' => '',
    ), $atts, 'product_rating' );

    if ( isset($atts['id']) && $atts['id'] > 0 ):

    // Get an instance of the WC_Product Object$product = wc_get_product( $atts['id'] );

    // The product average rating (or how many stars this product has)$average = $product->get_average_rating();

    endif;

    if ( isset($average) ) :

    return wc_get_rating_html($average);

    endif;
}

2) The old way (also works):

add_shortcode( 'product_rating', 'display_the_product_rating' );
functiondisplay_the_product_rating($atts) {
    // Shortcode attributes$atts = shortcode_atts( array(
        'id' => '',
    ), $atts, 'product_rating' );

    if ( isset($atts['id']) && $atts['id'] > 0 ):

    // Get an instance of the WC_Product Object$product = wc_get_product( $atts['id'] );

    // The product average rating (or how many stars this product has)$average = $product->get_average_rating();

    // HERE the average width$average_width = $average * 16 . 'px';

    endif;

    if ( isset($average) ) :

    return'<div class="starwrapper" itemprop="aggregateRating" itemscope itemtype="http://schema.org/AggregateRating">
        <span class="star-rating" title="'.sprintf(__('Rated %s out of 5', 'woocommerce'), $average).'">
            <span style="width:'.$average_width.'">
                <span itemprop="ratingValue" class="rating">'.$average.'</span>
            </span>
        </span>
    </div><br clear="all">';

    endif;
}

Code goes in function.php file of the active child theme (or active theme). Tested and works.


USAGE EXAMPLES:

1) In the WordPress pages or posts text editor (Where 37 is the product ID):

[product_rating id='37']

2) In the php code:

echo do_shortcode( "[product_rating id='37']" );

3) Between html tags:

<?phpecho do_shortcode( "[product_rating id='37']" ); ?>

You will get something like: enter image description here

Post a Comment for "Add Star Rating From A Product Id On Some Pages In Woocommerce"