Skip Navigation

[Resolved] Display Calculated Fields

This support ticket is created 5 years, 7 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 3 replies, has 2 voices.

Last updated by Waqar 5 years, 7 months ago.

Assisted by: Waqar.

Author
Posts
#1270441

Hi Waqar,

Thank you very much for your assistance on Friday, when you helped me filter the results, based on a comparison of a min and max value.

I would need your assistance once again, in order to display a calculated field in the results.

Just to remind you, here is the solution you provided on Friday:

1. You'll first add the two search filters for both your custom fields ( min and max ) as usual, but then adjust their query filters manually, as shown in this screenshot:
hidden link

Note: this setup assumes that expected values entered from visitors can range from 0 to 99999, but feel free to adjust as needed.

2. Next step would be to hide the front-end Max field so that there is just one input field available on the front-end.

You can add a special class "hidden-field" to this field's wrapper and then include custom CSS code, that hides it:
hidden link
.hidden-field {
display: none;
}

3. The last step would be to include some custom script, to make sure that Max field value is also filled automatically when the value is entered in the Min field:
hidden link

jQuery("input#wpv_control_textfield_wpv-wpcf-book-min").change(function() {
var newVal = jQuery("input#wpv_control_textfield_wpv-wpcf-book-min").val();
jQuery("input#wpv_control_textfield_wpv-wpcf-book-max").val(newVal);
});

So, at this point, I need to add a field in the results view, which would be: BookMin*Rate (where book min is the user input and rate is another custom field, which is a constant).

Thank you,

Kostas

#1271259

Hi Kostas,

Thank you for contacting us and I'll be happy to assist.

I'm afraid, from your message, the specification/requirement for a new search filter field is not clear.

Can you please share more specific details around what is missing and a link to the page where this view can be seen?

It would also help if you could share the website's admin login details so that I can see how the view is set up.

Note: Your next reply will be private and though no changes will be made on your website, please make a complete backup copy, before sharing the access details.

regards,
Waqar

#1271403

Hi Waqar,

Thanks for your reply.

I do not need to add another filter. I need a calculated field in the search results.

This field should make simple calculations, like BookMin*Rate (where BookMin is the user input and rate is a custom field, which is a constant).

Thank you,

Kostas

#1271719

Hi Kostas,

If your goal is to have a new input field in the view's search form, whose value is automatically filled based on the user's input for the BookMin field, you can:

1. Include the HTML for this field in the view's "Search and Pagination" section:


<div class="form-group">
	<label>[wpml-string context="wpv-views"]Rate[/wpml-string]</label>
	<input type="text" id="wpv_control_textfield_wpv-rate" name="wpv-rate" value="" class="js-wpv-filter-trigger-delayed form-control">
</div>

2. Update the custom script from the last ticket, to include the code to calculate and update the value of this new manually entered field:

Example:


// execute when the value of book min field changes
jQuery("input#wpv_control_textfield_wpv-wpcf-book-min").change(function() {
  // store value of book min field in a variable
  var newVal = jQuery("input#wpv_control_textfield_wpv-wpcf-book-min").val();
  
  // the value to calculate the rate field
  var newRate = 100;
  
  // fill the value in the book max field, same as the book min field
  jQuery("input#wpv_control_textfield_wpv-wpcf-book-max").val(newVal);
  
  // fill the value in the rate field multiplying the value of book min field with the rate
  jQuery("input#wpv_control_textfield_wpv-rate").val(newVal*newRate);
});

Note: please make sure to update the IDs of the fields in the script to match the ones used on your website.

I hope this helps.

regards,
Waqar