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' );
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.
enlace oculto
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
Thanks, Shane, but this is outputting '$$' and seems to drop the shortcode 'calc-total' currency number completely.
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
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 enlace oculto I'm not sure if that has anything to do with it?
Regards,
Gary
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.
enlace oculto
Please let me know if this helps.
Thanks,
Shane
Hello Shawn... Thanks for the assist, that did it! The issue is resolved
My issue is resolved now. Thank you!