wc_orders_count()WC 1.0

Return the orders count of a specific order status.

No Hooks.

Return

Int.

Usage

wc_orders_count( $status, $type );
$status(string) (required)
Status.
$type(string)
(Optional) Order type. Leave empty to include all 'for order-count' order types. @{see wc_get_order_types()}.
Default: ''

wc_orders_count() code WC 8.6.1

function wc_orders_count( $status, string $type = '' ) {
	$count           = 0;
	$legacy_statuses = array( 'draft', 'trash' );
	$valid_statuses  = array_merge( array_keys( wc_get_order_statuses() ), $legacy_statuses );
	$status          = ( ! in_array( $status, $legacy_statuses, true ) && 0 !== strpos( $status, 'wc-' ) ) ? 'wc-' . $status : $status;
	$valid_types     = wc_get_order_types( 'order-count' );
	$type            = trim( $type );

	if ( ! in_array( $status, $valid_statuses, true ) || ( $type && ! in_array( $type, $valid_types, true ) ) ) {
		return 0;
	}

	$cache_key    = WC_Cache_Helper::get_cache_prefix( 'orders' ) . $status . $type;
	$cached_count = wp_cache_get( $cache_key, 'counts' );

	if ( false !== $cached_count ) {
		return $cached_count;
	}

	$types_for_count = $type ? array( $type ) : $valid_types;

	foreach ( $types_for_count as $type ) {
		$data_store = WC_Data_Store::load( 'shop_order' === $type ? 'order' : $type );
		if ( $data_store ) {
			$count += $data_store->get_order_count( $status );
		}
	}

	wp_cache_set( $cache_key, $count, 'counts' );

	return $count;
}