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?
Minesh
Supporter
Languages:
English (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
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.
Minesh
Supporter
Languages:
English (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.
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;
}
Minesh
Supporter
Languages:
English (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.
Minesh
Supporter
Languages:
English (English )
Timezone:
Asia/Kolkata (GMT+05:30)
I hope you are able to fix the issue.
That resolved t thank you.