Skip Navigation

[Resolved] Function to return count of specific selected checkbox field

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

Problem: Function to return count of specific selected checkbox field

Solution: You will require to write a custom shortcode to count the checkboxes field option count per post.

You can find the proposed solution in this case with the following reply: https://toolset.com/forums/topic/function-to-return-count-of-specific-selected-checkbox-field/#post-1784111

Relevant Documentation: => https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/

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.

This topic contains 8 replies, has 2 voices.

Last updated by ericS-17 1 month, 1 week ago.

Assigned support staff: Minesh.

Author
Posts
#1776107

Tell us what you are trying to do?
I've created a reviews database using Toolset (see it here hidden link). Users submit a review about a particular smart trainer, and as part of that review they can select from a checkboxes field called "Qualities" which has values like "Easy setup","Well-built","Quiet", etc. When checked, these are stored as numbers in the database. Unchecked stores no values.

Now here's the challenge: each of the smart trainers has a detailed homepage (example hidden link) where I'm displaying the "qualities" selected for each review. But I want to show a SUMMARY of the qualities people selected in reviews, so you get a sort of "quality profile" for the trainer. It would show something like this:

Easy Setup: 30%
Accurate Power: 24%
Realistic: 65%
Well-built: 70%
Helpful support: 52%

So if 30% of the people who reviewed a particular trainer checked the "Easy setup" box, it would show 30%.

Is there any documentation that you are following?
I haven't found any good documentation for this.

Is there a similar example that we can see?

What is the link to your site?
hidden link

#1777151

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

There is no such feature available to calculate the how many people/user selected the specific checkbox option from your custom checkboxes field.

It will require custom shortcode to do such calculation and to write such custom code is beyond the scope of our support policy.

However, we do offer doc that should help you to learn how you can build your custom shortcode and adjust the code as per your requirement.
=> https://toolset.com/documentation/adding-custom-code/how-to-create-a-custom-shortcode/

If you still do not know how to do this, you can always contact our certified partners:
=> https://toolset.com/contractors/

if you know how to build shortcode and know the formula about how to calculate the percentage, I am happy to guide you.

#1777983

Hi Minesh,

Thanks for the reply. I've implemented a few custom shortcodes in my solution this far, including one based on this post (https://toolset.com/forums/topic/setting-up-a-ratings-average-for-reviews-cpt/) to show the average rating for an item, pulled from multiple reviews.

I could certainly pseudocode out a formula for computing the percentage. My problem is the PHP syntax, which I'm not very good with. I can read it and understand it, but writing it from scratch is another issue, especially when I don't really know how the data is structured.

In terms of pseudocode, here's how I need it to work:

function getQualityScore(qualityID, totalReviews,productID) {
// qualityID is the ID of the checkbox I'm computing a score for. This will vary from 1-8, I believe, since I have 8 checkboxes.
// totalReviews is the total number of reviews that have been submitted for this particular product. I have an existing shortcode for this.
// productID is unique ID of product being reviewed

totalChecked=total number of productID reviews where reviewer selected this particular qualityID;

// so if 100 reviews have been submitted for this product, and 20 of those reviews had this one particular qualityID checked, then we would return .2 (that is, 20%) for this particular quality.

return totalChecked/totalReviews;

}

Can you help me put together that shortcode?

#1778307

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I will require admin access details to review your current setup and on the trainer profile page, for example:
=> hidden link
- With above trainer profile page at what section exactly you would like to display the quality percentage?

Can you please setup one trainer profile with example data and tell me what will be the quality percentage for every quality options so when I try to build the solution I will be able to match the test profile you set for me.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#1779707

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

When I try to access the login page - I got the message on page "Jetpack has locked your site's login page." and cant able to login to admin.

Can you please deactivate such security plugin for now and send me access details again.

I have set the next reply to private which means only you and I have access to it.

#1780971

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Again even when I try to login with the "my-account" link you shared. When I try to login and submit the login form still I can see the same message "Jetpack has locked your site's login page." and I cant able to login.

Can you pleases remove security plugin for now and send me access details so that I can login.

I have set the next reply to private which means only you and I have access to it.

#1784111

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hi Eric,

Can you please check now: hidden link

I've added the "Qualities" section on your page just below the "OVERALL RATING" ratings section.

I've created the following shortcode and added it to "Custom Code" section offered by Toolset:
=> hidden link

add_shortcode('calculate_quality_rating', 'func_calculate_quality_rating');
function func_calculate_quality_rating($atts) {
    global $post;
   
  // checkboxes field name
   $types_field='qualities';
  
   // getting the checkboxes option names and set default option count to 0
   $fields = get_option( 'wpcf-fields');
    $quality_options = array();
    if(isset($fields[$types_field]['data']['options'])){
      	foreach ($fields[$types_field]['data']['options'] as $k=> $v){
          $quality_options[$v['set_value']]['title'] = $v['title'];
          $quality_options[$v['set_value']]['count'] = 0;
        }
       
    }
  
  // fetching related reviews
  $child_posts = toolset_get_related_posts($post->ID,'smart-trainer-review','parent',999,0,array(),'post_id','child');
  
  // count total reviews
  $total_reviews = count($child_posts);
    
  //count the total per checkbox option for all reviews
  foreach($child_posts as $k=>$v):
   $assigned_qualities = types_render_field( "qualities", array( "separator" => "," ,"item"=>$v) );
   foreach($quality_options as $x=>$y):
     if(in_array($x,explode(",",$assigned_qualities))){
              $quality_options[$x]['count'] = $quality_options[$x]['count']+1;  
            }
   endforeach;
  endforeach;
  
  // display/return the result on frontend
  $str ="<div><h2>Qualities:</h2></div><ul>";
  $total_quality_percent = array();
  foreach($quality_options as $k=>$v):
     $per = ($v['count']/$total_reviews)*100;
     $total_quality_percent[$v['title']] = round($per);
     $str = $str."<li><strong>".$v['title']."</strong>: <span>".round($per)."%</span></li>";
  		
  endforeach;
  $str .= "</ul>";
   
  return $str;
  
}

As per the test demo content setup, I can see the same percentage for each checkbox option. Can you pleasae confirm it works as your end as well 🙂

#1785393

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Have you got the chance to check and can you p lease confirm the solution I shared help you to resolve your issue.

#1786227

Hi Minesh,

Yes, it worked great! I spent a couple hours yesterday styling up things - check out the final colorful result: hidden link

Thanks so much!