Skip Navigation

[Cerrado] Cred Form Does Not Save Value Into Database

This support ticket is created hace 7 años, 6 meses. 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

Etiquetado: 

This topic contains 25 respuestas, has 4 mensajes.

Last updated by Shane hace 7 años, 6 meses.

Assisted by: Shane.

Autor
Mensajes
#443237
Screenshot 2016-10-04 17.29.54.png
Screenshot 2016-10-04 17.29.23.png

I am trying to set up review system from the following thread:
https://toolset.com/forums/topic/how-to-get-rating-average-from-cred-for-review/page/3/#post-428863

I setup post fields group called "Average Ratings and Total Reviews" and create two number fields one is called "Ratings Average" with slug ratings-average and another called "Reviews Total" with slug reviews-total. I attached these fields to Restaurant CPT which is a parent of Review.

Under Review CPT I created Radio fields from 0-5 for ratings called Overall Ratings with slug overall-ratings.

Then I create rating form with Overall Ratings radio so it generated ID=831

I follow the reference thread and have my code in my function as follow


//*Update Avg Ratings and Total # of Ratings after review submission
function update_post_ratings($post_id, $form_data)
{
    // if a specific form, change ID to the CRED "Review" ID
    if ($form_data['id']==831)
    {
        //Get ID of Post Being Reviewed
        $current_post_id = $form_data["container_id"];  
            $post = get_post( $current_post_id );
            $args = array(
            'posts_per_page'   => -1,
            'post_type' => 'review',
            'meta_key' => '_wpcf_belongs_' . $post->post_type . '_id',
            'meta_value' => $post->ID,
        );
        $child_posts = get_posts($args);
                 
        $sum = 0;
        $num = 0;
        foreach ($child_posts as $child_post) {
            $ratings = get_post_meta($child_post->ID, 'wpcf-overall-ratings', true);
            if($ratings)
            {
                $sum += $ratings;
                $num ++;
            }
        }
        $average = 0;
        if($num>0)
        {
            $average = $sum/$num;
        }
        $res = $average;
        if($average==0) $res = 0;
        if($average>0.001 && $average<0.5)$res = 0.5;
        if($average>0.501 && $average<1) $res = 1;
        if($average>1.001 && $average<1.5) $res = 1.5;
        if($average>1.501 && $average<2) $res = 2;
        if($average>2.001 && $average<2.5) $res = 2.5;
        if($average>2.501 && $average<3) $res = 3;
        if($average>3.001 && $average<3.5) $res = 3.5;
        if($average>3.501 && $average<4) $res = 4;
        if($average>4.001 && $average<4.5) $res = 4.5;
        if($average>4.501 && $average<5) $res = 5;
          
        update_post_meta( $current_post_id, 'wpcf-ratings-average', $res );
        update_post_meta( $current_post_id, 'wpcf-reviews-total', $num );
    }
}
add_action('cred_save_data', 'update_post_ratings',10,2);

I went ahead testing the review. The review title and review description go through but the form did not saved/pass data to Ratings Average and Reviews Total.

Here's My Form:

 
[credform class="cred-form cred-keep-original"]

	<div>
      <label>Review Title</label>
      [cred_field field='post_title' post='review' value='' urlparam='' placeholder='Title your review...']
	</div>
	<div>
      <label>Review Comment</label>
      [cred_field field='post_content' post='review' value='' urlparam='']
	</div>
 
	<div class="star-rating-wrap">
      <label class="cred-label">Overall Ratings</label>
		<div class="col-sm-6">           
          	<input class="star star-5" id="star-5" type="radio" name="wpcf-overall-ratings" value="5" data-types-value="5" data-wpt-name="wpcf-overall-ratings" title="Outstanding"/>
          	<label class="star star-5" for="star-5"></label>
               
                 
          	<input class="star star-4" id="star-4" type="radio" name="wpcf-overall-ratings" value="4" data-types-value="4" data-wpt-name="wpcf-overall-ratings" title="Very Good"/>
          	<label class="star star-4" for="star-4"></label>
               
                 
          	<input class="star star-3" id="star-3" type="radio" name="wpcf-overall-ratings" value="3" data-types-value="3" data-wpt-name="wpcf-overall-ratings" title="Good"/>      
          	<label class="star star-3" for="star-3"></label>
              
                 
          	<input class="star star-2" id="star-2" type="radio" name="wpcf-overall-ratings" value="2" data-types-value="2" data-wpt-name="wpcf-overall-ratings" title="Poor"/>
          	<label class="star star-2" for="star-2"></label>
              
               
          	<input class="star star-1" id="star-1" type="radio" name="wpcf-overall-ratings" value="1" data-types-value="1" data-wpt-name="wpcf-overall-ratings" title="Very Poor"/>
          	<label class="star star-1" for="star-1"></label>                   
         </div>
	</div>

    <div>
    [cred_field field="form_submit" value="Submit" urlparam=""]
	</div>
[/credform]

Kind Regards,
JSON

#443552

Shane
Supporter

Languages: Inglés (English )

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

Hello,

Thank you for contacting our support forum.

Hmm it seems that you are using regular fields <input> tags to do this but you can use our generic fields to create the form.

What you need to do is go to your form and click "Add Generic field" this way you can add you radio buttons without having to write any code.

From here your field should be saved to the database.

Please let me know if this helps.
Thanks,
Shane

#444259

Hello Shane,

Generic fields does not give option to add class inside input tag and label tag that I need to style on star rating using pure css star rating.
hidden link

Manual Cred fileds can be saved to database using CRED Save data API. Per this ticket I opened awhile back Luo Yang was able to help get it saved via CRED Save hook.
https://toolset.com/forums/topic/cred-input-radio-rating-value-does-not-savepass-to-database/#post-301985

Now I am creating new review system using this thread: https://toolset.com/forums/topic/how-to-get-rating-average-from-cred-for-review/page/3/#post-428863

This method of getting average ratings and total reviews is better than calculate it on the fly that was previously.

I managed to save overall rating from the radio inputs by adding this code below.

if (isset($_POST['wpcf-overall-ratings']))
        { 
            // add it to saved post meta
            add_post_meta($post_id, 'wpcf-overall-ratings', $_POST['wpcf-overall-ratings']);
        }

However, the wpcf-ratings-average and wpcf-reviews-total does pass data and saved in the database.

Here is my new code:

//*Update Avg Ratings and Total # of Ratings after review submission
function update_post_ratings($post_id, $form_data)
{
    // if a specific form, change ID to the CRED "Review" ID
    if ($form_data['id']==831)
    {
        //save overall ratings to database
        if (isset($_POST['wpcf-overall-ratings']))
        { 
            // add it to saved post meta
            add_post_meta($post_id, 'wpcf-overall-ratings', $_POST['wpcf-overall-ratings']);
        }
        
        //Get ID of Post Being Reviewed
        $current_post_id = $form_data["container_id"];  
            $post = get_post( $current_post_id );
            $args = array(
            'posts_per_page'   => -1,
            'post_type' => 'review',
            'meta_key' => '_wpcf_belongs_' . $post->post_type . '_id',
            'meta_value' => $post->ID,
        );
        $child_posts = get_posts($args);
                 
        $sum = 0;
        $num = 0;
        foreach ($child_posts as $child_post) {
            $ratings = get_post_meta($child_post->ID, 'wpcf-overall-ratings', true);
            if($ratings)
            {
                $sum += $ratings;
                $num ++;
            }
        }
        $average = 0;
        if($num>0)
        {
            $average = $sum/$num;
        }
        $res = $average;
        if($average==0) $res = 0;
        if($average>0.001 && $average<0.5)$res = 0.5;
        if($average>0.501 && $average<1) $res = 1;
        if($average>1.001 && $average<1.5) $res = 1.5;
        if($average>1.501 && $average<2) $res = 2;
        if($average>2.001 && $average<2.5) $res = 2.5;
        if($average>2.501 && $average<3) $res = 3;
        if($average>3.001 && $average<3.5) $res = 3.5;
        if($average>3.501 && $average<4) $res = 4;
        if($average>4.001 && $average<4.5) $res = 4.5;
        if($average>4.501 && $average<5) $res = 5;
          
        update_post_meta( $current_post_id, 'wpcf-ratings-average', $res );
        update_post_meta( $current_post_id, 'wpcf-reviews-total', $num );
    }
}
add_action('cred_save_data', 'update_post_ratings',10,2);

Thanks,

JSon

#444283

Shane
Supporter

Languages: Inglés (English )

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

Hi Json,

So which field is not saving to the database? I'm a bit lost 🙁

Also could you provide me with a link to the form so that I can have a look ?

I should be able to check the POST request for the values.

Thanks,
Shane

#444425
CRED 2016-10-08 11.07.04.png
CRED 2016-10-08 11.03.03.png

Hello Shane,

please go to this link: hidden link

then click on "review this restaurant" to go to the form (see screenshots).

Please see the screenshot for the fields it didn't save which it should of.

Thanks,
JSon

#444954

Shane
Supporter

Languages: Inglés (English )

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

Hi Json,

I'm not able to access the form as it seems I need to be logged in.

Would you mind creating a temporary login for me so I can test this out ?

I'll enable the private private fields for your next response.

Thanks,
Shane

#445506

Shane
Supporter

Languages: Inglés (English )

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

Hi Json,

So the field is actually in the payload so the problem is the hook.

Can you just try a simple hook like this and let me know if it populates.

//*Update Avg Ratings and Total # of Ratings after review submission
function update_post_ratings($post_id, $form_data)
{
    // if a specific form, change ID to the CRED "Review" ID
    if ($form_data['id']==831)
    {
        //save overall ratings to database
        if (isset($_POST['wpcf-overall-ratings']))
        { 
            // add it to saved post meta
            add_post_meta($post_id, 'wpcf-overall-ratings', $_POST['wpcf-overall-ratings']);
        }
}
add_action('cred_save_data', 'update_post_ratings',10,2);

Please try this and let me know if it helps.

Thanks,
Shane

#445531

Hello Shane,

I'm not sure where you want me place this code at.

Can you please put to whole codes together so I can see.

Below is the original code:

//*Update Avg Ratings and Total # of Ratings after review submission
function update_post_ratings($post_id, $form_data)
{
    // if a specific form, change ID to the CRED "Review" ID
    if ($form_data['id']==831)
    {
        //Get ID of Post Being Reviewed
        $current_post_id = $form_data["container_id"];  
            $post = get_post( $current_post_id );
            $args = array(
            'posts_per_page'   => -1,
            'post_type' => 'review',
            'meta_key' => '_wpcf_belongs_' . $post->post_type . '_id',
            'meta_value' => $post->ID,
        );
        $child_posts = get_posts($args);
                  
        $sum = 0;
        $num = 0;
        foreach ($child_posts as $child_post) {
            $ratings = get_post_meta($child_post->ID, 'wpcf-overall-ratings', true);
            if($ratings)
            {
                $sum += $ratings;
                $num ++;
            }
        }
        $average = 0;
        if($num>0)
        {
            $average = $sum/$num;
        }
        $res = $average;
        if($average==0) $res = 0;
        if($average>0.001 && $average<0.5)$res = 0.5;
        if($average>0.501 && $average<1) $res = 1;
        if($average>1.001 && $average<1.5) $res = 1.5;
        if($average>1.501 && $average<2) $res = 2;
        if($average>2.001 && $average<2.5) $res = 2.5;
        if($average>2.501 && $average<3) $res = 3;
        if($average>3.001 && $average<3.5) $res = 3.5;
        if($average>3.501 && $average<4) $res = 4;
        if($average>4.001 && $average<4.5) $res = 4.5;
        if($average>4.501 && $average<5) $res = 5;
           
        update_post_meta( $current_post_id, 'wpcf-ratings-average', $res );
        update_post_meta( $current_post_id, 'wpcf-reviews-total', $num );
    }
}
add_action('cred_save_data', 'update_post_ratings',10,2);
#445920

Shane
Supporter

Languages: Inglés (English )

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

Hi Json,

The reason why I want you to try just this section is to ensure that the function actually works.

Add it to your functions.php file

//*Update Avg Ratings and Total # of Ratings after review submission
function update_post_ratings($post_id, $form_data)
{
    // if a specific form, change ID to the CRED "Review" ID
    if ($form_data['id']==831)
    {
        //save overall ratings to database
        if (isset($_POST['wpcf-overall-ratings']))
        { 
            // add it to saved post meta
            add_post_meta($post_id, 'wpcf-overall-ratings', $_POST['wpcf-overall-ratings']);
        }
}
add_action('cred_save_data', 'update_post_ratings',10,2);

As it stands now this function should populate the overall-ratings field with the rating value. I want to see if this is being successfully saved to the database like this.

It's just one step in our debugging process.

Thanks,
Shane

#445950

Hello Shane,

Yes, with this code provided does saved wpcf-overall-ratings to database. When form submitted it saved that value.

Now, what can we do next to use the codes above to calculate ratings average and reviews total then pass that data to custom fields wpcf-ratings-average and wpcf-reviews-total so that we can display average rating and total reviews at front-end.

Thanks,
JSon

#446397

Shane
Supporter

Languages: Inglés (English )

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

Hi Json,

The problem could be here .

 $current_post_id = $form_data["container_id"];  
            $post = get_post( $current_post_id );

Since the review form is not currently on the same page as the item to be reviewed so the correct ID is not being applied.

If thats the case then we need to modify that line since the current review form is on another page. We need to use that review ID to get its parent.

So you will need to do this.

$parent_post = get_post_meta($post_id,"_wpcf_belongs_(parentcptslug)");
$post = get_post( $parent_post );

Please try this and let me know if it helps. Also replace the parentcptslug with the actual slug of the cpt and let me know if this works.

Thanks,
Shane

#446417

Hello Shane,

It's still not save (ratings average) 'wpcf-ratings-average' and (reviews total) 'wpcf-reviews-total' to database.

Here's the code:

 
//*Update Avg Ratings and Total # of Ratings after review submission
function update_post_ratings($post_id, $form_data)
{
    // if a specific form, change ID to the CRED "Review" ID
    if ($form_data['id']==831)
    {
        //save overall ratings to database
        if (isset($_POST['wpcf-overall-ratings']))
        { 
            // add it to saved post meta
            add_post_meta($post_id, 'wpcf-overall-ratings', $_POST['wpcf-overall-ratings']);
        }        
        //Get ID of Post Being Reviewed
        $parent_post = get_post_meta($post_id,"_wpcf_belongs_(restaurants)");
			$post = get_post( $parent_post );
            $args = array(
            'posts_per_page'   => -1,
            'post_type' => 'review',
            'meta_key' => '_wpcf_belongs_' . $post->post_type . '_id',
            'meta_value' => $post->ID,
        );
        $child_posts = get_posts($args);
                   
        $sum = 0;
        $num = 0;
        foreach ($child_posts as $child_post) {
            $ratings = get_post_meta($child_post->ID, 'wpcf-overall-ratings', true);
            if($ratings)
            {
                $sum += $ratings;
                $num ++;
            }
        }
        $average = 0;
        if($num>0)
        {
            $average = $sum/$num;
        }
        $res = $average;
        if($average==0) $res = 0;
        if($average>0.001 && $average<0.5)$res = 0.5;
        if($average>0.501 && $average<1) $res = 1;
        if($average>1.001 && $average<1.5) $res = 1.5;
        if($average>1.501 && $average<2) $res = 2;
        if($average>2.001 && $average<2.5) $res = 2.5;
        if($average>2.501 && $average<3) $res = 3;
        if($average>3.001 && $average<3.5) $res = 3.5;
        if($average>3.501 && $average<4) $res = 4;
        if($average>4.001 && $average<4.5) $res = 4.5;
        if($average>4.501 && $average<5) $res = 5;
            
        update_post_meta( $current_post_id, 'wpcf-ratings-average', $res );
        update_post_meta( $current_post_id, 'wpcf-reviews-total', $num );
        
    }
}
add_action('cred_save_data', 'update_post_ratings',10,2);
#446764

Shane
Supporter

Languages: Inglés (English )

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

Hi Json,

Taking a look at the code change this line from:

$parent_post = get_post_meta($post_id,"_wpcf_belongs_(restaurants)");

To

$parent_post = get_post_meta($post_id,"_wpcf_belongs_restaurants");

Please let me know if it works now.
Thanks,
Shane

#446835
Screenshot 2016-10-14 14.31.07.png
Screenshot 2016-10-14 14.28.00.png

Hello Shane,

I tried that code provided is not Saved.

As you can see the screenshots. The ratings saved but not the ratings average and review total.

Thanks,
JSon

Here's the code that was edited.

//*Update Avg Ratings and Total # of Ratings after review submission
function update_post_ratings($post_id, $form_data)
{
    // if a specific form, change ID to the CRED "Review" ID
    if ($form_data['id']==831)
    {
        //save overall ratings to database
        if (isset($_POST['wpcf-overall-ratings']))
        { 
            // add it to saved post meta
            add_post_meta($post_id, 'wpcf-overall-ratings', $_POST['wpcf-overall-ratings']);
        }        
        //Get ID of Post Being Reviewed
        $parent_post = get_post_meta($post_id,"_wpcf_belongs_restaurants");
			$post = get_post( $parent_post );
            $args = array(
            'posts_per_page'   => -1,
            'post_type' => 'review',
            'meta_key' => '_wpcf_belongs_' . $post->post_type . '_id',
            'meta_value' => $post->ID,
        );
        $child_posts = get_posts($args);
                   
        $sum = 0;
        $num = 0;
        foreach ($child_posts as $child_post) {
            $ratings = get_post_meta($child_post->ID, 'wpcf-overall-ratings', true);
            if($ratings)
            {
                $sum += $ratings;
                $num ++;
            }
        }
        $average = 0;
        if($num>0)
        {
            $average = $sum/$num;
        }
        $res = $average;
        if($average==0) $res = 0;
        if($average>0.001 && $average<0.5)$res = 0.5;
        if($average>0.501 && $average<1) $res = 1;
        if($average>1.001 && $average<1.5) $res = 1.5;
        if($average>1.501 && $average<2) $res = 2;
        if($average>2.001 && $average<2.5) $res = 2.5;
        if($average>2.501 && $average<3) $res = 3;
        if($average>3.001 && $average<3.5) $res = 3.5;
        if($average>3.501 && $average<4) $res = 4;
        if($average>4.001 && $average<4.5) $res = 4.5;
        if($average>4.501 && $average<5) $res = 5;
            
        update_post_meta( $current_post_id, 'wpcf-ratings-average', $res );
        update_post_meta( $current_post_id, 'wpcf-reviews-total', $num );
    }
}
add_action('cred_save_data', 'update_post_ratings',10,2);
#446995

I think the problem lies in getting the id of the parent post. You might want to doublecheck that the slug of your restaurants post type is "restaurants" and not the singular, "restaurant". Also the same for your reviews post type!

Since it's outputting the parent post ID in the url, you could probably just use a GET:

        //Get ID of Post Being Reviewed
        $parent_post = $_GET['parent_restaurants_id'];
            $post = get_post( $parent_post );
            $args = array(
            'posts_per_page'   => -1,
            'post_type' => 'review',
            'meta_key' => '_wpcf_belongs_' . $post->post_type . '_id',
            'meta_value' => $post->ID,
        );

Does this work?

El debate ‘[Cerrado] Cred Form Does Not Save Value Into Database’ está cerrado y no admite más respuestas.