Skip Navigation

[Résolu] CALCULATIONS DON'T WORK

Ce fil est résolu. Voici une description du problème et la solution proposée.

Problem:
How to calculate or make sum using custom field.

Solution:
To calculate custom field value and display sum accordingly you need to add custom shortcode to your current theme's functions.php file.

You can find the proposed solution with the following reply;
https://toolset.com/forums/topic/calculations-dont-work/#post-414000

Relevant Documentation:

This support ticket is created Il y a 7 années et 9 mois. 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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 7 réponses, has 2 voix.

Last updated by Jaffa Il y a 7 années et 9 mois.

Assisted by: Minesh.

Auteur
Publications
#413972

Calculations between fields do not work, i have tried almost every combination mentioned in threads with all failed results.

All I want is to calculate a value from a parent field and one from a child theme to show a result in views

Parent Post has Land value
Child Post has House Value
Views shows Parent and Child info but how do i calculate the two fields to show a total?

#413984

Minesh
Supporter

Languages: Anglais (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

You should write a custom shortcode and pass your parent and child field values to the custom shortcode and within custom, shortcode uses the values of parent and child field and perform your desired operation and return the value

You can try following example - you need to make necessary changes as required:
https://toolset.com/forums/topic/calculating-fields/#post-166916

#413989

Hi,

This works but it still strips out any commas, so if my value is 1,650.00 + 100 the total sum shows
1100 and ignore anything past the comma.

#413994

Minesh
Supporter

Languages: Anglais (English )

Timezone: Asia/Kolkata (GMT+05:30)

You can use str_replace() function to convert your string to the number.

For example:

$number = "1,650.00";
$number = str_replace(",","",$number);

I hope this help. if you still not able to how to do it, please share custom shortcode you are using.

#413997

where do i put that code? Here's my shortcode:

[types field='price' id="$house"][/types] + [types field='land-price' id="$house-land"][/types]<br/>
[sum-total val1="[types field='price' id="$house"][/types]" val2="[types field='land-price' id="$house-land"][/types]" val3="[types field='allocation-amount' id="$house-land" output="raw"][/types]"] [/sum-total]

Here's the function.php

add_shortcode('sum-total', 'sum_total_func');
function sum_total_func($atts)
{
extract( shortcode_atts( array(
'val1' => 0,
'val2' => 0,
'val3' => 0
), $atts ) );
$number = $val1 + $val2 + $val3;
$number = str_replace(",","",$number);
return $number;
}

#414000

Minesh
Supporter

Languages: Anglais (English )

Timezone: Asia/Kolkata (GMT+05:30)

Could you please try following code:

add_shortcode('sum-total', 'sum_total_func');
function sum_total_func($atts)
{
extract( shortcode_atts( array(
'val1' => 0,
'val2' => 0,
'val3' => 0
), $atts ) );

$val1 =  str_replace(",","",$val1 ); 
$val2 =  str_replace(",","",$val2 ); 
$val3 =  str_replace(",","",$val3 ); 

$number = $val1 + $val2 + $val3;

return $number;
}

I hope above solution will help you to resolve your issue.

#414180

Minesh
Supporter

Languages: Anglais (English )

Timezone: Asia/Kolkata (GMT+05:30)

I hope you are able to fix the issue.

#415666

That resolved t thank you.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.