Automattic\WooCommerce\Internal\Admin\Schedulers
OrdersScheduler::get_items_from_orders_table
Helper method to ger order/refund IDS and total count that needs to be synced from HPOS.
Method of the class: OrdersScheduler{}
No Hooks.
Returns
Object. Total counts.
Usage
$result = OrdersScheduler::get_items_from_orders_table( $limit, $page, $days, $skip_existing );
- $limit(int) (required)
- Number of records to retrieve.
- $page(int) (required)
- Page number.
- $days(int|true|false) (required)
- Number of days prior to current date to limit search results.
- $skip_existing(true|false) (required)
- Skip already imported orders.
OrdersScheduler::get_items_from_orders_table() OrdersScheduler::get items from orders table code WC 10.6.2
private static function get_items_from_orders_table( $limit, $page, $days, $skip_existing ) {
global $wpdb;
$where_clause = '';
$offset = $page > 1 ? ( $page - 1 ) * $limit : 0;
$order_table = OrdersTableDataStore::get_orders_table_name();
if ( is_int( $days ) ) {
$days_ago = gmdate( 'Y-m-d 00:00:00', time() - ( DAY_IN_SECONDS * $days ) );
$where_clause .= " AND orders.date_created_gmt >= '{$days_ago}'";
}
if ( $skip_existing ) {
$where_clause .= "AND NOT EXiSTS (
SELECT 1 FROM {$wpdb->prefix}wc_order_stats
WHERE {$wpdb->prefix}wc_order_stats.order_id = orders.id
)
";
}
$count = $wpdb->get_var(
"
SELECT COUNT(*) FROM {$order_table} AS orders
WHERE type in ( 'shop_order', 'shop_order_refund' )
AND status NOT IN ( 'wc-auto-draft', 'trash', 'auto-draft' )
{$where_clause}
"
); // phpcs:ignore unprepared SQL ok.
$order_ids = absint( $count ) > 0 ? $wpdb->get_col(
$wpdb->prepare(
"SELECT id FROM {$order_table} AS orders
WHERE type IN ( 'shop_order', 'shop_order_refund' )
AND status NOT IN ( 'wc-auto-draft', 'auto-draft', 'trash' )
{$where_clause}
ORDER BY date_created_gmt ASC
LIMIT %d
OFFSET %d",
$limit,
$offset
)
) : array(); // phpcs:ignore unprepared SQL ok.
return (object) array(
'total' => absint( $count ),
'ids' => $order_ids,
);
}