fbpx

How to make the rating star inactive on WooCommerce stores

If a WooCommerce store wanted to make it more obvious that a product had not yet been rated, they could use this snippet to make the rating star inactive when it’s 0, thus providing more clarity to customers.


    add_filter('woocommerce_product_get_rating_html', __NAMESPACE__ . '\\codeart_custom_rating_html', 10, 2);
function codeart_custom_rating_html($rating_html, $rating) {
  if ( $rating > 0 ) {
    $title = sprintf( __( 'Ocjenjeno %s od 5', 'woocommerce' ), $rating );
    $rating_html  = '
'; $rating_html .= '' . $rating . ' ' . __( 'out of 5', 'woocommerce' ) . ''; $rating_html .= '
'; return $rating_html; } else { $title = 'Još nije ocjenjeno'; $rating = 5; $rating_html = '
'; $rating_html .= '' . $rating . ' ' . __( 'out of 5', 'woocommerce' ) . ''; $rating_html .= '
'; return $rating_html; } }

This snippet is a function that is hooked into the “woocommerce_product_get_rating_html” filter. It takes in two parameters, a rating html string and a rating number. The function first checks if the rating is greater than 0. If it is, it creates a new rating html string with the rating value and the text “out of 5”. If the rating is 0, the function creates a new rating html string with a rating of 5 and the text “out of 5”, but also adds the class “inactive” to indicate that the product has not yet been rated. The new rating html string is then returned.

Hire your Codeartist

Looking for a development of your own project?

Is your agency looking for a partner?