Automattic\WooCommerce\Internal\DataStores\Orders

OrdersTableQuery::build_query()privateWC 1.0

Builds the final SQL query to be run.

{} Это метод класса: OrdersTableQuery{}

Хуков нет.

Возвращает

null. Ничего.

Использование

// private - только в коде основоного (родительского) класса
$result = $this->build_query(): void;

Код OrdersTableQuery::build_query() WC 6.9.1

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

	// 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();

	// 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'];

	// DISTINCT.
	$distinct = '';

	// 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( ' ', $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                  = $row_count === -1 ? 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";
}