Home › Forums › Basel support forum › Product reviews for category pages
Product reviews for category pages
- This topic has 11 replies, 2 voices, and was last updated 1 year ago by Elise Noromit.
-
AuthorPosts
-
October 26, 2023 at 10:04 pm #507660
TuomoParticipantHi there,
Will your theme make a shortcode system for transfering reviews to category pages?
It would be nice if the shortcode would collect and show bulk review rating for categories.
For example I have 2 products in one category. The first product X has 10 reviews and the second P product has 20 reviews. The shortcode would collect the X and P products total rating for the category.This plugin would do the job, but it is not working properly and it is out of date:
https://wordpress.org/plugins/simple-woo-reviews-lite/Or do you know any other plugins which could do the same thing in a simple way?
Best Regards,
Tuomo NurkkalaOctober 27, 2023 at 2:23 pm #507886
Elise NoromitMemberHello,
You can create HTML and insert any content into this block: https://xtemos.com/docs-topic/html-blocks/
Then you can insert the shortcode of this block into the product category description.
If you have any questions please feel free to contact us.
Best Regards
November 4, 2023 at 3:15 pm #509895
TuomoParticipantI tried to add this code to functions.php, but with no luck and I added this shortcode to category page
[category_reviews]
. Do you know what type of code should I add to get the category reviews in a shortcode?function display_category_reviews() { // Get the current category ID $category = get_queried_object(); $category_id = $category->term_id; // Query WooCommerce products in the current category $args = array( 'post_type' => 'product', 'posts_per_page' => -1, 'product_cat' => $category_id, ); $products = new WP_Query($args); // Initialize variables for total reviews and total rating $total_reviews = 0; $total_rating = 0; // Loop through products to calculate total reviews and ratings while ($products->have_posts()) { $products->the_post(); $product = wc_get_product(get_the_ID()); // Get the product's average rating $average_rating = $product->get_average_rating(); // Get the number of reviews for the product $review_count = $product->get_review_count(); // Update total reviews and ratings $total_reviews += $review_count; $total_rating += $average_rating * $review_count; } // Calculate the overall average rating if ($total_reviews > 0) { $overall_average_rating = round($total_rating / $total_reviews, 2); } else { $overall_average_rating = 0; } // Output the result ob_start(); ?> <div class="category-reviews"> <p class="rating">★★★★★</p> <p class="average-rating"><?php echo esc_html($overall_average_rating); ?>/5 from <?php echo esc_html($total_reviews); ?> reviews</p> </div> <?php return ob_get_clean(); } add_shortcode('category_reviews', 'display_category_reviews');
November 4, 2023 at 11:02 pm #509938
TuomoParticipantI found the star rating for categories from here and it works only with functions.php: https://stackoverflow.com/questions/70977508/woocommerce-category-rating-based-on-reviews .
Do you know how I could get the star rating on the bottom of the category page, because now it is on the top part?
I was wondering as well that do you have similar type of code for HTML blocks so the texts could be translated with WPML?Here is the modified functions.php code from that page:
defined('ABSPATH') or die("Te pup!"); function super_plugin_install() { // Do some installation work } register_activation_hook(__FILE__, 'super_plugin_install'); // HOOKS add_action('init', 'super_plugin_init'); /********************************************************/ /* FUNCTIONS /********************************************************/ function super_plugin_init() { add_action('woocommerce_after_shop_loop', 'show_rating_stars'); function show_rating_stars() { if (is_paged()) return; global $post, $wpdb, $wp_query; if (comments_open()) : // Get the query object $cat_obj = $wp_query->get_queried_object(); if ($cat_obj) { $category_name = $cat_obj->name; $category_desc = $cat_obj->description; $category_ID = $cat_obj->term_id; } $post_ids = get_posts(array( 'numberposts' => -1, // Get all posts. 'post_type' => 'product', 'tax_query' => array( array( 'taxonomy' => 'product_cat', 'field' => 'id', 'terms' => $category_ID, ), ), 'fields' => 'ids', // Only get post IDs )); $sumrating = 0; $sumcount = 0; foreach ($post_ids as $pId) { $count = $wpdb->get_var(" SELECT COUNT(meta_value) FROM $wpdb->commentmeta LEFT JOIN $wpdb->comments ON $wpdb->commentmeta.comment_id = $wpdb->comments.comment_ID WHERE meta_key = 'rating' AND comment_post_ID = $pId AND comment_approved = '1' AND meta_value > 0 "); $sumcount = $sumcount + $count; $rating = $wpdb->get_var(" SELECT SUM(meta_value) FROM $wpdb->commentmeta LEFT JOIN $wpdb->comments ON $wpdb->commentmeta.comment_id = $wpdb->comments.comment_ID WHERE meta_key = 'rating' AND comment_post_ID = $pId AND comment_approved = '1' "); $sumrating = $sumrating + $rating; } if ($sumcount > 0) : $average = number_format($sumrating / $sumcount, 2); $adminratingtext = esc_attr(get_option('wcr_ratings_text')); if ($adminratingtext == '') { $ratingstext = ''; } else { $ratingstext = '<span class="ratings-text">' . $adminratingtext . ' ' . $category_name . ':</span> '; } echo '<br /><br />'; echo '<div itemprop="aggregateRating" itemscope="" itemtype="http://schema.org/AggregateRating">' . $ratingstext . wc_get_rating_html($average, $sumcount) . ' ' . $average . '/5</div>'; echo 'Perustuu ' . $sumcount . ' tuotearvioon'; endif; endif; } } function wcr_init() { register_setting('wcr_options', 'wcr_ratings_text'); } add_action('admin_init', 'wcr_init'); function wcr_options_page() { ?> <div class "wrap"> <?php screen_icon(); ?> <h2>Woocommerce Category Rating Options</h2> <form action="options.php" method="post" id="wcr_options_form"> <?php settings_fields('wcr_options'); ?> <h3><label for="rating-text">Custom rating text:</label> <input type="text" id="wcr_ratings_text" name="wcr_ratings_text" placeholder="Rating for: %category%" value="<?php echo esc_attr(get_option('wcr_ratings_text')); ?>" /></h3> <p><input class="button-primary" type="submit" name="submit" value="Save options" /></p> </form> </div> <?php } function wcr_plugin_menu() { add_options_page('Woocommerce Category Rating Settings', 'Category Ratings', 'manage_options', 'woocommerce-category-rating-plugin', 'wcr_options_page'); } add_action('admin_menu', 'wcr_plugin_menu'); function my_plugin_action_links($links) { $links[] = '<a href="' . get_admin_url(null, 'options-general.php?page=woocommerce-category-rating-plugin') . '">Settings</a>'; return $links; } add_filter('plugin_action_links_' . plugin_basename(__FILE__), 'my_plugin_action_links');
Attachments:
You must be logged in to view attached files.November 4, 2023 at 11:52 pm #509942
TuomoParticipantHow can I add to the functions.php the parent category name?
This code works:echo '<br /><br />'; echo 'Tuotearviot: ' . $category_name . '<br>'; echo '<div itemprop="aggregateRating" itemscope="" itemtype="http://schema.org/AggregateRating">' . $ratingstext . wc_get_rating_html($average, $sumcount) . ' ' . $average . '/5</div>'; echo 'Perustuu ' . $sumcount . ' tuotearvioon';
But this wont give me the parent category name:
echo '<br /><br />'; echo 'Tuotearviot: ' . $parent_category_name . ' - ' . $category_name . '<br>'; echo '<div itemprop="aggregateRating" itemscope="" itemtype="http://schema.org/AggregateRating">' . $ratingstext . wc_get_rating_html($average, $sumcount) . ' ' . $average . '/5</div>'; echo 'Perustuu ' . $sumcount . ' tuotearvioon';
November 5, 2023 at 12:51 am #509946
Elise NoromitMemberHello,
You can insert the shortcode into the Theme Settings, widgets, or builder element.
Please remove this shortcode from functions.php, it is not correct it may break your site.
If you have any questions please feel free to contact us.
Best Regards
November 5, 2023 at 1:11 am #509955
TuomoParticipantHi Elise,
Can you show some example where I should add that code and would that code work the same way as it does from funstions.php file?
Where in Theme Settings should I add that code to?Best Regards,
Tuomo NurkkalaNovember 6, 2023 at 12:42 am #510034
Elise NoromitMemberHello,
You can create HTML and insert its shortcode ID into the product category description field, which you see if you edit the product category. Please check this manual: https://xtemos.com/docs-topic/html-blocks/
If you have any questions please feel free to contact us.
Best Regards
November 6, 2023 at 10:36 am #510105
TuomoParticipantHi Elise,
I am not able to make that code work in the HTML blocks.
The code I provided is primarily written in PHP, with some HTML embedded within it. In this code, PHP is used to fetch and manipulate data, while HTML is used to structure and display the content on the web page.
I tried to convert the whole code to HTML, but this didn’t help. Can you help to make the code work in HTML blocks?<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Your HTML Page</title> </head> <body> <h1>Your HTML Page</h1> <?php defined('ABSPATH') or die("Te pup!"); function super_plugin_install() { // Do some installation work } register_activation_hook(__FILE__, 'super_plugin_install'); // HOOKS add_action('init', 'super_plugin_init'); /********************************************************/ /* FUNCTIONS /********************************************************/ function super_plugin_init() { add_action('woocommerce_after_shop_loop', 'show_rating_stars'); function show_rating_stars() { if (is paged()) return; global $post, $wpdb, $wp_query; if (comments_open()) : // Get the query object $cat_obj = $wp_query->get_queried_object(); if ($cat_obj) { $category_name = $cat_obj->name; $category_desc = $cat_obj->description; $category_ID = $cat_obj->term_id; } $post_ids = get_posts(array( 'numberposts' => -1, // Get all posts. 'post_type' => 'product', 'tax_query' => array( array( 'taxonomy' => 'product_cat', 'field' => 'id', 'terms' => $category_ID, ), ), 'fields' => 'ids', // Only get post IDs )); $sumrating = 0; $sumcount = 0; foreach ($post_ids as $pId) { $count = $wpdb->get_var(" SELECT COUNT(meta_value) FROM $wpdb->commentmeta LEFT JOIN $wpdb->comments ON $wpdb->commentmeta.comment_id = $wpdb->comments.comment_ID WHERE meta_key = 'rating' AND comment_post_ID = $pId AND comment_approved = '1' AND meta_value > 0 "); $sumcount = $sumcount + $count; $rating = $wpdb->get_var(" SELECT SUM(meta_value) FROM $wpdb->commentmeta LEFT JOIN $wpdb->comments ON $wpdb->commentmeta.comment_id = $wpdb->comments.comment_ID WHERE meta_key = 'rating' AND comment_post_ID = $pId AND comment_approved = '1' "); $sumrating = $sumrating + $rating; } if ($sumcount > 0) : $average = number_format($sumrating / $sumcount, 2); $adminratingtext = esc_attr(get_option('wcr_ratings_text')); if ($adminratingtext == '') { $ratingstext = ''; } else { $ratingstext = '<span class="ratings-text">' . $adminratingtext . ' ' . $category_name . ':</span> '; } echo '<br /><br />'; echo 'Tuotearviot: ' . $category_name . '<br>'; echo '<div itemprop="aggregateRating" itemscope="" itemtype="http://schema.org/AggregateRating">' . $ratingstext . wc_get_rating_html($average, $sumcount) . ' ' . $average . '/5</div>'; echo 'Perustuu ' . $sumcount . ' tuotearvioon'; endif; endif; } } function wcr_init() { register_setting('wcr_options', 'wcr_ratings_text'); } add_action('admin_init', 'wcr_init'); function wcr_options_page() { ?> <div class="wrap"> <?php screen_icon(); ?> <h2>Woocommerce Category Rating Options</h2> <form action="options.php" method="post" id="wcr_options_form"> <?php settings_fields('wcr_options'); ?> <h3><label for="rating-text">Custom rating text:</label> <input type="text" id="wcr_ratings_text" name="wcr_ratings_text" placeholder="Rating for: %category%" value="<?php echo esc_attr(get_option('wcr_ratings_text')); ?>" /></h3> <p><input class="button-primary" type="submit" name="submit" value="Save options" /></p> </form> </div> <?php } function wcr_plugin_menu() { add_options_page('Woocommerce Category Rating Settings', 'Category Ratings', 'manage_options', 'woocommerce-category-rating-plugin', 'wcr_options_page'); } add_action('admin_menu', 'wcr_plugin_menu'); function my_plugin_action_links($links) { $links[] = '<a href="' . get_admin_url(null, '
November 6, 2023 at 4:31 pm #510242
Elise NoromitMemberHello,
Will your theme make a shortcode system for transfering reviews to category pages?
It would be nice if the shortcode would collect and show bulk review rating for categories.
For example I have 2 products in one category. The first product X has 10 reviews and the second P product has 20 reviews. The shortcode would collect the X and P products total rating for the category.By default, Woocommerce does not provide the option to show reviews on the category page, at the same time you can show your custom images, content on the product category.
You need to create HTML block, then insert its shortcode ID in the place you want to show the content.
Our support does not cover /include customization of Wooommerce functionality, we suggest to search for the plugin to meet your requirements.
We have not met or tested any plugins for the purpose which is why we can hardly recommend any.
If you have any questions please feel free to contact us.
Best Regards
November 6, 2023 at 5:14 pm #510270
TuomoParticipantHi Elise,
Okay, thanks for the help though! 🙂
Best Regards,
Tuomo NurkkalaNovember 7, 2023 at 2:38 am #510380
Elise NoromitMemberYou are welcome!
Wish you a wonderful day!
-
AuthorPosts
The topic ‘Product reviews for category pages’ is closed to new replies.
- You must be logged in to create new topics. Login / Register