[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
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
}
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' );
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' );
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?
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