I want to calculate the sum of all meta values with the key _alg_wc_cog_order_profit only if order status is set to “completed”. I have created a shortcode for this but it does not return correct value and returns 0 instead. But if i replace the get_the_ID() with a specific post ID such as 56 or 11 it does return correct value.
Please guide me where i am making a mistake.
add_shortcode('user_on_hold_cogs', 'get_user_orders_on_hold_totalb');
function get_user_orders_on_hold_totalb() {
$total_amount = 0; // Initializing
// Get current user
if( $user = wp_get_current_user() ){
// Get 'on-hold' customer ORDERS
$on_hold_orders = wc_get_orders( array(
'limit' => -1,
//'customer_id' => $user->ID,
'status' => 'completed',
) );
foreach( $on_hold_orders as $order) {
$stockk = (float) get_post_meta( get_the_ID() , '_alg_wc_cog_order_profit', true );
//$total_amount += $order->get_total();
$total_amount += $stockk ;
}
}
return $total_amount;
}