Skip Navigation

[Fermé] making calculations with 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.

This topic contains 1 réponse, has 2 voix.

Last updated by Caridad Il y a 9 années et 3 mois.

Assigned support staff: Caridad.

Auteur
Publications
#14190

Is it possible to make a calculation with fields?

What I want to do is this:
I have a field in a custom post type named "price per week"
I want to make a view/template with a table where in one cell this "price per week" field is shown and in the next cell I want to display automatically the value of 20% of "price per week"

If possible, how would I do this?

Thanks

#14250

Dear Jeroen,

It is not possible to do calculations with Views, but you can always create a shortcode to help you achieve it. Try adding these lines to functions.php in your theme:

add_shortcode('wpv-calculate', 'wpv_calculate_shortcode');
function wpv_calculate_shortcode($atts) {
 global $post;
 foreach ($atts as $key => $value) {
  if (substr($value,0,5) == 'wpcf-') {
   $$key = get_post_meta($post->ID, $value, true);
  }
 }
 $out = false;
 eval("\$out = $atts[expression];");
 return $out;
}

Now you can make calculations with custom fields like this:

Total price: [wpv-calculate f1="wpcf-price" expression="$f1 * 1.20"]

Please let me know if there is anything else that I can assist you with.

Regards.
Caridad

Le sujet ‘[Fermé] making calculations with fields’ est fermé à de nouvelles réponses.