Automattic\WooCommerce\Internal\DataStores\Orders

OrdersTableQuery::build_count_queryprivateWC 1.0

Build SQL query for counting total number of results.

Method of the class: OrdersTableQuery{}

Returns

null. Nothing (null).

Usage

// private - for code of main (parent) class only
$result = $this->build_count_query( $fields, $join, $where, $groupby );
$fields(string) (required)
Prepared fields for SELECT clause.
$join(string) (required)
Prepared JOIN clause.
$where(string) (required)
Prepared WHERE clause.
$groupby(string) (required)
Prepared GROUP BY clause.

OrdersTableQuery::build_count_query() code WC 10.3.3

private function build_count_query( $fields, $join, $where, $groupby ) {
	if ( ! isset( $this->sql ) || '' === $this->sql ) {
		wc_doing_it_wrong( __FUNCTION__, 'Count query can only be build after main query is built.', '7.3.0' );
	}
	$orders_table = $this->tables['orders'];
	$count_fields = "COUNT(DISTINCT $fields)";
	if ( "{$orders_table}.id" === $fields && '' === $join ) {
		// DISTINCT adds performance overhead, exclude the DISTINCT function when confident it is not needed.
		$count_fields = 'COUNT(*)';
	}
	$this->count_sql = "SELECT $count_fields FROM $orders_table $join WHERE $where";

	if ( ! $this->suppress_filters ) {
		/**
		 * Filters the count SQL query.
		 *
		 * @since 8.6.0
		 *
		 * @param string           $sql   The count SQL query.
		 * @param OrdersTableQuery $query The OrdersTableQuery instance (passed by reference).
		 * @param array            $args  Query args.
		 * @param string           $fields Prepared fields for SELECT clause.
		 * @param string           $join Prepared JOIN clause.
		 * @param string           $where Prepared WHERE clause.
		 * @param string           $groupby Prepared GROUP BY clause.
		 */
		$this->count_sql = apply_filters_ref_array( 'woocommerce_orders_table_query_count_sql', array( $this->count_sql, &$this, $this->args, $fields, $join, $where, $groupby ) );
	}
}