Automattic\WooCommerce\Admin\API\Reports\Coupons\Stats

Segmenter::get_segments()protectedWC 1.0

Return array of segments formatted for REST response.

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

Хуков нет.

Возвращает

Массив.

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

// protected - в коде основоного (родительского) или дочернего класса
$result = $this->get_segments( $type, $query_params, $table_name );
$type(строка) (обязательный)
Type of segments to return--'totals' or 'intervals'.
$query_params(массив) (обязательный)
SQL query parameter array.
$table_name(строка) (обязательный)
Name of main SQL table for the data store (used as basis for JOINS).

Код Segmenter::get_segments() WC 6.6.1

protected function get_segments( $type, $query_params, $table_name ) {
	global $wpdb;
	if ( ! isset( $this->query_args['segmentby'] ) || '' === $this->query_args['segmentby'] ) {
		return array();
	}

	$product_segmenting_table = $wpdb->prefix . 'wc_order_product_lookup';
	$unique_orders_table      = '';
	$segmenting_where         = '';

	// Product, variation, and category are bound to product, so here product segmenting table is required,
	// while coupon and customer are bound to order, so we don't need the extra JOIN for those.
	// This also means that segment selections need to be calculated differently.
	if ( 'product' === $this->query_args['segmentby'] ) {
		$product_level_columns     = $this->get_segment_selections_product_level( $product_segmenting_table );
		$order_level_columns       = $this->get_segment_selections_order_level( $table_name );
		$segmenting_selections     = array(
			'product_level' => $this->prepare_selections( $product_level_columns ),
			'order_level'   => $this->prepare_selections( $order_level_columns ),
		);
		$this->report_columns      = array_merge( $product_level_columns, $order_level_columns );
		$segmenting_from           = "INNER JOIN $product_segmenting_table ON ($table_name.order_id = $product_segmenting_table.order_id)";
		$segmenting_groupby        = $product_segmenting_table . '.product_id';
		$segmenting_dimension_name = 'product_id';

		$segments = $this->get_product_related_segments( $type, $segmenting_selections, $segmenting_from, $segmenting_where, $segmenting_groupby, $segmenting_dimension_name, $table_name, $query_params, $unique_orders_table );
	} elseif ( 'variation' === $this->query_args['segmentby'] ) {
		if ( ! isset( $this->query_args['product_includes'] ) || count( $this->query_args['product_includes'] ) !== 1 ) {
			throw new ParameterException( 'wc_admin_reports_invalid_segmenting_variation', __( 'product_includes parameter need to specify exactly one product when segmenting by variation.', 'woocommerce' ) );
		}

		$product_level_columns     = $this->get_segment_selections_product_level( $product_segmenting_table );
		$order_level_columns       = $this->get_segment_selections_order_level( $table_name );
		$segmenting_selections     = array(
			'product_level' => $this->prepare_selections( $product_level_columns ),
			'order_level'   => $this->prepare_selections( $order_level_columns ),
		);
		$this->report_columns      = array_merge( $product_level_columns, $order_level_columns );
		$segmenting_from           = "INNER JOIN $product_segmenting_table ON ($table_name.order_id = $product_segmenting_table.order_id)";
		$segmenting_where          = "AND $product_segmenting_table.product_id = {$this->query_args['product_includes'][0]}";
		$segmenting_groupby        = $product_segmenting_table . '.variation_id';
		$segmenting_dimension_name = 'variation_id';

		$segments = $this->get_product_related_segments( $type, $segmenting_selections, $segmenting_from, $segmenting_where, $segmenting_groupby, $segmenting_dimension_name, $table_name, $query_params, $unique_orders_table );
	} elseif ( 'category' === $this->query_args['segmentby'] ) {
		$product_level_columns     = $this->get_segment_selections_product_level( $product_segmenting_table );
		$order_level_columns       = $this->get_segment_selections_order_level( $table_name );
		$segmenting_selections     = array(
			'product_level' => $this->prepare_selections( $product_level_columns ),
			'order_level'   => $this->prepare_selections( $order_level_columns ),
		);
		$this->report_columns      = array_merge( $product_level_columns, $order_level_columns );
		$segmenting_from           = "
		INNER JOIN $product_segmenting_table ON ($table_name.order_id = $product_segmenting_table.order_id)
		LEFT JOIN {$wpdb->term_relationships} ON {$product_segmenting_table}.product_id = {$wpdb->term_relationships}.object_id
		JOIN {$wpdb->term_taxonomy} ON {$wpdb->term_taxonomy}.term_taxonomy_id = {$wpdb->term_relationships}.term_taxonomy_id
		LEFT JOIN {$wpdb->wc_category_lookup} ON {$wpdb->term_taxonomy}.term_id = {$wpdb->wc_category_lookup}.category_id
		";
		$segmenting_where          = " AND {$wpdb->wc_category_lookup}.category_tree_id IS NOT NULL";
		$segmenting_groupby        = "{$wpdb->wc_category_lookup}.category_tree_id";
		$segmenting_dimension_name = 'category_id';

		$segments = $this->get_product_related_segments( $type, $segmenting_selections, $segmenting_from, $segmenting_where, $segmenting_groupby, $segmenting_dimension_name, $table_name, $query_params, $unique_orders_table );
	} elseif ( 'coupon' === $this->query_args['segmentby'] ) {
		$coupon_level_columns  = $this->segment_selections_orders( $table_name );
		$segmenting_selections = $this->prepare_selections( $coupon_level_columns );
		$this->report_columns  = $coupon_level_columns;
		$segmenting_from       = '';
		$segmenting_groupby    = "$table_name.coupon_id";

		$segments = $this->get_order_related_segments( $type, $segmenting_selections, $segmenting_from, $segmenting_where, $segmenting_groupby, $table_name, $query_params );
	}

	return $segments;
}