Function

(Woo Function) Output Product Rating – woocommerce_template_single_rating()

Pinterest LinkedIn Tumblr

Hi, Woolovers πŸ‘‹πŸΌ Today we’re going to review woocommerce_template_single_rating function which outputs product rating.

Details

Boring but sometimes useful staff.

TypeFunction
Full Namewoocommerce_template_single_rating
Added3.0.2

What does it do?

Output Product Rating

How WordPress Use?

wc-template-functions.php

if ( ! function_exists( 'woocommerce_template_single_rating' ) ) {

	/**
	 * Output the product rating.
	 */
	function woocommerce_template_single_rating() {
		if ( post_type_supports( 'product', 'comments' ) ) {
			wc_get_template( 'single-product/rating.php' );
		}
	}
}

wc-template-hooks.php
/**
 * Product Summary Box.
 *
 * @see woocommerce_template_single_title()
 * @see woocommerce_template_single_rating()
 * @see woocommerce_template_single_price()
 * @see woocommerce_template_single_excerpt()
 * @see woocommerce_template_single_meta()
 * @see woocommerce_template_single_sharing()
 */
add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_title', 5 );
add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_rating', 10 );
add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 20 );
add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_meta', 40 );
add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_sharing', 50 );
content-single-products.php
	<div class="summary entry-summary">
		<?php
		/**
		 * Hook: woocommerce_single_product_summary.
		 *
		 * @hooked woocommerce_template_single_title - 5
		 * @hooked woocommerce_template_single_rating - 10
		 * @hooked woocommerce_template_single_price - 10
		 * @hooked woocommerce_template_single_excerpt - 20
		 * @hooked woocommerce_template_single_add_to_cart - 30
		 * @hooked woocommerce_template_single_meta - 40
		 * @hooked woocommerce_template_single_sharing - 50
		 * @hooked WC_Structured_Data::generate_product_data() - 60
		 */
		do_action( 'woocommerce_single_product_summary' );
		?>
	</div>

Code Examples Which includes woocommerce_template_single_rating

Show Product Percentage instead of Comment Count on WooCommerce
remove_action('woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_rating', 5);
remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_rating', 10);

add_action( 'woocommerce_after_shop_loop_item_title', 'amc_function_add_rating_percentage', 5  );
add_action( 'woocommerce_single_product_summary', 'amc_function_add_rating_percentage', 5  );
function  amc_function_add_rating_percentage() { 
    global $product;  
    $rating_count = $product->get_rating_count(); 
    $average      = $product->get_average_rating(); 
    $average_percent = $average * 100/5;
    if($rating_count >0){
        echo '<div class="woocommerce-product-rating">'.wc_get_rating_html( $average, $rating_count ).esc_html__( '('.$average_percent.'% customers satisfied )' , 'amc' ).'</div>'; 
    }
    
}

Source from Our WooCommerce Blog

Move single product stars rating in WooCommerce
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_rating', 10 );
add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_rating', 3 );

Source from Stackoverflow – Answered by LoicTheAztec