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)
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:
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?
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.
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.
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 !).
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
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.
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?
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.
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.