Calculate sum of meta values

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;

}

so what does get_the_ID actually return? Check with var_dump().

Preface: I don’t know Wordpress. All of this is speculation.

A quick look at wc_get_orders indicates it probably returns objects of type wc_order; meaning they’re not posts. meaning. meaning that get_the_ID() looks oddly at you and says “What do you want me to get an ID from?” Because it’s looking for posts?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.