I'm new to coding and wordpress/woocommerce and am trying to find a simple way to edit the output of wc_get_rating_html.
At the moment it outputs the following:
function wc_get_rating_html( $rating ) {
if ( $rating > 0 ) {
$rating_html = '<div class="star-rating" title="' . sprintf( esc_attr__( 'Rated %s out of 5', 'woocommerce' ), $rating ) . '">';
$rating_html .= '<span style="width:' . ( ( $rating / 5 ) * 100 ) . '%"><strong class="rating">' . $rating . '</strong> ' . esc_html__( 'out of 5', 'woocommerce' ) . '</span>';
$rating_html .= '</div>';
} else {
$rating_html = '';
}
return apply_filters( 'woocommerce_product_get_rating_html', $rating_html, $rating );
}
I simply want to output the star rating and not the text that goes before and after. Any suggestions on how i can do this as i obv cant edit the template file. thanks
Wordpress utilizes filters which allow you to replace and/or modify the output of what the filter processes. Here is the full block for the
wc_get_rating_htmlfunction:In your example,
woocommerce_product_get_rating_htmlis the filter name and takes three arguments:$rating_htmlwhich represents the output which the user sees$ratingwhich is the numerical rating$countwhich is the total number of ratings.If you wish to replace the output, you'll need to hook into the filter
woocommerce_product_get_rating_htmlwith a function that will return the modified content. You'll definitely need to review Hooks & Filters and how they work in Wordpress if you are not familiar with utilizing hooks and filters in Wordpress.