Automattic\WooCommerce\Internal\Admin\Schedulers

OrdersScheduler::get_orders_since_from_orders_tableprivate staticWC 1.0

Get orders from HPOS orders table updated since the specified cursor position.

Query logic uses a compound cursor (date, ID) to handle pagination when multiple orders share the same timestamp:

  • WHERE date > cursor_date: Get orders with newer timestamps
  • OR (date = cursor_date AND id > cursor_id): Continue processing same timestamp

Example: With batch_size=100 and 1000 orders at '2024-01-01 10:00:00', this processes them across 10 batches without infinite loops or duplicates.

Method of the class: OrdersScheduler{}

No Hooks.

Returns

Array. Array of objects with 'id' and 'date_updated_gmt' properties.

Usage

$result = OrdersScheduler::get_orders_since_from_orders_table( $cursor_date, $cursor_id, $limit );
$cursor_date(string) (required)
Cursor date in 'Y-m-d H:i:s' format.
$cursor_id(int) (required)
Cursor order ID.
$limit(int) (required)
Number of orders to retrieve.

OrdersScheduler::get_orders_since_from_orders_table() code WC 10.8.1

private static function get_orders_since_from_orders_table( $cursor_date, $cursor_id, $limit ) {
	global $wpdb;
	$orders_table = OrdersTableDataStore::get_orders_table_name();

	// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
	return $wpdb->get_results(
		$wpdb->prepare(
			"SELECT id, date_updated_gmt
			FROM {$orders_table}
			WHERE type IN ('shop_order', 'shop_order_refund')
			AND status NOT IN ('wc-auto-draft', 'auto-draft', 'trash')
			AND (
				date_updated_gmt > %s
				OR (date_updated_gmt = %s AND id > %d)
			)
			ORDER BY date_updated_gmt ASC, id ASC
			LIMIT %d",
			$cursor_date,
			$cursor_date,
			$cursor_id,
			$limit
		)
	);
	// phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
}