Home › Toolset Professional Support › [Resolved] Calculate search input field with custom fields
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 |
---|---|---|---|---|---|---|
- | 10:00 – 13:00 | 10:00 – 13:00 | 10:00 – 13:00 | 10:00 – 13:00 | 10: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/Kolkata (GMT+05:30)
This topic contains 10 replies, has 2 voices.
Last updated by markP-19 1 year, 10 months ago.
Assisted by: Minesh.
I would like help to calculate search input field with custom fields in posts. The result should appear in the view loop.
I have the code for calculating two custom fields and it works fine calculating the values of the posts in the view loop:
// Calculate shortcode
add_shortcode('wpv-calculate', 'calculate_shortcode');
function calculate_shortcode($atts) {
return wpv_condition($atts);
}
Using this shortcode to calculate the fields in view loop:
[wpv-calculate evaluate=" [types field='bredde' format='FIELD_VALUE'][/types] + [types field='pris' format='FIELD_VALUE'] [/types] "]
In the view search there is a field to enter height. When the user enters any height, it should take this value and calculate with the custom fields and the result should then appear for every post in the view loop.
How can i get this to work?
Regards
Note that the posts have the height as a number custom field in the field group for the post type, which is why it is provided in the view search. But the posts doesn't have the height values entered, as the user needs to type this in the view search first, making it able to calculate with the other custom fields. Those other custom fields already have a defined value.
Updated calculation:
[wpv-calculate evaluate=" [types field='hojde' format='FIELD_VALUE'][/types] * [types field='pris' format='FIELD_VALUE'] [/types] "]
The entered 'hojde' in view search should multiply with the already defined custom field 'pris' and calculate the result in the view loop.
Hello. Thank you for contacting the Toolset support.
I would like to know what is the URL param of your height field?
Basically, lets say if your field URL param is "wpv-height" then you can catch the URL param using the [wpv-search-term] shortcode:
=> https://toolset.com/documentation/programmer-reference/views/views-shortcodes/#wpv-search-term
For example to catch the URL param "wpv-height" value, you can use the shortcode as:
[wpv-search-term param='wpv-height']
So, in your code - it should look like:
[wpv-calculate evaluate=" [wpv-search-term param='wpv-height'] * [types field='pris' format='FIELD_VALUE'] [/types] "]
Can you please try this solution and check if that help you to resolve your issue.
The search parameters and the calculation is now like this:
[wpv-calculate evaluate=" [wpv-search-term param='wpv-wpcf-folieprint-hojde'] * [wpv-search-term param='wpv-wpcf-folieprint-bredde'] * [types field='pris-folieprint' format='FIELD_VALUE'] [/types] "]
But when testing it gives the error: "Error while parsing the evaluate expression".
And it seems like it still search the custom field "folieprint-hojde" and "folieprint-bredde" even though there are no values in these fields for the posts.
So, if your requirement is that the calculation should work only when both search params are available then you should uses the following conditional statement. It will check that if both params are available then it will run the shortcode [wpv-calculate].
[wpv-conditional if="( '[wpv-search-term param='wpv-wpcf-folieprint-hojde']' ne '' ) AND ( '[wpv-search-term param='wpv-wpcf-folieprint-bredde']' ne '' )"] [wpv-calculate evaluate=" [wpv-search-term param='wpv-wpcf-folieprint-hojde'] * [wpv-search-term param='wpv-wpcf-folieprint-bredde'] * [types field='pris-folieprint' format='FIELD_VALUE'] [/types] "] [/wpv-conditional]
Yes that's right, thanks
I've just tested again. But the problem is now, that it doesn't produce any results, unless the posts have values in these fields; 'wpv-wpcf-folieprint-hojde' and 'wpv-wpcf-folieprint-hojde'.
These fields shouldn't have any value in the post, because the customer needs to enter values in view search for these fields manually for the calculation. In field settings, i've tried setting Type of control to "Using manually entered values" but with no luck.
Can you please share problem URL where you added your view and admin access details.
*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin) 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.
Could you please check now: hidden link
I've added the following view's filter "wpv_filter_query" to "Custom Code" section offered by Toolset:
=> hidden link
add_filter( 'wpv_filter_query', 'func_remove_unwanted_arguments',10,3 ); function func_remove_unwanted_arguments( $query_args ,$view_settings, $view_id ) { if ($view_id == 224 ) { $all_args = $query_args['meta_query']; $unwanted_fields = array('wpcf-folieprint-hojde','wpcf-folieprint-bredde'); foreach($all_args as $k=>$v): if(in_array($v['key'],$unwanted_fields)){ unset($query_args['meta_query'][$k]); } endforeach; } return $query_args; }
More info:
- https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
- https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/
Thanks! it works.
Last thing, how can i make it possible to use comma instead of dot, when typing a value with decimals?
For example the height will be entered as 2 * 3,2 (with comma instead of dot)
New threads created by Minesh and linked to this one are listed below:
Glad to know that solution I shared help you to resolve your original issue reported with this ticket..
As per our support policy, we entertain only one question per ticket. This will help other users searching on the forum as well as help us to write correct problem resolution summery for the original issue reported.
May I kindly ask you to mark resolve this ticket and I'll split the ticket with your new question and follow-up there. Here is the split ticket.
=> https://toolset.com/forums/topic/split-calculate-search-input-field-with-custom-fields-use-comma-for-decimal-instead-of-dot/
My issue is resolved now. Thank you!