I’m trying to get the total count of a value of a custom field for WooCommerce products. My products are stock of shoes, so for each product I create a custom field for the total pairs quantity. Now I need to display in a new page the total count of pairs.
I tried with this code but the result is always 0
<?php
$pair_total = 0; //my main variable
$posts = get_posts(array(
'posts_per_page' => -1, //get all post
'post_type' => 'product' //my custom post type
));
if( $posts ) {
foreach( $posts as $post ){
setup_postdata( $post );
if (get_field('n_totale')) {
echo get_field('n_totale'); //show individual field(not needed)
$pair_total = get_field('n_totale') + $pair_total; //sum all fields
}
}
wp_reset_postdata();
}?>
<?php echo '<h1>'.$pair_total.'</h1>'//showing the total value;
?>
What can I try next?
2
Answers
I solved with this code:
Thanks to all
check get_field() Try below code.