Home Forums Basel support forum Product reviews for category pages

Product reviews for category pages

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #507660

    Tuomo
    Participant

    Hi 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 Nurkkala

    #507886

    Hello,

    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

    #509895

    Tuomo
    Participant

    I 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');
    #509938

    Tuomo
    Participant

    I 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.
    #509942

    Tuomo
    Participant

    How 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';
    #509946

    Hello,

    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

    #509955

    Tuomo
    Participant

    Hi 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 Nurkkala

    #510034

    Hello,

    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

    #510105

    Tuomo
    Participant

    Hi 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, '
    #510242

    Hello,

    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

    #510270

    Tuomo
    Participant

    Hi Elise,

    Okay, thanks for the help though! 🙂

    Best Regards,
    Tuomo Nurkkala

    #510380

    You are welcome!

    Wish you a wonderful day!

Viewing 12 posts - 1 through 12 (of 12 total)

The topic ‘Product reviews for category pages’ is closed to new replies.