Automattic\WooCommerce\Internal\DataStores\Orders

OrdersTableStatusUnionQuery::is_enabledprivateWC 1.0

Returns whether the rewrite should be used for the given types and statuses.

Enabled by default once the matching order count reaches MIN_ORDER_COUNT. Counts come from OrderUtil::get_count_for_type() — the same facade the order admin list screen uses for its status counts — which reads the order count cache and computes (and caches) the counts on a miss.

Method of the class: OrdersTableStatusUnionQuery{}

Returns

true|false. Whether the rewrite should be used.

Usage

// private - for code of main (parent) class only
$result = $this->is_enabled( $types, $statuses, $suppress_filters ): bool;
$types(string[]) (required)
Queried order types.
$statuses(string[]) (required)
Queried order statuses.
$suppress_filters(true|false) (required)
Whether the query is running with filters suppressed.

OrdersTableStatusUnionQuery::is_enabled() code WC 11.0.1

private function is_enabled( array $types, array $statuses, bool $suppress_filters ): bool {
	$orders_count = 0;

	foreach ( $types as $type ) {
		$counts = OrderUtil::get_count_for_type( $type );

		foreach ( $statuses as $status ) {
			$orders_count += $counts[ $status ] ?? 0;
		}
	}

	$enabled = $orders_count >= self::MIN_ORDER_COUNT;

	if ( $suppress_filters ) {
		return $enabled;
	}

	/**
	 * Filters whether a query for multiple order statuses ordered by creation date may be rewritten as a
	 * UNION ALL of single-status queries for performance. The rewrite produces the same results and, even
	 * when enabled here, only applies to queries generated purely from the 'type' and 'status' query args
	 * (no search, meta or field filters), such as the default order admin list screen query.
	 *
	 * Hosts that know their database benefits from the rewrite regardless of store size (or that don't want
	 * to depend on the order count cache being warm) can force-enable it with
	 * add_filter( 'woocommerce_orders_table_query_status_union_optimization', '__return_true' ); the
	 * structural eligibility checks above still apply.
	 *
	 * @param bool             $enabled Whether the rewrite is enabled. Defaults to TRUE only when the cached
	 *                                  number of orders matching the queried types and statuses is at least
	 *                                  500,000; FALSE otherwise (including when the order count cache is cold).
	 * @param OrdersTableQuery $query   The OrdersTableQuery instance.
	 *
	 * @since 11.0.0
	 */
	return (bool) apply_filters( 'woocommerce_orders_table_query_status_union_optimization', $enabled, $this->query );
}