Automattic\WooCommerce\Admin\API\Reports\Variations

DataStore::get_order_item_by_attribute_subquery()protectedWC 1.0

Generate a subquery for order_item_id based on the attribute filters.

Method of the class: DataStore{}

No Hooks.

Return

String.

Usage

// protected - for code of main (parent) or child class
$result = $this->get_order_item_by_attribute_subquery( $query_args );
$query_args(array) (required)
Query arguments supplied by the user.

DataStore::get_order_item_by_attribute_subquery() code WC 9.3.3

protected function get_order_item_by_attribute_subquery( $query_args ) {
	$order_product_lookup_table = self::get_db_table_name();
	$attribute_subqueries       = $this->get_attribute_subqueries( $query_args );

	if ( $attribute_subqueries['join'] && $attribute_subqueries['where'] ) {
		// Perform a subquery for DISTINCT order items that match our attribute filters.
		$attr_subquery = new SqlQuery( $this->context . '_attribute_subquery' );
		$attr_subquery->add_sql_clause( 'select', "DISTINCT {$order_product_lookup_table}.order_item_id" );
		$attr_subquery->add_sql_clause( 'from', $order_product_lookup_table );

		if ( $this->should_exclude_simple_products( $query_args ) ) {
			$attr_subquery->add_sql_clause( 'where', "AND {$order_product_lookup_table}.variation_id != 0" );
		}

		foreach ( $attribute_subqueries['join'] as $attribute_join ) {
			$attr_subquery->add_sql_clause( 'join', $attribute_join );
		}

		$operator = $this->get_match_operator( $query_args );
		$attr_subquery->add_sql_clause( 'where', 'AND (' . implode( " {$operator} ", $attribute_subqueries['where'] ) . ')' );

		return "AND {$order_product_lookup_table}.order_item_id IN ({$attr_subquery->get_query_statement()})";
	}

	return false;
}