Automattic\WooCommerce\Internal\Admin\Orders

ListTable::count_orders_by_status()privateWC 1.0

Count orders by status.

Method of the class: ListTable{}

Return

Int.

Usage

// private - for code of main (parent) class only
$result = $this->count_orders_by_status( $status ): int;
$status(string|string[]) (required)
The order status we are interested in.

ListTable::count_orders_by_status() code WC 9.0.1

private function count_orders_by_status( $status ): int {
	global $wpdb;

	// Compute all counts and cache if necessary.
	if ( is_null( $this->status_count_cache ) ) {
		$orders_table = OrdersTableDataStore::get_orders_table_name();

		$res = $wpdb->get_results(
			$wpdb->prepare(
				"SELECT status, COUNT(*) AS cnt FROM {$orders_table} WHERE type = %s GROUP BY status", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
				$this->order_type
			),
			ARRAY_A
		);

		$this->status_count_cache =
			$res
			? array_combine( array_column( $res, 'status' ), array_map( 'absint', array_column( $res, 'cnt' ) ) )
			: array();
	}

	$status = (array) $status;
	$count  = array_sum( array_intersect_key( $this->status_count_cache, array_flip( $status ) ) );

	/**
	 * Allows 3rd parties to modify the count of orders by status.
	 *
	 * @param int      $count  Number of orders for the given status.
	 * @param string[] $status List of order statuses in the count.
	 * @since 7.3.0
	 */
	return apply_filters(
		'woocommerce_' . $this->order_type . '_list_table_order_count',
		$count,
		$status
	);
}