Automattic\WooCommerce\Internal\DataStores\Orders

OrdersTableQuery::get_core_mapping_join()publicWC 7.0.0

Returns an SQL JOIN clause that can be used to join the main orders table with another order table.

Method of the class: OrdersTableQuery{}

No Hooks.

Return

String. The JOIN clause.

Usage

$OrdersTableQuery = new OrdersTableQuery();
$OrdersTableQuery->get_core_mapping_join( $mapping_id ): string;
$mapping_id(string) (required)
The mapping name (e.g. 'orders' or 'operational_data').

Changelog

Since 7.0.0 Introduced.

OrdersTableQuery::get_core_mapping_join() code WC 7.1.1

public function get_core_mapping_join( string $mapping_id ): string {
	global $wpdb;

	if ( 'orders' === $mapping_id ) {
		return '';
	}

	$is_address_mapping = in_array( $mapping_id, array( 'billing_address', 'shipping_address' ), true );

	$alias   = $this->get_core_mapping_alias( $mapping_id );
	$table   = $is_address_mapping ? $this->tables['addresses'] : $this->tables[ $mapping_id ];
	$join    = '';
	$join_on = '';

	$join .= "INNER JOIN `{$table}`" . ( $alias !== $table ? " AS `{$alias}`" : '' );

	if ( isset( $this->mappings[ $mapping_id ]['order_id'] ) ) {
		$join_on .= "`{$this->tables['orders']}`.id = `{$alias}`.order_id";
	}

	if ( $is_address_mapping ) {
		$join_on .= $wpdb->prepare( " AND `{$alias}`.address_type = %s", substr( $mapping_id, 0, -8 ) ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
	}

	return $join . ( $join_on ? " ON ( {$join_on} )" : '' );
}