Site logo

Archived topic

PHP code for review count on product archive pages

1 reply · Started by Jordan on March 15, 2023

Viewing posts 1–2 of 2

This is in reference to this thread: https://generatepress.com/forums/topic/displaying-number-of-reviews-on-product-archive-pages-in-woocommerce/page/2/

I am using this PHP in the Code Snippets plugin `add_filter('woocommerce_product_get_rating_html', function( $html, $rating, $count ){
global $product;
$review_count = $product->get_review_count();
if ( $review_count && !is_product() ) {
$html = '<div class="rating-wrapper">' . $html . '(' . $review_count . ')' . '</div>';
} elseif ( $review_count < 1 && !is_product() ) {
$html = '';
}
return $html;
},10,3);`

It works fine on product category pages but is giving errors on the homepage. The homepage is static. I did try it on my live site but gave the same errors so I disabled the code.

Hi Jordan,

Technically this should be a question for WooCommerce support team as that's a WooCommerce filter.
Please refer this article for more details: https://generatepress.com/what-support-includes/

Maybe we can try wrapping the code in is_woocommerce() so it only runs on WooCommerce pages?

add_filter('woocommerce_product_get_rating_html', function( $html, $rating, $count ){
    if ( is_woocommerce() ) {
        global $woocommerce_loop;
        global $product;
        $loop_data = $woocommerce_loop['name'];
        $review_count = $product->get_review_count();
        if ( $review_count && $loop_data ) {
            $html = '<div class="rating-wrapper">' . $html . $review_count . '</div>';
        } elseif ( $review_count < 1 && !is_product() ) {
            $html = '';
        }
    }
        return $html;
},10,3);

You can adjust the conditional tag as needed: https://www.businessbloomer.com/woocommerce-conditional-logic-ultimate-php-guide/

This archived topic is closed to new replies.