Automattic\WooCommerce\Internal\DataStores\Orders

DataSynchronizer::get_current_orders_pending_sync_count()publicWC 1.0

Calculate how many orders need to be synchronized currently. A database query is performed to get how many orders match one of the following:

  • Existing in the authoritative table but not in the backup table.
  • Existing in both tables, but they have a different update date.

Метод класса: DataSynchronizer{}

Хуков нет.

Возвращает

null. Ничего.

Использование

$DataSynchronizer = new DataSynchronizer();
$DataSynchronizer->get_current_orders_pending_sync_count( $use_cache ): int;
$use_cache(true|false)
Whether to use the cached value instead of fetching from database.
По умолчанию: false

Код DataSynchronizer::get_current_orders_pending_sync_count() WC 7.3.0

public function get_current_orders_pending_sync_count( $use_cache = false ): int {
	global $wpdb;

	if ( $use_cache ) {
		$pending_count = wp_cache_get( 'woocommerce_hpos_pending_sync_count' );
		if ( false !== $pending_count ) {
			return (int) $pending_count;
		}
	}
	$orders_table     = $this->data_store::get_orders_table_name();
	$order_post_types = wc_get_order_types( 'cot-migration' );

	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;
	}

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

	if ( $this->custom_orders_table_is_authoritative() ) {
		$missing_orders_count_sql = "
SELECT COUNT(1) FROM $wpdb->posts posts
INNER JOIN $orders_table orders ON posts.id=orders.id
WHERE posts.post_type = '" . self::PLACEHOLDER_ORDER_POST_TYPE . "'
 AND orders.status not in ( 'auto-draft' )
";
		$operator                 = '>';
	} else {
		// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare -- $order_post_type_placeholder is prepared.
		$missing_orders_count_sql = $wpdb->prepare(
			"
SELECT COUNT(1) 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",
			$order_post_types
		);
		// phpcs:enable
		$operator = '<';
	}

	// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare -- $missing_orders_count_sql is prepared.
	$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 );
	wp_cache_set( 'woocommerce_hpos_pending_sync_count', $pending_count );
	return $pending_count;
}