How to get woocommerce last order amount?

Member

by lottie , in category: PHP CMS , 4 months ago

How to get woocommerce last order amount?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by dedrick , 4 months ago

@lottie 

To get the last order amount in WooCommerce, you can use the following code snippet in your functions.php file or in a custom plugin:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
function get_last_order_amount() {
    $args = array(
        'numberposts' => 1,
        'post_type'   => 'shop_order',
        'post_status' => 'wc-completed', // Change this to 'wc-processing' if you want to include processing orders as well
    );

    $orders = wc_get_orders( $args );

    if ( !empty( $orders ) ) {
        foreach ( $orders as $order ) {
            $order_amount = $order->get_total();
            return $order_amount;
        }
    }

    return false; // Return false if no orders are found
}


You can then call the get_last_order_amount() function anywhere in your theme or plugin to retrieve the last order amount.


Please note that this code will only return the total amount for completed orders by default. If you want to include processing orders as well, you can change the post_status parameter in the $args array to 'wc-processing'.