Startseite › Toolset Professional Support › [Gelöst] Sum radio values using 'Value to show' insted of 'Custom field content' in php
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.
Heute stehen keine Supporter zur Arbeit im Werkzeugsatz-Forum zur Verfügung. Sie können gern Tickets erstellen, die wir bearbeiten werden, sobald wir online sind. Vielen Dank für Ihr Verständnis.
Sun | Mon | Tue | Wed | Thu | Fri | Sat |
---|---|---|---|---|---|---|
- | 12:00 – 17:00 | 12:00 – 17:00 | 12:00 – 17:00 | 12:00 – 17:00 | 12:00 – 17:00 | - |
- | 18:00 – 21:00 | 18:00 – 21:00 | 18:00 – 21:00 | 18:00 – 21:00 | 18:00 – 21:00 | - |
Supporter timezone: Asia/Karachi (GMT+05:00)
Tags: Types fields API, Types plugin
Verwandte Dokumentation:
Dieses Thema enthält 14 Antworten, hat 2 Stimmen.
Zuletzt aktualisiert von Noman vor 7 Jahren, 1 Monat.
Assistiert von: Noman.
I am trying to: Sum radio buttons values using 'Value to show' instead of 'Custom field content' in php formula
Link to a page where the issue can be seen: versteckter Link
I expected to see: values of the sum in the calculated fields using the 'value to shoe'
Instead, I got: three calculated value instead of all calculated values. the calculated values seems to be based on the 'Custom field content' and not the 'Value to show'
in my custom function php file (versteckter Link) I created a "// calculate Investment Profile" formula that sum the radio buttons values. the formula is not fully working, not sure why. as I see only a few fields been calculated and not all.
the main question here is how to use the 'Value to show' in the formula instead of the 'Custom field content' and to see if I have any errors in my code that prevent the formula from working correctly.
the custom post fileds group name is "Investment Profile" and the post type is "Investment Profile"
Thanks,
David
I was able to resolve the calculation error in the php formula.
the main question here is how to use the 'Value to show' in the formula instead of the 'Custom field content' of the radio custom fields.
Thanks,
David
Hi David,
Thank you for contacting Toolset Support. If you are using a Radio field and “Value to show >> Show one of these values option, then you need to use type's shortcode in the php code instead of get_post_meta function. Like this:
echo(do_shortcode('[types field="book-price" id="67"]'));
==> Whereas "book-price" will be field slug, and "67" will be the post id.
And in Types >> Field Group >> setup your Radio field settings as shown in attached screenshot.
Thank you
Thanks Noman,
allow me to be more specific so I know I implement this correctly
I have the following code in my function.php:
// calculate Investment Profile function calculate_investment_profile( $post_ID ) { if ( get_post_type( $post_ID ) == 'investment-profile' ) { // Investment objectives $portfolio_intent_ip = get_post_meta($post_ID, 'wpcf-portfolio-intent', true); $protfolio_major_goal_ip = get_post_meta($post_ID, 'wpcf-protfolio-major-goal', true); ... rest of the code ...
So I need to change the code to:
// calculate Investment Profile function calculate_investment_profile( $post_ID ) { if ( get_post_type( $post_ID ) == 'investment-profile' ) { // Investment objectives $portfolio_intent_ip = echo(do_shortcode('[types field='wpcf-portfolio-intent' id=$post_ID]')); $portfolio_intent_ip = echo(do_shortcode('[types field='wpcf-protfolio-major-goal' id=$post_ID]')); ... rest of the code ...
In other words:
The line - $portfolio_intent_ip = get_post_meta($post_ID, 'wpcf-portfolio-intent', true);
Change with - $portfolio_intent_ip = echo(do_shortcode('[types field='wpcf-portfolio-intent' id=$post_ID]'));
and doing so for the other lines as well...
is this correct?
thanks,
David
Hi Noman,
after working on this issue all day I realized the echo function will not work for me as I'm not building a template and need to plot the value on a page. this is already possible with the available short code in toolset.
my php calculation works well with the 'Custom field content' but my goal is to use the 'Value to show' in my calculation.
I can see the value using a short code on the custom post page using a short code with the same slug I can get the raw value in my php calculation but not the 'Value to show'
when I use:
$portfolio_intent_ip = do_shortcode('[types field="portfolio-intent"][/types] id=$post_ID');
no value is been used for $portfolio_intent_ip
using the:
$portfolio_intent_ip = get_post_meta($post_ID, 'wpcf-portfolio-intent', true);
provide the raw data as it stored in the DB
in the custom post I use the following short codes for tests
[types field='portfolio-intent'][/types] //display the 'Value to show' [types field='portfolio-intent' output='raw'][/types] //display the 'Custom field content'
hope this clear the issue a bit more.
thanks,
David
Hello David,
There is mistake in the updated code. I will check at my end and send you updated code shortly as example.
Thank you
I have been working on this and trying to figure out why its not working. I have found that following shortcode returns ‘Value to show’ on page but not working in ‘save_post’ action:
do_shortcode("[types field='book-price' id='260']");
I have forwarded this to our development team and waiting to get more info on this, I will update you accordingly.
In the meantime, if you can think of an alternate way for displaying these values and only use "Custom field content" for calculations, that would be ideal solution as for now.
Thank you
Thanks Noman,
I prepare to wait for your update if possible.
The only workaround will be to have a 'if...elseif....else' statement in the PHP formula to replace the 'Custom field content' with 'Value to show' (hard coded into the formula) and to do this for each radio custom field I have. I think I have about 14 of them, that means 14 'if.. else... else...' statement... (not ideal)
I will give it a try but will be happy to get you help as I'm not a developer by education 🙂
// calculate Investment Profile function calculate_investment_profile( $post_ID ) { if ( get_post_type( $post_ID ) == 'investment-profile' ) { // Investment objectives $portfolio_intent_ip = get_post_meta($post_ID, 'wpcf-portfolio-intent', true); if ($portfolio_intent_ip = "1") { $q1 = "0"; } elseif ($portfolio_intent_ip = "2") { $q1 = "5"; } elseif ($portfolio_intent_ip = "3") { $q1 = "10"; } else { $q1 = "20"; } .... // Total Investment objectives $total_invs_objec = $q1 + .... + $qn; update_post_meta( $post_ID, 'wpcf-total-investment-objectives', $total_invs_objec ); ....
Thanks,
David
Hi Noman,
the code I tried above didn't work.
not sure why the ticket was closed. I must accidently clicked the wrong radio button.
I still need help with this.
Thanks,
David
Hello David,
I am still waiting from our developer but I managed to find an alternate method to get those values, I used WordPress’s get_option function now: https://developer.wordpress.org/reference/functions/get_option/
And this is working good with the “Value to show” options. Here is the updated code to use:
function get_radio_display_value($post_id, $field_slug, $all_fields){ $field_selected_val = get_post_meta($post_id, 'wpcf-'.$field_slug, true); $field_options = $all_fields[$field_slug]['data']['options']; $display_value = ''; foreach($field_options as $field) { if( $field['value'] == $field_selected_val ) { $display_value = $field['display_value']; break; } } return $display_value; } // calculate Investment Profile function calculate_investment_profile( $post_ID ) { if ( get_post_type( $post_ID ) == 'investment-profile' ) { // CPT Slug $all_post_fields = get_option( 'wpcf-fields' ); //here you can get field values. Just needs to update slugs i.e 'portfolio-intent' and 'protfolio-major-goal' $portfolio_intent_value = get_radio_display_value($post_ID, 'portfolio-intent', $all_post_fields); $protfolio_major_goal = get_radio_display_value($post_ID, 'protfolio-major-goal', $all_post_fields); } } add_action( 'save_post', 'calculate_investment_profile', 99);
==> Please replace your field names in this accordingly and look for comments in the above code.
I hope this will work good for you. Thanks
P.S. your site login got changed so I tried this at my own test site.
Thanks Noman,
I have tried the code provided above as is but it didn't work for me.
instead I was able to use the if else if else statement as follow:
if ($portfolio_intent_ip == '1') { $q1 = '0'; } elseif ($portfolio_intent_ip == '2') { $q1 = '5'; } elseif ($portfolio_intent_ip == '3') { $q1 = '10'; } else { $q1 = '20'; }
I guess it will be a workaround until i have a solution from your team. i really dont like to have hard coded values in the code...
i'm waiting for you update.
Thanks,
David
The code I provided cannot be used as it is, it requires proper addition into your current code to perform calculations. As soon as I hear from our developer I will update you for sure. get_option function is also a good way as it is returning us correct values.
The info I have provided is basically for experience WP developers, as you are not comfortable with this level of coding I recommend to have someone who can read and modify code in a better way, because we can't consult much on the custom coding. The method and approach I have provided in previous response was to provide a general idea for the solution and which returns the values to be used for calculations etc.
Custom coding & custom development is out of support policy (https://toolset.com/toolset-support-policy/). So I highly recommend you to contact Toolset recommended service providers to utilize the provided code examples and info (or some freelance developer). We have some recommended list of service providers here if you would like to take a look: https://toolset.com/consultant/
If you would like to provide me with WP login info again, I can try to add this into your current code and future modifications to the code will be needed from your end.
Thanks
I totally understand and respect this.
at the moment I use my workaround and its working just fine.
I will wait for your update before changing the code again.
Thanks,
David
Thank you for your understanding in this regard.
I am changing ticket status as escalated so I can reply to it later on.
Our developer informed that it is not recommended to use these "Values to show" fields for calculations. These are just to show as label or text. Normal custom field content should be used for calculations.
Other than that the get_option method that I explained above if you like to implement using that:
https://toolset.com/forums/topic/sum-radio-values-using-value-to-show-insted-of-custom-field-content-in-php/#post-574060
Thanks