Skip Navigation

[Resolved] Display woocommerce ratings conditionnally

This thread is resolved. Here is a description of the problem and solution.

Problem:
The user would like to display the WooCommerce rating only if they exist

Solution:
1. Activate "_wc_average_rating" hidden custom field in Toolset->Settings->Frontend Content, first section. Check this screenshot http://prntscr.com/t0pel9
2. Use the custom field on the condition:

[wpv-conditional if="( $(_wc_average_rating) gt '0' )"]
   When it is true
[/wpv-conditional]
 
[wpv-conditional if="( $(_wc_average_rating) gt '0' )"  evaluate="false"]
    When it is false
[/wpv-conditional]

Relevant Documentation:

This support ticket is created 4 years, 8 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
9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 - - 9:00 – 13:00
14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 - - 14:00 – 18:00

Supporter timezone: Africa/Casablanca (GMT+01:00)

This topic contains 8 replies, has 2 voices.

Last updated by ralphG 4 years, 7 months ago.

Assisted by: Jamal.

Author
Posts
#1660423
PrtScr capture_14.jpg

Tell us what you are trying to do?
I'm trying to display ratings conditionnally, if there are ratings, display them, if not show nothing.
[wpv-conditional if="( wpv_woo_products_rating_on_listing_func() ne '' )"]
[wpv-woo-products-rating-listing]
[/wpv-conditional]
But this is not working, I tried to compare to 'null' or '0' but without success.

Is there any documentation that you are following?
None. Only using the button on the default editor to create the conditionnal output.

Is there a similar example that we can see?

What is the link to your site?

#1661141

Hello and thank you for contacting the Toolset support.

I wonder if you have registered the function "wpv_woo_products_rating_on_listing_func" to be used inside conditional shortcodes. Check this article https://toolset.com/documentation/user-guides/views/conditional-html-output-in-views/using-custom-functions-in-conditions/

If this does not help, please allow me temporary access to check your view closely. Your next reply will be private to let you share credentials safely. ** Make a database backup before sharing credentials. **

#1663473
PrtScr capture_15.jpg

Hi,
thanks for the link, but the function was already registered (please see screenshot).

#1664859

Thank you for your feedback and access credentials. My apologies for the late reply, but I do not work on Sundays and Mondays.

I tried a different approach on the "Coups de coeur" view and it worked as expected. Instead of relying on the return value of the "wpv_woo_products_rating_on_listing_func" function, I activated a hidden custom field from WooCommerce and I used it on the condition:
1. Activate "_wc_average_rating" hidden custom field in Toolset->Settings->Frontend Content, first section. Check this screenshot hidden link
2. Use the custom field on the condition:

        [wpv-conditional if="( $(_wc_average_rating) gt '0' )"]
        	[wpv-woo-products-rating-listing]
        [/wpv-conditional]

Check this screenshot hidden link

I'll let you update the conditions on the other views. Let us know what you will get.

#1665069
PrtScr capture_16.jpg

Hi,
Thanks a lot! This works on each view/archive when it's needed.

I have 2 questions before closing this ticket:
- What the difference between _wcml_average_rating and _wc_average_rating ? since the 2 are available as hidden fields.
- Would it be better to use _wc_rating_count instead of _wc_average_rating ?

And lastly, I don't know if there is an 'else' statement so I added this to create a spacing, this way the title and other elements stay at the same place even if there are no ratings.

[wpv-conditional if="( $(_wc_average_rating) eq '0' )"]
        <div style="height:14.400px;"></div>
[/wpv-conditional]
#1665157

- What the difference between _wcml_average_rating and _wc_average_rating ? since the 2 are available as hidden fields.
_wc_average_rating is a custom field added by WooCommerce, _wcml_average_rating is added by WooCommerce Multilingual.
I am not 100% sure what each custom field does, I'll need to investigate it before being sure.

- Would it be better to use _wc_rating_count instead of _wc_average_rating ?
Sorry, but I am not sure of this too, I'll need to run some tests locally or access your database and run some additional tests to be sure. Right now, I can think of a possible difference, maybe _wc_rating_count holds all the reviews(all statuses) and _wc_average_rating holds an average of the "approved" reviews.

And lastly, I don't know if there is an 'else' statement so I added this to create a spacing, this way the title and other elements stay at the same place even if there are no ratings.
I think that your solution is good, it will keep the spacing as if the star rating is available but not visible.
Actually, there is no else keyword for Toolset conditions, but there is an option to force the condition to evaluate as false, check this screenshot hidden link
The option is then translated as a shortcode argument(evaluate="false"):

[wpv-conditional if="( $(_wc_average_rating) gt '0' )"]
   When it is true
[/wpv-conditional]

[wpv-conditional if="( $(_wc_average_rating) gt '0' )"  evaluate="false"]
    When it is false
[/wpv-conditional]

I hope this answers your questions, let me know if you have other questions.

#1665279

Ok, no problem.

Thanks, I updated my code with evaluate="false" instead.

For me the main issue is solved, I'm waiting your response for the 2 questions.

#1666927

It seems that WooCommerce stores an average rating on the _wc_average_rating custom field as an array, and all the rated reviews rating on the _wc_rating_count custom field as an array. Check this screenshot hidden link

For the _wcml_average_rating average rating, I don't have a ready WCML installation, but I think that it will calculate the average rating for a product across all languages. It will query all reviews in all languages and calculates the average rating of the product. Reviews are not translated with WPML, products can be translated, which will create a product for each language, but they still need to be managed as one single product, WCML will calculate the average of reviews in all languages, and will sync it to the translations.

#1667207

I think you wanted to say : _wc_average_rating as a number.

And the 2nd field _wcml_average_rating is not necessary if reviews are not translated as you said.

My issue is resolved now. Thank you!