1

I want to show the counting number of reviews on each woocommerce products so I have followed this link and its working for all the products as well.

but I have also added recent reviews section with the help of another plugin. where the counting number not showing.

The reason is that the plugin not found the ratting.php code so i want to know how i can use the rating.php code into theme functions.php file. In other words, words, How i can convert this code like functions so i can use this into functions.php file

here is the code of rating.php

<?php
/**
 * Loop Rating
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/loop/rating.php.
 *
 * HOWEVER, on occasion WooCommerce will need to update template files and you
 * (the theme developer) will need to copy the new files to your theme to
 * maintain compatibility. We try to do this as little as possible, but it does
 * happen. When this occurs the version of the template file will be bumped and
 * the readme will list any important changes.
 *
 * @see         https://docs.woocommerce.com/document/template-structure/
 * @author      WooThemes
 * @package     WooCommerce/Templates
 * @version     3.0.0
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

global $product;

if ( get_option( 'woocommerce_enable_review_rating' ) === 'no' ) {
    return;
}

$rating_count = $product->get_rating_count();
$review_count = $product->get_review_count();
$average      = $product->get_average_rating();

if ( $rating_count >= 0 ) : ?>

            <?php echo wc_get_rating_html($average, $rating_count); ?>
        <?php if ( comments_open() ): ?><a href="<?php echo get_permalink() ?>#reviews" class="woocommerce-review-link" rel="nofollow">(<?php printf( _n( '%s',$review_count,'woocommerce' ), '<span class="count">' . esc_html( $review_count ) . '</span>' ); ?>)</a><?php endif ?>


<?php endif; ?>

Thanks

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
R.K.Bhardwaj
  • 2,168
  • 1
  • 14
  • 24

1 Answers1

0

You can embed the code in a function as a shortcode with an id argument (the product id):

add_shortcode( 'product_rating', 'display_product_rating' );
function display_product_rating( $atts ){
    // Shortcode attributes (default)
    $atts = shortcode_atts( array(
        'id' => '0',
    ), $atts, 'product_rating' );

    if ( get_option( 'woocommerce_enable_review_rating' ) === 'no' )
        return; // Exit

    global $product, $post;

    if( ! is_a( $product, 'WC_product') ){
        $product_id   = $atts['id'] > 0 ? $atts['id'] : get_the_id();
        $product      = wc_get_product( $product_id );
        if( ! is_a( $product, 'WC_product') ) 
            return; // Exit
    }

    $rating_count = $product->get_rating_count();
    $review_count = $product->get_review_count();
    $average      = $product->get_average_rating();
    $output       = '';

    if ( $rating_count >= 0 ) {
        $output .= wc_get_rating_html($average, $rating_count);
        if ( comments_open() ) {
            $fcount = $printf( _n( '%s',$review_count,'woocommerce' ), '<span class="count">' . esc_html( $review_count ) . '</span>' );
            $output .= '<a href="'.get_permalink().'#reviews" class="woocommerce-review-link" rel="nofollow">('.$fcount.')</a>';
        }
    }
    return $output;
}

Code goes in function.php file of the active child theme (or active theme). Tested and works.


USAGE EXAMPLES:

1) In a WordPress text editor of a post or a page, defining the id argument (the product Id):

[product_rating id='37']

2) In a some php code with a dynamic $product_id variable (the product ID):

echo do_shortcode( "[product_rating id='$product_id']" );

3) Inside the product loop of a WP_Query or a WC_Product_Query:

echo do_shortcode( "[product_rating]" );
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399