Automattic\WooCommerce\Utilities

OrderUtil::get_count_for_typepublic staticWC 8.7.0

Counts number of orders of a given type.

Method of the class: OrderUtil{}

No Hooks.

Returns

Array. Array of order counts indexed by order type.

Usage

$result = OrderUtil::get_count_for_type( $order_type );
$order_type(string) (required)
Order type.

Changelog

Since 8.7.0 Introduced.

OrderUtil::get_count_for_type() code WC 10.4.3

public static function get_count_for_type( $order_type ) {
	global $wpdb;

	$order_type = (string) $order_type;

	$order_count_cache = new OrderCountCache();
	$count_per_status  = $order_count_cache->get( $order_type );

	if ( null === $count_per_status ) {
		if ( self::custom_orders_table_usage_is_enabled() ) {
			// phpcs:disable WordPress.DB.PreparedSQL.NotPrepared
			$results = $wpdb->get_results(
				$wpdb->prepare(
					'SELECT `status`, COUNT(*) AS `count` FROM ' . self::get_table_for_orders() . ' WHERE `type` = %s GROUP BY `status`',
					$order_type
				),
				ARRAY_A
			);
			// phpcs:enable

			$count_per_status = array_map( 'absint', array_column( $results, 'count', 'status' ) );

		} else {
			$count_per_status = (array) wp_count_posts( $order_type );
		}

		// Make sure all order statuses are included just in case.
		$count_per_status = array_merge(
			array_fill_keys( array_merge( array_keys( wc_get_order_statuses() ), array( OrderStatus::TRASH ) ), 0 ),
			$count_per_status
		);

		$order_count_cache->set_multiple( $order_type, $count_per_status );
	}

	return $count_per_status;
}