0

I want to get the amount of reviews of a certain woocommerce product. I have this code but it gives me the total amount of reviews (so only the amount of reviews of that specific product).

function get_total_reviews_count(){
    return get_comments(array(
        'post_ID' =>'12345',
    'status'   => 'approve',
        'post_status' => 'publish',
        'count' => true
    ));
}
add_shortcode('reviews_count', 'get_total_reviews_count');

Hope you guys can help me adjust the code so that I will only see the amount of reviews of product 12345.

1

WooCommerce provides a function for this. You should not need to query the comments and count them:

add_shortcode(
    'reviews_count',
    function() {
        // Make sure we're on a Product.
        if ( function_exists( 'is_product' ) && is_product() ) {
            // Get a WC_Product object for the current product.
            $product = wc_get_product( get_queried_object_id() );
            // Return the review count.
            return $product->get_review_count();
        }
    }
);

If you want to display the reviews count for a specific product you can use:

add_shortcode(
    'reviews_count',
    function() {
        if ( function_exists( 'wc_get_product' ) ) {
            // Get a WC_Product object for the product.
            $product = wc_get_product( 12345 );
            // Return the review count.
            return $product->get_review_count();
        }
    }
);

If you want to be able to pass the product ID to the shortcode (like this: [reviews_count id="12345"]), you can use:

add_shortcode(
    'reviews_count',
    function( $atts ) {
        // Make sure an ID was passed,
        if ( ! empty( $atts['id'] && function_exists( 'wc_get_product' ) ) {
            // Get a WC_Product object for the product.
            $product = wc_get_product( (int) $atts['id'] );
            // Return the review count.
            return $product->get_review_count();
        }
    }
);
3
  • Hi Jacob, thanks for your answer, I want to place this counter on a regular page. So I need to tell somewhere that this is about product 12345. Where should I do this or will the code be different then? Thanks! Right now I use the following adjusted code and this seems to work (but maybe it can be better coded?) function get_total_reviews_count(){ return get_comments(array( 'post_id' => '25374', 'status' => 'approve', 'post_status' => 'publish', )); } add_shortcode('reviews_count', 'get_total_reviews_count');
    – Bas
    Sep 3 '20 at 12:53
  • I updated my answer with two options. Don't use get_comments(). Sep 3 '20 at 13:05
  • Hi Jacob, thank you so much for your help. The third and last code sounds like a perfect solution, this way I can use 1 code for multiple products. The only problem is that when I place the code in my functions.php my site goes down. Can you have a second look at the code because when we'll be able to get this working that would be terrific. Thanks!
    – Bas
    Sep 8 '20 at 7:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.