Automattic\WooCommerce\Internal\DataStores\Orders

OrdersTableQuery::build_query()privateWC 1.0

Builds the final SQL query to be run.

{} It's a method of the class: OrdersTableQuery{}

No Hooks.

Return

null. Nothing.

Usage

// private - for code of main (parent) class only
$result = $this->build_query(): void;

Code of OrdersTableQuery::build_query() WC 7.1.0

private function build_query(): void {
	$this->maybe_remap_args();

	// Field queries.
	if ( ! empty( $this->args['field_query'] ) ) {
		$this->field_query = new OrdersTableFieldQuery( $this );
		$sql               = $this->field_query->get_sql_clauses();
		$this->join        = $sql['join'] ? array_merge( $this->join, $sql['join'] ) : $this->join;
		$this->where       = $sql['where'] ? array_merge( $this->where, $sql['where'] ) : $this->where;
	}

	// Build query.
	$this->process_date_args();
	$this->process_orders_table_query_args();
	$this->process_operational_data_table_query_args();
	$this->process_addresses_table_query_args();

	// Search queries.
	if ( ! empty( $this->args['s'] ) ) {
		$this->search_query = new OrdersTableSearchQuery( $this );
		$sql                = $this->search_query->get_sql_clauses();
		$this->join         = $sql['join'] ? array_merge( $this->join, $sql['join'] ) : $this->join;
		$this->where        = $sql['where'] ? array_merge( $this->where, $sql['where'] ) : $this->where;
	}

	// Meta queries.
	if ( ! empty( $this->args['meta_query'] ) ) {
		$this->meta_query = new OrdersTableMetaQuery( $this );

		$sql = $this->meta_query->get_sql_clauses();

		$this->join  = $sql['join'] ? array_merge( $this->join, $sql['join'] ) : $this->join;
		$this->where = $sql['where'] ? array_merge( $this->where, array( $sql['where'] ) ) : $this->where;

		if ( $sql['join'] ) {
			$this->groupby[] = "{$this->tables['orders']}.id";
		}
	}

	// Date queries.
	if ( ! empty( $this->args['date_query'] ) ) {
		$this->date_query = new \WP_Date_Query( $this->args['date_query'], "{$this->tables['orders']}.date_created_gmt" );
		$this->where[]    = substr( trim( $this->date_query->get_sql() ), 3 ); // WP_Date_Query includes "AND".
	}

	$this->process_orderby();
	$this->process_limit();

	$orders_table = $this->tables['orders'];

	// SELECT [fields].
	$this->fields = "{$orders_table}.id";
	$fields       = $this->fields;

	// SQL_CALC_FOUND_ROWS.
	if ( ( ! $this->arg_isset( 'no_found_rows' ) || ! $this->args['no_found_rows'] ) && $this->limits ) {
		$found_rows = 'SQL_CALC_FOUND_ROWS';
	} else {
		$found_rows = '';
	}

	// JOIN.
	$join = implode( ' ', array_unique( array_filter( array_map( 'trim', $this->join ) ) ) );

	// WHERE.
	$where = '1=1';
	foreach ( $this->where as $_where ) {
		$where .= " AND ({$_where})";
	}

	// ORDER BY.
	$orderby = $this->orderby ? ( 'ORDER BY ' . implode( ', ', $this->orderby ) ) : '';

	// LIMITS.
	$limits = '';

	if ( ! empty( $this->limits ) && count( $this->limits ) === 2 ) {
		list( $offset, $row_count ) = $this->limits;
		$row_count                  = -1 === $row_count ? self::MYSQL_MAX_UNSIGNED_BIGINT : (int) $row_count;
		$limits                     = 'LIMIT ' . (int) $offset . ', ' . $row_count;
	}

	// GROUP BY.
	$groupby = $this->groupby ? 'GROUP BY ' . implode( ', ', (array) $this->groupby ) : '';

	$this->sql = "SELECT $found_rows DISTINCT $fields FROM $orders_table $join WHERE $where $groupby $orderby $limits";
}