Automattic\WooCommerce\Admin\API\Reports

Segmenter::set_all_segments()protectedWC 1.0

Fetches all segment ids from db and stores it for later use.

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

Возвращает

null. Ничего (null).

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

// protected - в коде основоного (родительского) или дочернего класса
$result = $this->set_all_segments();

Код Segmenter::set_all_segments() WC 8.7.0

protected function set_all_segments() {
	global $wpdb;

	if ( ! isset( $this->query_args['segmentby'] ) || '' === $this->query_args['segmentby'] ) {
		$this->all_segment_ids = array();
		return;
	}

	$segments       = array();
	$segment_labels = array();

	if ( 'product' === $this->query_args['segmentby'] ) {
		$args = array(
			'return' => 'objects',
			'limit'  => -1,
		);

		if ( isset( $this->query_args['product_includes'] ) ) {
			$args['include'] = $this->query_args['product_includes'];
		}

		if ( isset( $this->query_args['category_includes'] ) ) {
			$categories       = $this->query_args['category_includes'];
			$args['category'] = array();
			foreach ( $categories as $category_id ) {
				$terms              = get_term_by( 'id', $category_id, 'product_cat' );
				$args['category'][] = $terms->slug;
			}
		}

		$segment_objects = wc_get_products( $args );
		foreach ( $segment_objects as $segment ) {
			$id                    = $segment->get_id();
			$segments[]            = $id;
			$segment_labels[ $id ] = $segment->get_name();
		}
	} elseif ( 'variation' === $this->query_args['segmentby'] ) {
		$args = array(
			'return' => 'objects',
			'limit'  => -1,
			'type'   => 'variation',
		);

		if (
			isset( $this->query_args['product_includes'] ) &&
			is_array( $this->query_args['product_includes'] ) &&
			count( $this->query_args['product_includes'] ) === 1
		) {
			$args['parent'] = $this->query_args['product_includes'][0];
		}

		if ( isset( $this->query_args['variation_includes'] ) ) {
			$args['include'] = $this->query_args['variation_includes'];
		}

		$segment_objects = wc_get_products( $args );

		foreach ( $segment_objects as $segment ) {
			$id           = $segment->get_id();
			$segments[]   = $id;
			$product_name = $segment->get_name();
			$separator    = apply_filters( 'woocommerce_product_variation_title_attributes_separator', ' - ', $segment );
			$attributes   = wc_get_formatted_variation( $segment, true, false );

			$segment_labels[ $id ] = $product_name . $separator . $attributes;
		}

		// If no variations were specified, add a segment for the parent product (variation = 0).
		// This is to catch simple products with prior sales converted into variable products.
		// See: https://github.com/woocommerce/woocommerce-admin/issues/2719.
		if ( isset( $args['parent'] ) && empty( $args['include'] ) ) {
			$parent_object     = wc_get_product( $args['parent'] );
			$segments[]        = 0;
			$segment_labels[0] = $parent_object->get_name();
		}
	} elseif ( 'category' === $this->query_args['segmentby'] ) {
		$args = array(
			'taxonomy' => 'product_cat',
		);

		if ( isset( $this->query_args['category_includes'] ) ) {
			$args['include'] = $this->query_args['category_includes'];
		}

		// @todo: Look into `wc_get_products` or data store methods and not directly touching the database or post types.
		$categories = get_categories( $args );

		$segments       = wp_list_pluck( $categories, 'cat_ID' );
		$segment_labels = wp_list_pluck( $categories, 'name', 'cat_ID' );

	} elseif ( 'coupon' === $this->query_args['segmentby'] ) {
		$args = array();
		if ( isset( $this->query_args['coupons'] ) ) {
			$args['include'] = $this->query_args['coupons'];
		}
		$coupons_store  = new CouponsDataStore();
		$coupons        = $coupons_store->get_coupons( $args );
		$segments       = wp_list_pluck( $coupons, 'ID' );
		$segment_labels = wp_list_pluck( $coupons, 'post_title', 'ID' );
		$segment_labels = array_map( 'wc_format_coupon_code', $segment_labels );
	} elseif ( 'customer_type' === $this->query_args['segmentby'] ) {
		// 0 -- new customer
		// 1 -- returning customer
		$segments = array( 0, 1 );
	} elseif ( 'tax_rate_id' === $this->query_args['segmentby'] ) {
		$args = array();
		if ( isset( $this->query_args['taxes'] ) ) {
			$args['include'] = $this->query_args['taxes'];
		}
		$taxes = TaxesStatsDataStore::get_taxes( $args );

		foreach ( $taxes as $tax ) {
			$id                    = $tax['tax_rate_id'];
			$segments[]            = $id;
			$segment_labels[ $id ] = \WC_Tax::get_rate_code( (object) $tax );
		}
	} else {
		// Catch all default.
		$segments = array();
	}

	$this->all_segment_ids = $segments;
	$this->segment_labels  = $segment_labels;
}