• Resolved tuomonurkkala

    (@tuomonurkkala)


    Hi there,

    Are you able to have ratings for product categories at some point like for all of the products?

    I found a way to add pretty good review system to functions.php, but I would like to have it under the term-description. This code helps it to be just under the products (woocommerce_after_shop_loop) and above term-description. If I try to add the code to (woocommerce_after_main_content) it will be on the wrong spot in the page. :/

    https://ufile.io/czosperh

    I found the the review system from here: https://stackoverflow.com/questions/70977508/woocommerce-category-rating-based-on-reviews

    Here is the tweaked code that works for my sites, but I am strill trying to figure out a better spot for it:

    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="https://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');

    Best Regards,

    Tuomo Nurkkala

    The page I need help with: [log in to see the link]

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter tuomonurkkala

    (@tuomonurkkala)

    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.

    Is there a possibility to have all of the above code written in HTML so I could make HTML blocks of them in woocommerce not for functions.php file and transferred them as in shortcode to category description?

    Plugin Support Shameem R. a11n

    (@shameemreza)

    Hi @tuomonurkkala

    While I understand that you want to convert your PHP code into HTML to create blocks in WooCommerce, unfortunately, this is not feasible. The PHP code you’ve shared is responsible for fetching and processing data from your database, which HTML alone cannot do as it is a markup language primarily used for structuring and displaying static content on web pages.

    However, you can create a shortcode to use this PHP functionality within your category descriptions or any other areas where shortcodes are processed.

    Here are some online resources I discovered that could assist you in understanding the process of creating shortcode:

    I hope this clarifies your concern! If you have any other questions or need further clarification, feel free to ask.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Reviews for category pages’ is closed to new replies.