Skip Navigation

[Resolved] Adding field values

This support ticket is created 3 years, 6 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: Asia/Karachi (GMT+05:00)

This topic contains 9 replies, has 2 voices.

Last updated by Waqar 3 years, 6 months ago.

Assisted by: Waqar.

Author
Posts
#1808721
screenshot2.jpg

Tell us what you are trying to do?

Hi Waqar,

sorry that I have to claim you again. The code you gave me to add the total number of checked boxes in each categorie is not working.


add_action('cred_save_data','func_connect_child_posts',15,2);
function func_connect_child_posts($post_id,$form_data) {
   if ($form_data['id']==339) {
 
        $field-1 = $_POST['wpcf-count-checkboxes-auto-verkehr'];
        $field-2 = $_POST['wpcf-count-checkboxes-bauen-renovieren'];
        $field-3 = $_POST['wpcf-count-checkboxes-behoerden-verbaende'];
 
        $sum = $field-1 + $field-2 + $field-3;
 
        update_post_meta($post_id, 'wpcf-count-checkboxes-gesamtzahl', $sum);
    }
}  

There is an error message in the "Custom code snippets - Settings" and the the field "count-checkboxes-gesamtzahl" remains empty in the form:

[2020-10-12 13:01:41, frontend] syntax error, unexpected '=' in /homepages/15/d845764028/htdocs/clickandbuilds/FirmeninfoRegional/wp-content/toolset-customizations/count-checkboxes-total.php on line 15.
A problem occurred when executing snippet "count-checkboxes-total". The result of include_once is: ""

(I've attached a screenshot of the error message)

I've tried something out myself but couldn't come up with a solution. Hope you can help me again.

Is there any documentation that you are following?
Earlier support ticket "How to count number of selected chechboxes "
https://toolset.com/forums/topic/how-to-count-number-of-selected-chechboxes/

Thank you in advance for your further help.
Jenny

Is there a similar example that we can see?
No

What is the link to your site?
hidden link

#1810017

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Jenny,

Thanks for writing back and I apologize for the trouble.

I shouldn't have used "-" characters in the field name variables, in my example snippet.


add_action('cred_save_data','func_connect_child_posts',15,2);
function func_connect_child_posts($post_id,$form_data) {
   if ($form_data['id']==339) {
  
        $field1 = $_POST['wpcf-count-checkboxes-auto-verkehr'];
        $field2 = $_POST['wpcf-count-checkboxes-bauen-renovieren'];
        $field3 = $_POST['wpcf-count-checkboxes-behoerden-verbaende'];
  
        $sum = $field1 + $field2 + $field3;
  
        update_post_meta($post_id, 'wpcf-count-checkboxes-gesamtzahl', $sum);
    }
}

regards,
Waqar

#1810149
screenshot3.jpg

Hi Waqar,

thanks for correcting the code. I completely overlooked that.
The error message has disappeared. But the field "count-checkboxes-gesamtzahl" always outputs a 0 (Screenshot)
Here is the full code that I am using. Is there anything wrong?
I used the same Form-ID 352 for the new part of the code (this is the form where you can tick the checkboxes).

add_action('cred_save_data','func_connect_child_posts',15,2);
function func_connect_child_posts($post_id,$form_data) {
    if ($form_data['id']==352) {
        if(!empty($_POST['wpcf-checkboxes-auto-verkehr'])) {
                update_post_meta($post_id, 'wpcf-count-checkboxes-auto-verkehr', sizeof($_POST['wpcf-checkboxes-auto-verkehr']));
        }
    }

    if ($form_data['id']==352) {
        if(!empty($_POST['wpcf-checkboxes-bauen-renovieren'])) {
                update_post_meta($post_id, 'wpcf-count-checkboxes-bauen-renovieren', sizeof($_POST['wpcf-checkboxes-bauen-renovieren']));
        }
    }
  
  
   if ($form_data['id']==352) {
        if(!empty($_POST['wpcf-checkboxes-behoerden-verbaende'])) {
                update_post_meta($post_id, 'wpcf-count-checkboxes-behoerden-verbaende', sizeof($_POST['wpcf-checkboxes-behoerden-verbaende']));
        }
    }
  
  
   if ($form_data['id']==352) {
   
        $field1 = $_POST['wpcf-count-checkboxes-auto-verkehr'];
        $field2 = $_POST['wpcf-count-checkboxes-bauen-renovieren'];
        $field3 = $_POST['wpcf-count-checkboxes-behoerden-verbaende'];
   
        $sum = $field1 + $field2 + $field3;
   
        update_post_meta($post_id, 'wpcf-count-checkboxes-gesamtzahl', $sum);
    }

}

I am so grateful to you for your help.

regards,
Jenny

#1811213

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Jenny,

The values for the fields "wpcf-count-checkboxes-auto-verkehr", "wpcf-count-checkboxes-bauen-renovieren" and "wpcf-count-checkboxes-behoerden-verbaende" are not available directly from the form, which is why they are not available in global variable "$_POST".

You'll need to get them the same way as for the "update_post_meta" functions:


add_action('cred_save_data','func_connect_child_posts',15,2);
function func_connect_child_posts($post_id,$form_data) {
    if ($form_data['id']==352) {
        if(!empty($_POST['wpcf-checkboxes-auto-verkehr'])) {
                update_post_meta($post_id, 'wpcf-count-checkboxes-auto-verkehr', sizeof($_POST['wpcf-checkboxes-auto-verkehr']));
        }
    }
 
    if ($form_data['id']==352) {
        if(!empty($_POST['wpcf-checkboxes-bauen-renovieren'])) {
                update_post_meta($post_id, 'wpcf-count-checkboxes-bauen-renovieren', sizeof($_POST['wpcf-checkboxes-bauen-renovieren']));
        }
    }
   
   
   if ($form_data['id']==352) {
        if(!empty($_POST['wpcf-checkboxes-behoerden-verbaende'])) {
                update_post_meta($post_id, 'wpcf-count-checkboxes-behoerden-verbaende', sizeof($_POST['wpcf-checkboxes-behoerden-verbaende']));
        }
    }
   
   
   if ($form_data['id']==352) {
    
        $field1 = sizeof($_POST['wpcf-checkboxes-auto-verkehr']);
        $field2 = sizeof($_POST['wpcf-checkboxes-bauen-renovieren']);
        $field3 = sizeof($_POST['wpcf-checkboxes-behoerden-verbaende']);
    
        $sum = $field1 + $field2 + $field3;
    
        update_post_meta($post_id, 'wpcf-count-checkboxes-gesamtzahl', $sum);
    }
 
}

regards,
Waqar

#1817699

Hi Waqar,

Sorry for my late answer. I tested your code and it works great now. Thank you for your great support on my issue.

I have one last question on this subject. I would like to calculate a price for the number of checkboxes and output it in the field "price-checkboxes".

    ...
   $price = ($field1 + $field2 + $field3)* 1.80;
   update_post_meta($post_id, 'wpcf-price-checkboxes', $price);

The script calculates correctly and gives me an output like this e.g. 23.468.

Is there a possibility with the PHP script to round the number to two decimal places and to replace the comma with a point (German spelling). So that the output looks like this 23,47. I've tried something like this but it doesn't work 🙁

   if ($form_data['id']==352) {
     
        $field1 = sizeof($_POST['wpcf-checkboxes-auto-verkehr']);
        $field2 = sizeof($_POST['wpcf-checkboxes-bauen-renovieren']);
        $field3 = sizeof($_POST['wpcf-checkboxes-behoerden-verbaende']);
     
        $price = ($field1 + $field2 + $field3)* 1.80;
        $price = str_replace(".",",", $price);
        var_dump(round($price, 2));
     
        update_post_meta($post_id, 'wpcf-price-checkboxes', $price);
    }

Hope you can help me with this one last time.

Regards and a big thank you,
Jenny

#1817815

Hi Waqar,

I managed to round the number to two decimal places with this code:

 if ($form_data['id']==352) {
     
        $field1 = sizeof($_POST['wpcf-checkboxes-auto-verkehr']);
        $field2 = sizeof($_POST['wpcf-checkboxes-bauen-renovieren']);
        $field3 = sizeof($_POST['wpcf-checkboxes-behoerden-verbaende']);
     
        $price = (($field1 + $field2 + $field3 - 1)* 1.80 + 85.00)*0.16;
        $pricerounded = round($price, 2);
     
        update_post_meta($post_id, 'wpcf-geamtpreis-checkboxen', $pricerounded);
    }

Can I also output it so that it fills both decimal places with 0?
For example: 10.30 or 10.00

And replacing the point with a comma still doesn't work.
I try to add something like this to the code but than the output field stays empty.

...
$pricecomma = str_replace(".",",", $pricerounded);

update_post_meta($post_id, 'wpcf-geamtpreis-checkboxen', $pricecomma);

#1819741

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Jenny,

Thanks for the update and glad that my message helped.

You can perform the rounding and formatting with ",00", in the same line, like this:


$pricerounded = round($price).',00';

regards,
Waqar

#1819791

Hi Waqar,

thank you for your answer.
I've tried your suggestion, but the output field remains empty 🙁


if ($form_data['id']==352) {
     
        $field1 = sizeof($_POST['wpcf-checkboxes-auto-verkehr']);
        $field2 = sizeof($_POST['wpcf-checkboxes-bauen-renovieren']);
        $field3 = sizeof($_POST['wpcf-checkboxes-behoerden-verbaende']);
     
        $price = ($field1 + $field2 + $field3 - 1)* 1.80 + 85.00)*0.16;
        $pricerounded = round($price).',00';
       
     
        update_post_meta($post_id, 'wpcf-geamtpreis-checkboxen', $pricerounded);
    }

#1819903

Hi Waqar,

You gave me an idea. I found a solution to fill the decimal places with 0.
Now my output looks like this e.g. 11.00


        $pricerounded = number_format(round($price, 2), 2);

Now my only problem is to replace the point with a comma.
I've tried something like this but then the output field remains empty and i don't understand why.

$pricerounded = number_format(round($price, 2), 2, ',', '.');

Hope we can also find a solution for that ; )

Regards,
Jenny

#1822231

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Jenny,

In your second last message's code snippet, I noticed that an opening bracket is missing at line# 8 , which should throw a PHP error:


$price = (($field1 + $field2 + $field3 - 1)* 1.80 + 85.00)*0.16;

As for the next line (line# 9) that I suggested, it should work for the rounding and formatting part.

I'll recommend to test it again and use value debugging at each line to see at which point it is failing.

regards,
Waqar

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.