Automattic\WooCommerce\Admin\API\Reports\Variations\Stats

DataStore::update_sql_query_params │ protected │ WC 1.0

Updates the database query with parameters used for Products Stats report: categories and order status.

Method of the class: DataStore{}

No Hooks.

Returns

null. Nothing (null).

Usage

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

DataStore::update_sql_query_params() code WC 11.2.2

protected function update_sql_query_params( $query_args ) {
	global $wpdb;

	$products_where_clause      = '';
	$products_from_clause       = '';
	$where_subquery             = array();
	$order_product_lookup_table = self::get_db_table_name();
	$order_item_meta_table      = $wpdb->prefix . 'woocommerce_order_itemmeta';

	$included_products = $this->get_included_products( $query_args );
	if ( $included_products ) {
		$products_where_clause .= " AND {$order_product_lookup_table}.product_id IN ({$included_products})";
	}

	$excluded_products = $this->get_excluded_products( $query_args );
	if ( $excluded_products ) {
		$products_where_clause .= "AND {$order_product_lookup_table}.product_id NOT IN ({$excluded_products})";
	}

	$included_variations = $this->get_included_variations( $query_args );
	if ( $included_variations ) {
		$products_where_clause .= " AND {$order_product_lookup_table}.variation_id IN ({$included_variations})";
	} elseif ( $this->should_exclude_simple_products( $query_args ) ) {
		$products_where_clause .= " AND {$order_product_lookup_table}.variation_id != 0";
	}

	$order_status_filter = $this->get_status_subquery( $query_args );
	if ( $order_status_filter ) {
		$products_from_clause  .= " JOIN {$wpdb->prefix}wc_order_stats ON {$order_product_lookup_table}.order_id = {$wpdb->prefix}wc_order_stats.order_id";
		$products_where_clause .= " AND ( {$order_status_filter} )";
	}

	$attribute_order_items_subquery = $this->get_order_item_by_attribute_subquery( $query_args );
	if ( $attribute_order_items_subquery ) {
		// JOIN on product lookup if we haven't already.
		if ( ! $order_status_filter ) {
			$products_from_clause .= " JOIN {$wpdb->prefix}wc_order_stats ON {$order_product_lookup_table}.order_id = {$wpdb->prefix}wc_order_stats.order_id";
		}

		// Add subquery for matching attributes to WHERE.
		$products_where_clause .= $attribute_order_items_subquery;
	}

	if ( 0 < count( $where_subquery ) ) {
		$operator               = $this->get_match_operator( $query_args );
		$products_where_clause .= 'AND (' . implode( " {$operator} ", $where_subquery ) . ')';
	}

	$this->add_time_period_sql_params( $query_args, $order_product_lookup_table );
	$this->total_query->add_sql_clause( 'where', $products_where_clause );
	$this->total_query->add_sql_clause( 'join', $products_from_clause );

	$this->add_intervals_sql_params( $query_args, $order_product_lookup_table );
	$this->interval_query->add_sql_clause( 'where', $products_where_clause );
	$this->interval_query->add_sql_clause( 'join', $products_from_clause );
	$this->interval_query->add_sql_clause( 'select', $this->get_sql_clause( 'select' ) . ' AS time_interval' );
}