Skip to main content
PHP Blog

Back to all posts

How to Get the Latest Order Id In Woocommerce?

Published on
2 min read
How to Get the Latest Order Id In Woocommerce? image

Best WooCommerce Tools to Buy in October 2025

1 The Ultimate WordPress & WooCommerce Handbook: From Setup to Success in Online Selling

The Ultimate WordPress & WooCommerce Handbook: From Setup to Success in Online Selling

BUY & SAVE
$12.90
The Ultimate WordPress & WooCommerce Handbook: From Setup to Success in Online Selling
2 WordPress WooCommerce: Tienda online con WooCommerce (Spanish Edition)

WordPress WooCommerce: Tienda online con WooCommerce (Spanish Edition)

BUY & SAVE
$35.90 $37.90
Save 5%
WordPress WooCommerce: Tienda online con WooCommerce (Spanish Edition)
3 Building E-Commerce Solutions with WooCommerce - Second Edition

Building E-Commerce Solutions with WooCommerce - Second Edition

BUY & SAVE
$38.99
Building E-Commerce Solutions with WooCommerce - Second Edition
4 WordPress WooCommerce: Webshop met WooCommerce (Dutch Edition)

WordPress WooCommerce: Webshop met WooCommerce (Dutch Edition)

BUY & SAVE
$24.99
WordPress WooCommerce: Webshop met WooCommerce (Dutch Edition)
5 Formation Woocommerce: Créer et exploiter une boutique en ligne (French Edition)

Formation Woocommerce: Créer et exploiter une boutique en ligne (French Edition)

BUY & SAVE
$9.99
Formation Woocommerce: Créer et exploiter une boutique en ligne (French Edition)
6 Setting Up and Running an Online Store

Setting Up and Running an Online Store

BUY & SAVE
$2.99
Setting Up and Running an Online Store
+
ONE MORE?

To get the latest order ID in WooCommerce, you can use the following code snippet:

$order = wc_get_orders( array( 'limit' => 1, 'orderby' => 'date', 'order' => 'DESC', ) );

if ( ! empty( $order ) ) { $latest_order_id = $order[0]->get_id(); echo $latest_order_id; }

What is the significance of the latest order id in WooCommerce?

The significance of the latest order ID in WooCommerce is that it acts as a unique identifier for each new order that is placed on the website. It helps the website's admin and customers easily track and manage their orders. The order ID is also used in the backend of the website for administrative purposes, such as processing payments, updating order status, and generating reports. Additionally, the order ID can help in resolving any issues or disputes related to a specific order by providing a reference point for communication and documentation.

How to create a custom endpoint to fetch the latest order id in WooCommerce?

To create a custom endpoint to fetch the latest order id in WooCommerce, you can follow these steps:

  1. Create a custom plugin or add the following code to your theme's functions.php file:

add_action( 'rest_api_init', function () { register_rest_route( 'my-namespace/v1', '/latest-order-id/', array( 'methods' => 'GET', 'callback' => 'get_latest_order_id', ) ); } );

function get_latest_order_id( $request ) { $latest_order = wc_get_orders( array( 'limit' => 1, 'orderby' => 'date', 'order' => 'DESC', ) );

$latest\_order\_id = ! empty( $latest\_order ) ? $latest\_order\[0\]->get\_id() : false;

return rest\_ensure\_response( $latest\_order\_id );

}

  1. Replace 'my-namespace/v1' with your custom namespace and version.
  2. Save your changes and make sure your custom plugin is activated or your theme is active.
  3. You can now make a GET request to https://example.com/wp-json/my-namespace/v1/latest-order-id/ to fetch the latest order id.

After following these steps, you should have a custom endpoint that fetches the latest order id in WooCommerce.

How to get the latest order id using a PHP script in WooCommerce?

You can get the latest order ID in WooCommerce using the following PHP script:

global $wpdb;

// Get the latest order ID $latest_order_id = $wpdb->get_var( "SELECT MAX(ID) FROM {$wpdb->prefix}posts WHERE post_type = 'shop_order'" );

echo "Latest Order ID: " . $latest_order_id;

This script retrieves the latest order ID from the wp_posts table where the post_type is set to shop_order. It then prints out the latest order ID on the screen.