If WooCommerce product reviews are not showing on your website, there could be a few reasons for this. One common issue is that the reviews are disabled for the product or the theme you are using does not display them properly. To troubleshoot this, you can follow these steps:
- Check if reviews are enabled for the product:
$product = wc_get_product( $product_id );
if ( $product->get_reviews_allowed() ) {
// Reviews are enabled for this product
} else {
// Reviews are disabled for this product
}
This code snippet checks if reviews are enabled for a specific product. You need to replace $product_id
with the ID of the product you want to check.
- Verify if the theme supports displaying reviews:
if ( comments_open() && wc_review_ratings_enabled() ) {
// The theme supports displaying reviews
} else {
// The theme does not support displaying reviews
}
This code snippet checks if the current theme supports displaying reviews. It ensures that comments are open and the WooCommerce review ratings are enabled.
- Check if the reviews template is overridden in the theme:
if ( ! comments_open() && get_comments_number() && post_type_supports( get_post_type(), 'comments' ) ) {
// The reviews template is overridden in the theme
} else {
// The reviews template is not overridden in the theme
}
This code snippet checks if the reviews template is overridden in the theme. It verifies if comments are closed, there are comments for the post, and the post type supports comments.
By using these code snippets, you can identify the possible reasons why WooCommerce product reviews are not showing on your website.
Examples
Example 1: Check if WooCommerce product reviews are enabled
This example demonstrates how to check if WooCommerce product reviews are enabled on a specific product.
if ( wpsnippets_is_woocommerce_reviews_enabled( $product_id ) ) {
// Product reviews are enabled
} else {
// Product reviews are disabled
}
The code uses the wpsnippets_is_woocommerce_reviews_enabled()
function to check if product reviews are enabled for a given product ID. It returns true
if reviews are enabled and false
if they are disabled.
Example 2: Display a message when WooCommerce product reviews are disabled
This example shows how to display a custom message when product reviews are disabled for a specific product.
if ( ! wpsnippets_is_woocommerce_reviews_enabled( $product_id ) ) {
echo 'Sorry, reviews are currently disabled for this product.';
}
The code checks if product reviews are disabled using the wpsnippets_is_woocommerce_reviews_enabled()
function. If reviews are disabled, it displays a custom message using echo
.
Example 3: Modify WooCommerce product reviews query
This example demonstrates how to modify the query used to retrieve WooCommerce product reviews.
function wpsnippets_modify_product_reviews_query( $query ) {
if ( $query->is_main_query() && is_product() ) {
$query->set( 'posts_per_page', 5 ); // Show only 5 reviews per page
}
}
add_action( 'pre_get_posts', 'wpsnippets_modify_product_reviews_query' );
The code defines a custom function wpsnippets_modify_product_reviews_query()
that modifies the query used to retrieve product reviews. In this example, it sets the posts_per_page
parameter to limit the number of reviews displayed to 5 per page. The function is hooked into the pre_get_posts
action to apply the modification.