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. Ничего (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 8.7.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;
		}
	}

	$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();

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

	if ( ! $this->get_table_exists() ) {
		$count = $wpdb->get_var(
			// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare -- $order_post_type_placeholder is prepared.
			$wpdb->prepare(
				"SELECT COUNT(*) FROM $wpdb->posts where post_type in ( $order_post_type_placeholder )",
				$order_post_types
			)
			// phpcs:enable
		);
		return $count;
	}

	if ( $this->custom_orders_table_is_authoritative() ) {
		$missing_orders_count_sql = $wpdb->prepare(
			"
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' )
 AND orders.type IN ($order_post_type_placeholder)",
			$order_post_types
		);
		$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 );

	$deleted_from_table = $this->get_current_deletion_record_meta_value();

	$deleted_count  = $wpdb->get_var(
		$wpdb->prepare(
			"SELECT count(1) 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;

	wp_cache_set( 'woocommerce_hpos_pending_sync_count', $pending_count );
	return $pending_count;
}