Skip Navigation

[Resolved] format as money calculated function on Views

This thread is resolved. Here is a description of the problem and solution.

Problem:

The issue here is that the user wanted to convert the output of they're code to currency format. Instead of the function returning 1000 they wanted $1,000.00

Solution:

This can be done by wrapping your final output from your code with the number_format() function.

Here is a quick sample below.

$num = 1000; //value to be passed

return "$".number_format($num, 2, '.', ',');

// The above will change the format to $1,000.00

For more details on the number_format() function you can check the php documentation.
https://www.php.net/number_format

This support ticket is created 3 years, 1 month 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 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 7 replies, has 2 voices.

Last updated by garyK-5 3 years, 1 month ago.

Assisted by: Shane.

Author
Posts
#2265883

I've used the following code snippet to successfully add up a total number using the custom field 'current-value'. I would like to format the view to show up as money using ( $ )symbol and ( , ) to three places?
---------------------------------------------------------------------------------------------------------------

function calc_func( $atts ){

// get all Posts of your type
$all_posts = get_posts(array(
'current-value' => -1,
'post_type' => 'inventory',
)
);

//if it returns some posts
if( $all_posts ){

//Start the count on 0
$single_posts_value_sum = 0;

//now get the single posts fields values
foreach( $all_posts as $single_post ){

//get each Posts post data
$single_post_data = get_post($single_post);
//get each ID
$single_post_id = $single_post_data->ID;
//get each posts field value
$single_post_value = get_post_meta($single_post_id, 'wpcf-current-value', true);

//we need to sum this up BEFORE the if is closed and BEFORE the foreach is closed
//Sum the values all posts fields
$single_posts_value_sum+= $single_post_value;

}
}
return $single_posts_value_sum; //return summed value
}

add_shortcode( 'calc-total', 'calc_func' );

#2265895

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Gary,

Thank you for getting in touch.

I'm assuming you want your final output to be in currency.

Then you should be able to use the function below on your final output.
hidden link

Here is the practical usage of the function in your code.

function calc_func( $atts ){

// get all Posts of your type
$all_posts = get_posts(array(
'current-value' => -1,
'post_type' => 'inventory',
)
);

//if it returns some posts
if( $all_posts ){

//Start the count on 0
$single_posts_value_sum = 0;

//now get the single posts fields values
foreach( $all_posts as $single_post ){

//get each Posts post data
$single_post_data = get_post($single_post);
//get each ID
$single_post_id = $single_post_data->ID;
//get each posts field value
$single_post_value = get_post_meta($single_post_id, 'wpcf-current-value', true);

//we need to sum this up BEFORE the if is closed and BEFORE the foreach is closed
//Sum the values all posts fields
$single_posts_value_sum+= $single_post_value;

}
}
return money_format("$",$single_posts_value_sum); //return summed value
}

add_shortcode( 'calc-total', 'calc_func' );

Please let me know if this helps.
Thanks,
Shane

#2265939

Thanks, Shane, but this is outputting '$$' and seems to drop the shortcode 'calc-total' currency number completely.

#2265945

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Gary,

I've updated the code a bit, can you test this now and let me know if it works now.

function calc_func( $atts ){
 
// get all Posts of your type
$all_posts = get_posts(array(
'current-value' => -1,
'post_type' => 'inventory',
)
);
 
//if it returns some posts
if( $all_posts ){
 
//Start the count on 0
$single_posts_value_sum = 0;
 
//now get the single posts fields values
foreach( $all_posts as $single_post ){
 
//get each Posts post data
$single_post_data = get_post($single_post);
//get each ID
$single_post_id = $single_post_data->ID;
//get each posts field value
$single_post_value = get_post_meta($single_post_id, 'wpcf-current-value', true);
 
//we need to sum this up BEFORE the if is closed and BEFORE the foreach is closed
//Sum the values all posts fields
$single_posts_value_sum+= $single_post_value;
 
}
}
setlocale(LC_MONETARY, 'en_US.UTF-8');
return money_format('%.2n', $single_posts_value_sum); //return summed value
}
 
add_shortcode( 'calc-total', 'calc_func' );

Thanks,
Shane

#2265969

Hi Shane, sorry but that code gives the same result as before. I noticed that the money_format has been deprecated in PHP 7.4 and it's suggested to use hidden link I'm not sure if that has anything to do with it?

Regards,
Gary

#2266523

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Gary,

I was avoiding trying to build out the formatting, however it would appear that this would've been the simpler choice by just using the number_format() function. This code below should give you exactly what you need.

function calc_func( $atts ){
 
// get all Posts of your type
$all_posts = get_posts(array(
'current-value' => -1,
'post_type' => 'inventory',
)
);
 
//if it returns some posts
if( $all_posts ){
 
//Start the count on 0
$single_posts_value_sum = 0;
 
//now get the single posts fields values
foreach( $all_posts as $single_post ){
 
//get each Posts post data
$single_post_data = get_post($single_post);
//get each ID
$single_post_id = $single_post_data->ID;
//get each posts field value
$single_post_value = get_post_meta($single_post_id, 'wpcf-current-value', true);
 
//we need to sum this up BEFORE the if is closed and BEFORE the foreach is closed
//Sum the values all posts fields
$single_posts_value_sum+= $single_post_value;
 
}
}
return "$".number_format($single_posts_value_sum, 2, '.', ','); //return summed value
}
 
add_shortcode( 'calc-total', 'calc_func' );

For more details on the number_format() function you can see the php document below.
hidden link

Please let me know if this helps.
Thanks,
Shane

#2266619

Hello Shawn... Thanks for the assist, that did it! The issue is resolved

#2266635

My issue is resolved now. Thank you!