Automattic\WooCommerce\Internal\DataStores\Orders

OrdersTableQuery::join()privateWC 1.0

JOINs the main orders table with another table.

Method of the class: OrdersTableQuery{}

No Hooks.

Return

null. Nothing.

Usage

// private - for code of main (parent) class only
$result = $this->join( $table, $alias, $on, $join_type, $alias_once );
$table(string) (required)
Table name (including prefix).
$alias(string)
Table alias to use.
Default: $table
$on(string)
ON clause.
Default: "wc_orders.id = {$alias}.order_id"
$join_type(string)
JOIN type: LEFT, RIGHT or INNER.
Default: 'inner'
$alias_once(true|false)
If TRUE, table won't be JOIN'ed again if already JOIN'ed.
Default: false

OrdersTableQuery::join() code WC 7.1.1

private function join( string $table, string $alias = '', string $on = '', string $join_type = 'inner', bool $alias_once = false ) {
	$alias     = empty( $alias ) ? $table : $alias;
	$join_type = strtoupper( trim( $join_type ) );

	if ( $this->tables['orders'] === $alias ) {
		// translators: %s is a table name.
		throw new \Exception( sprintf( __( '%s can not be used as a table alias in OrdersTableQuery', 'woocommerce' ), $alias ) );
	}

	if ( empty( $on ) ) {
		if ( $this->tables['orders'] === $table ) {
			$on = "{$this->tables['orders']}.id = {$alias}.id";
		} else {
			$on = "{$this->tables['orders']}.id = {$alias}.order_id";
		}
	}

	if ( isset( $this->join[ $alias ] ) ) {
		if ( ! $alias_once ) {
			// translators: %s is a table name.
			throw new \Exception( sprintf( __( 'Can not re-use table alias "%s" in OrdersTableQuery.', 'woocommerce' ), $alias ) );
		}

		return;
	}

	if ( '' === $join_type || ! in_array( $join_type, array( 'LEFT', 'RIGHT', 'INNER' ), true ) ) {
		$join_type = 'INNER';
	}

	$sql_join  = '';
	$sql_join .= "{$join_type} JOIN {$table} ";
	$sql_join .= ( $alias !== $table ) ? "AS {$alias} " : '';
	$sql_join .= "ON ( {$on} )";

	$this->join[ $alias ] = $sql_join;
}