Skip Navigation

[Resolved] Add number of ratings in product listing page

This support ticket is created 5 years, 10 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

Tagged: 

This topic contains 12 replies, has 4 voices.

Last updated by Pat 5 years, 10 months ago.

Assisted by: Beda.

Author
Posts
#1215311

Pat

Hello,

I'm working on a site with Toolset and WC and wants to display ratings on the product listing page.
For that, I'm using the [wpv-woo-products-rating-listing] shortcode.
Unfortunately, this shortcode outputs only the stars display and no the number of reviews (as it is done in the product page).
Is there a way to display this info in the the product listing page (but without the link to the reviews itself, only the number of ratings)

Regards
Pat

#1216988

Hi, there's no shortcode built-in to Toolset that will display the number of ratings alone. In the listing archive, you would have to use PHP to get that information from the global WooCommerce Product object. There are WooCommerce APIs available for this. You can get either the total rating count or the number of ratings per star:

global $product; 
$rating_count = $product->get_rating_count();
$rating_1_stars = $product->get_rating_count(1);
$rating_2_stars = $product->get_rating_count(2);
...etc...

http://woocommerce.wp-a2z.org/oik_api/wc_productget_rating_count/
https://wordpress.stackexchange.com/questions/286462/how-to-get-total-count-for-each-star-rating

#1217392

Pat

Hi Christian,

Thanks for helping. I have created a shortcode :

function number_of_ratings( ) {
  global $product; 
  $rating_count = $product->get_rating_count();
  return $rating_count;
  }
  add_shortcode( 'nb_ratings', 'number_of_ratings' );

and have recorded the shortcode [nb_ratings] inside the Toolset paramers.
The display is fine and I'm getting the right number of ratings.

Now, I want to use this shortcode in a condition output and then, this is not the same story !
If I use it inside a conditional output, then, the Views is no more displayed (not only the part related to the condition but all the Views results !).
If I try the same conditional output with another field, then everything is fine.

Any idea what could be wrong in what I'm doing here?

Regards
Pat

#1218392

Did you register your shortcode in Toolset > Settings > Front-end Content > Third-party shortcode arguments? Or somewhere else?

Add debug="true" to your conditional and tell me the results on-screen. If there are no debug results, please copy + paste the entire conditional here for me to review.

#1218531

Pat

Hi Christian,
Yes the shortcode is well registered and appear in the conditional box where all shortcodes and functions are listed. In addition, I can see the right result that is displayed when I'm using the Views without the conditional output !

I have tried to add the debug="true" but as nothing is displayed (only the page title ans nothing from the Views), this is not really helping.

Here is the total Views code :

<div class="blocproduit">
<a href="[wpv-post-url]" class="imgzoom">
  <div style="background-image:url('[wpv-woo-product-image size="medium" output="raw"]');background-size:cover;background-repeat:no-repeat;background-position:50% 50%;"><img src="<em><u>hidden link</u></em>" width="600" height="600" class="size-full" />
  </div>
</a>
  
<div class="titreparfum">[wpv-post-link]</div>
  <div class="etoiles">[wpv-woo-products-rating-listing]
    [wpv-conditional if="( '[nb_ratings]' ne '0' )" debug="true"]<div style="position: absolute;right: 2%;margin-top: -1.1em;">[nb_ratings] avis</div>[/wpv-conditional]
  </div>
  [wpv-conditional if="( $(wpcf-stock-insuffisant) ne '1' )"]<div class="stock">En stock</div>[/wpv-conditional]
  [wpv-conditional if="( $(wpcf-stock-insuffisant) eq '1' )"]<div class="nostock">Rupture de stock</div>[/wpv-conditional]
  <div class="affichprix">[wpv-woo-buy-or-select add_to_cart_text="PANIER" link_to_product_text="PANIER" show_variation_options="yes" link_to_product_text="Sélectionnez le contenant"]</div>
</div>

The 2 conditions linked to stock are working fine.
If I use another Views shortcode in the place of [nb_ratings], then it's working fine.
If I deactivate the function number_of_ratings (see in my previews post), then it's working (the Views is displayed but not the number of ratings of course !).

Hope that helps.
Regards
Pat

#1219209

Okay can you try one more test please? Save the code you have now as a backup and replace it with this simplified code:

<div class="blocproduit">
  Ratings: [nb_ratings]<br />
  [wpv-conditional if="( '[nb_ratings]' ne '0' )" debug="true"]]
    NE 0<br />
  [/wpv-conditional]
</div>

Please let me know what you see on the front-end.

#1219407

Pat

Hi Christian,

Doing this leads to the same issue than before : nothing from the Views is displayed (only the page post title)!
Even the admin bar is no more displayed.
Regards
Pat

#1220048

Okay please add this to your wp-config.php file just before where it says 'stop editing here':

ini_set('pcre.jit', false);

If this doesn't work, I'll need to reproduce the problem locally. Please provide login credentials in the private reply fields here.

#1220506

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Pat,

Christian is out today but should return tomorrow to continue assisting.

Thanks,
Shane

#1221520

Most likely your ShortCode outputs more than just the number.

This is visible when you insert the (custom) shortcode [nb_ratings] in the View - which will display the number as you say.
Now if you look at the browser console you will see what it outputs really - and likely there will be some HTML or anything else that is not a pure number.

Another issue I see is invalid Syntax in your View.

  <div style="background-image:url('[wpv-woo-product-image size="medium" output="raw"]');background-size:cover;background-repeat:no-repeat;background-position:50% 50%;"><img src="<em><u>hidden link</u></em>" width="600" height="600" class="size-full" />

This is broken due to overusage of quotes.

The only page with the View I found is "catalogue".
hidden link

There is no page "PARFUMS POUR BOUGIES" on the site. Can you show me where the View is used?

Additionally the [nb-avis] shortcode is not existing on the site, because if you output it, you simply see it printed, not executed.
Neither is [nb_ratings].

Could you help me to help you and re-iterate the ShortCode and as well above suggestions?

Thanks!

#1221614

Pat

Hi Beda,

Here is the changes I have made (I have duplicated some parts in order for you to have full access and be able to modify them) :

- The Views is now : Views test nb rating. I have simplified the image display (no more background, only the image)
- The page where you can have this Views is AAA
- I have changed the name of the shortcode to the initial one [nb-of-rating]
- I have also modified the nb-of-rating function :

function number_of_ratings( ) {
  global $product; 
  $review_count = $product->get_review_count();
  return $review_count;
  }
  add_shortcode( 'nb-of-rating', 'number_of_ratings' );

After this, if I place the shortcode inside the loop, then I'm still getting the display issue. If I place the shortcode out of the loop, then, the value is correctly displayed (for example, I have 2 reviews on the "Agrume" product that are correctly displayed in that case.

Any idea?
Regards
Pat

#1222224

The display of [nb-of-rating] is 2 for the first, 0 for the other products.
As soon [wpv-conditional if="( '[nb-of-rating]' eq '2' )"]is 2[/wpv-conditional] is inserted the page breaks (it seems there is a PHP error, however I cannot see it, maybe you need to enable WP Debug?)

I am pretty sure the code within your ShortCode is faulty.
Please try to remove it and just return 1, then check again.
The Condition now should work, proving that the Custom code within it is broken.

We can assist custom code only limitedly, you would have to clarify with WooCommerce how to get and return the expected value properly, but lets first see what WP Debug tells us and what if you just return a string or number in the custom shortcode.

#1222391

Pat

Hi Beda,

You're right. For any reason, the result of my function should be numerical and I added this line :
$result = intval ($review_count);

Now everything is working fine.
Thanks for your patience !
Regards
Pat