Skip to content

Instantly share code, notes, and snippets.

@webtoffee-git
Last active March 6, 2025 04:33
Show Gist options
  • Save webtoffee-git/840271b839ed82f64dbb0195126b26f6 to your computer and use it in GitHub Desktop.
Save webtoffee-git/840271b839ed82f64dbb0195126b26f6 to your computer and use it in GitHub Desktop.
Retrieve Order ID Using _order_number Metadata via WooCommerce REST API - By WebToffee (Sequential Order Number for WooCommerce)
<?php //Do not copy this line of code
/*You can fetch the Order ID based on the _order_number metadata using WooCommerce's REST API. To do this, use the following API endpoint:
wp-json/wc/v3/orders?meta_key=_order_number&meta_value=123
Simply replace 123 with the desired order number to retrieve the corresponding Order ID.*/
add_filter( 'woocommerce_rest_shop_order_object_query', function( $args, $request ) {
if (! empty($request['meta_key'] ) &&! empty( $request['meta_value'] ) ) {
$args ['meta_query'] = array(
array (
'key' => sanitize_text_field( $request ['meta_key'] ),
'value' => sanitize_text_field( $request ['meta_value'] ),
'compare' => '='
),
);
return $args;
}
}, 10, 2);
@webtoffee-git
Copy link
Author

You can fetch the Order ID based on the _order_number metadata using WooCommerce's REST API. To do this, use the following API endpoint:
wp-json/wc/v3/orders?meta_key=_order_number&meta_value=123
Simply replace 123 with the desired order number to retrieve the corresponding Order ID.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment