Home > Mobile >  WordPress WooCommrce Empty Star Rating Issue
WordPress WooCommrce Empty Star Rating Issue

Time:09-10

I am trying to add empty star rating to those products which don't have any rating. I am using the below code for this purpose. `


add_action('woocommerce_after_shop_loop_item_title', 'get_star_rating' );

function get_star_rating()
{
    global $woocommerce, $product;
    $rating_count = $product->get_rating_count();

    if ($rating_count == 0){
   $rating_html  = '</a><a href="' . get_the_permalink() . '#respond"><div ><span style="width:' . (( $rating / 5 ) * 100) . '%"></span></div><span ></span></a>';
    echo $rating_html;
    }
}

The problem with the above code is that it shows empty stars after price and the products which have rating show stars above price. See the image for refrence

I want that empty stars must show above the price. Anyone please guide me how can I achive that?

CodePudding user response:

Just put this code in your theme's functions.php file and remove your above code that you mentioned in question.

add_filter(
    'woocommerce_product_get_rating_html',
    function( $html, $rating, $count ) {
        /* translators: %s: rating */
        $label = sprintf( __( 'Rated %s out of 5', 'woocommerce' ), $rating );
        $html  = '<div  role="img" aria-label="' . esc_attr( $label ) . '">' . wc_get_star_rating_html( $rating, $count ) . '</div>';

        return $html;
    },
    10,
    3
);
  • Related