Skip to content

Instantly share code, notes, and snippets.

@santanup789
Created January 5, 2022 10:59
Show Gist options
  • Save santanup789/1cb3ba4e31ec3cae43a03ff0f5e05f8e to your computer and use it in GitHub Desktop.
Save santanup789/1cb3ba4e31ec3cae43a03ff0f5e05f8e to your computer and use it in GitHub Desktop.
Re arrange or add WooCommer product ratings with number of reviews before shop loop item title using woocommerce hook
<?php
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_rating', 5 );// remoove default star rating from shop listing page
//add star rating before shop loop title
add_filter( 'woocommerce_before_shop_loop_item_title', 'star_rating' );
function star_rating() {
global $product;
if ( get_option( 'woocommerce_enable_review_rating' ) === 'no' ) {
return;
}
$rating_count = $product->get_rating_count();
$review_count = $product->get_review_count();
$average = $product->get_average_rating();
if ( $rating_count >= 0 ) : ?>
<?php echo "<div class='ratings'>".wc_get_rating_html($average, $rating_count); ?>
<?php if ( comments_open() ): ?>(<?php printf( _n( '%s',$review_count,'woocommerce' ), '<span class="count">' . esc_html( $review_count ) . '</span>' ); ?>)<?php
endif;
echo "</div>"; ?>
<?php endif;
}
//END----add star rating before shop loop title
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment