Automattic\WooCommerce\Internal\DataStores\Orders

DataSynchronizer::query_orders_pending_sync_countprivateWC 1.0

Query the number of orders pending synchronization.

Method of the class: DataSynchronizer{}

No Hooks.

Returns

Int. The number of orders pending synchronization.

Usage

// private - for code of main (parent) class only
$result = $this->query_orders_pending_sync_count( $full_count );
$full_count(true|false)
Whether to return the full count or a single row.
Default: true

DataSynchronizer::query_orders_pending_sync_count() code WC 10.3.6

private function query_orders_pending_sync_count( $full_count = true ) {
	global $wpdb;

	$order_post_types = wc_get_order_types( 'cot-migration' );

	$order_post_type_placeholder = implode( ', ', array_fill( 0, count( $order_post_types ), '%s' ) );

	$orders_table = $this->data_store::get_orders_table_name();

	$count_clause = $full_count ? 'COUNT(1)' : '1';

	$limit_clause = $full_count ? '' : 'LIMIT 1';

	if ( empty( $order_post_types ) ) {
		$this->error_logger->debug(
			sprintf(
				/* translators: 1: method name. */
				esc_html__( '%1$s was called but no order types were registered: it may have been called too early.', 'woocommerce' ),
				__METHOD__
			)
		);

		return 0;
	}

	// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare,WordPress.DB.PreparedSQL.NotPrepared --
	// -- $order_post_type_placeholder, $orders_table, self::PLACEHOLDER_ORDER_POST_TYPE are all safe to use in queries.
	if ( ! $this->get_table_exists() ) {
		$count = $wpdb->get_var(
			$wpdb->prepare(
				"SELECT $count_clause FROM $wpdb->posts where post_type in ( $order_post_type_placeholder )",
				$order_post_types
			)
		);
		return $count;
	}

	if ( $this->custom_orders_table_is_authoritative() ) {
		$missing_orders_count_sql = $wpdb->prepare(
			"
SELECT $count_clause FROM $wpdb->posts posts
RIGHT JOIN $orders_table orders ON posts.ID=orders.id
WHERE (posts.post_type IS NULL OR posts.post_type = '" . self::PLACEHOLDER_ORDER_POST_TYPE . "')
 AND orders.status NOT IN ( 'auto-draft' )
 AND orders.type IN ($order_post_type_placeholder)
$limit_clause",
			$order_post_types
		);
		$operator                 = '>';
	} else {
		$missing_orders_count_sql = $wpdb->prepare(
			"
SELECT $count_clause FROM $wpdb->posts posts
LEFT JOIN $orders_table orders ON posts.ID=orders.id
WHERE
  posts.post_type in ($order_post_type_placeholder)
  AND posts.post_status != 'auto-draft'
  AND orders.id IS NULL
$limit_clause",
			$order_post_types
		);

		$operator = '<';
	}

	$sql = $wpdb->prepare(
		"
SELECT(
($missing_orders_count_sql)
+
(SELECT COUNT(1) FROM (
	SELECT orders.id FROM $orders_table orders
	JOIN $wpdb->posts posts on posts.ID = orders.id
	WHERE
	  posts.post_type IN ($order_post_type_placeholder)
	  AND orders.date_updated_gmt $operator posts.post_modified_gmt
) x)
) count",
		$order_post_types
	);
	// phpcs:enable

	// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
	$pending_count = (int) $wpdb->get_var( $sql );

	$deleted_from_table = $this->get_current_deletion_record_meta_value();

	$deleted_count  = $wpdb->get_var(
		$wpdb->prepare(
			"SELECT $count_clause FROM {$wpdb->prefix}wc_orders_meta WHERE meta_key=%s AND meta_value=%s",
			array( self::DELETED_RECORD_META_KEY, $deleted_from_table )
		)
	);
	$pending_count += $deleted_count;

	return $pending_count;
}