Site logo

[Support request] PHP code for review count on product archive pages

Home Forums Support [Support request] PHP code for review count on product archive pages

Home Forums Support PHP code for review count on product archive pages

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #2569112
    Jordan

    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.

    #2569136
    Leo
    Staff
    Customer Support

    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/

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.