/** * Custom shortcode to calculate and display the result on a page. */ function calculate_result_shortcode() { // Get the submitted form entry. $entry_id = FrmAppHelper::get_param( 'entry' ); $entry = FrmEntry::getOne( $entry_id );
$form_id = $entry->form_id; // Calculate the result for vloerverwarming. $result = calculate_result_zonnepanelen( $entry ); // Format the result to display two decimals, change the decimal symbol to a comma, and remove the thousand separator. $formatted_result = number_format( $result, 2, ',', '' ); return $formatted_result; } add_shortcode( 'calculate_result', 'calculate_result_shortcode' );
Why is my $form_id always empty in the above PHP code I wrote? I want to do check on the form_id because I want to do different calculations based on what form was used in the same shortcode as I am using that shortcode in a page I show on confirmation. Can anyone help me out?
Edit: added the functions below
/** * Custom function to calculate the result of vloerverwarming based on the given parameters. * * @param object $entry The form entry object. * * @return float The calculated result. */ function calculate_result_vloerverwarming( $entry ) { // Get the value of the number field $field1_value = FrmProEntriesController::get_field_value_shortcode( array( 'field_id' => '67', 'entry' => $entry ) ); // Get the selected option from the dropdown field $dropdown_value = FrmProEntriesController::get_field_value_shortcode( array( 'field_id' => '66', 'entry' => $entry ) ); // Perform the calculation based on the selected option and multiplication factor. if ( $dropdown_value === 'Droog' ) { $result = $field1_value * 32; } else { $result = $field1_value * 58; } $result = apply_building_year_calculation( $entry, $result, '65' ); return $result; }
/** * Custom function to calculate the result for zonnepanelen based on the given parameters. * * @param object $entry The form entry object. * * @return float The calculated result. */ function calculate_result_zonnepanelen( $entry ) { // Get the value of the dropdown field $dropdown_value = FrmProEntriesController::get_field_value_shortcode( array( 'field_id' => '53', 'entry' => $entry ) ); // Get the value of the number field for available surface in square meters $surface_value = FrmProEntriesController::get_field_value_shortcode( array( 'field_id' => '54', 'entry' => $entry ) ); // Get the value of the number field for yearly energy consumption $energy_consumption = FrmProEntriesController::get_field_value_shortcode( array( 'field_id' => '61', 'entry' => $entry ) ); // Calculate the energy production based on available surface $energy_production = $surface_value * 200; // Determine the factor to multiply based on the dropdown selection $multiplication_factor = ( $dropdown_value === 'Plat' ) ? 1 : 1.15; // Check if the energy production exceeds the energy consumption if ( $energy_production > $energy_consumption ) { // Use the energy consumption as the upper limit $result = $energy_consumption * 1.36 * $multiplication_factor; } else { // Use the energy production multiplied by 1.36 and the multiplication factor $result = $energy_production * 1.36 * $multiplication_factor; } // Apply the building year calculation to the result $result = apply_building_year_calculation( $entry, $result, '52' ); return $result; } /** * Apply the building year calculation to the result. * * @param object $entry The form entry object. * @param float $result The current result. * @param float $building_year_field_id The building year field id. * * @return float The result with the building year calculation applied. */ function apply_building_year_calculation( $entry, $result, $building_year_field_id ) { // Get the value of the slider field $building_year = FrmProEntriesController::get_field_value_shortcode( array( 'field_id' => $building_year_field_id, 'entry' => $entry ) ); // Get the current year. $current_year = intval( date( 'Y' ) ); // Calculate the age of the building. $building_age = $current_year - intval( $building_year ); // Set the multiplication factor based on the building age. $multiplication_factor = ( $building_age < 10 ) ? 1.21 : 1.06; // Apply the multiplication factor to the result. $result *= $multiplication_factor; return $result; }
What do you mean with "require or they won't work"? I read in the docs that you've linked me the following: "Three parameters are passed to the shortcode callback function. You can choose to use any number of them including none of them."
I had a shortcode I was debugging and it would not work for anything I tried. Then I realized I had left out the $atts parameter in the callback function. The WordPress Shortcode API docs say you don't need any parameters, but this shortcode wouldn't fire without the $atts parameter. As soon as I added the parameter, the shortcode worked. Your shortcode callback function must be declared as:
function calculate_result_shortcode( $atts ) {
}
Try it. It won't hurt anything.
Please login or Register to submit your answer
Have you put debugging or logging code in to see what $entry_id is set to? The two lines that follow the assignment of $entry_id are fine. Also, you can pass a parameter to a custom shortcode, which may be a better way to pass the entry value you're looking for.
Hey Rob, I've added an echo on the $entry_id and you seem right, it seems to return empty. (Echo on $formatted_result does correctly display though, just to be sure as I am no PHP expert at all.)
echo 'Entry ID: ' . $entry_id . '';
I did already try out the option of passing a parameter to my custom shortcode, but I was unsure how I would need to go about retrieving the entry and pass it as a parameter in my custom shortcode. Any guidelines on that would be greatly appreciated if this would fix my problem.
I'll give you a short idea of what I'm trying to achieve. I have a page with 4 tabs in it to switch between 4 different forms providing quote calculators. Those 4 forms all collect some input and do some calculations based on those input and then return a estimated price. I show this estimated price in another WordPress page that I set in the confirmation action as "show page content". Showing the estimated price with the shortcode above is no problem as is retrieving the values from 2 different forms using the same shortcode but different function inside (until I figure out how to switch on form_id).
So it kind of surprises me that I can retrieve the values correctly when I pass $entry to calculate_result_zonnepanelen() for example, since the above shows that $entry_id returns empty so it shouldn't be able to retrieve $entry in the line below, right? Any thoughts? Thanks in advance!
I don't know what calculate_result_zonnepanelen() is, though it's probable that the code falls back to a default value and you're just getting lucky that there's a value to process. As far as passing the entry value into the shortcode, as Victor states below, there's a method for sending params to a shortcode. Without seeing your pages, forms and views, I can't really explain how to figure what to send for the entry id (or key).
<p>Rob, I've added the functions in the original post above. I can assure you that it isn't falling back to a default value. The results always differ, taking the input in correctly. As far as sending params to the shortcode, I've tried that before, but as I stated in my previous comment, I was unsure how to pass the actual $entry to the shortcode or rather how the shortcode knows what the current $entry is, since it should be dynamic for every new entry. The four forms can be found on <a href="https://komposiet.com/offerte</a></a>"" rel="nofollow">https://komposiet.com/offerte</a> by the way, the one where the calculation works is "Vloerverwarming".</p>
I've also enabled AJAX on the forms, should I do something special to retrieve the entry_id?