WC_REST_Products_V2_Controller::prepare_objects_query │ protected │ WC 3.0.0

Prepare objects query.

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

Хуков нет.

Возвращает

array.

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

// protected - в коде основоного (родительского) или дочернего класса
$result = $this->prepare_objects_query( $request );
$request(WP_REST_Request) (обязательный)
Full details about the request.

Список изменений

С версии 3.0.0 Введена.

Код WC_REST_Products_V2_Controller::prepare_objects_query() WC 11.3.0

protected function prepare_objects_query( $request ) {
	$args = parent::prepare_objects_query( $request );

	// Set post_status.
	$args['post_status'] = $request['status'];

	// Taxonomy query to filter products by type, category,
	// tag, shipping class, and attribute.
	$tax_query = array();

	// Map between taxonomy name and arg's key.
	$taxonomies = array(
		'product_cat'            => 'category',
		'product_tag'            => 'tag',
		'product_shipping_class' => 'shipping_class',
	);

	// Set tax_query for each passed arg.
	foreach ( $taxonomies as $taxonomy => $key ) {
		if ( ! empty( $request[ $key ] ) ) {
			$tax_query[] = array(
				'taxonomy' => $taxonomy,
				'field'    => 'term_id',
				'terms'    => $request[ $key ],
			);
		}
	}

	// Filter product type by slug.
	if ( ! empty( $request['type'] ) ) {
		$tax_query[] = array(
			'taxonomy' => 'product_type',
			'field'    => 'slug',
			'terms'    => $request['type'],
		);
	}

	// Filter by attribute and term.
	if ( ! empty( $request['attribute'] ) && ! empty( $request['attribute_term'] ) ) {
		if ( in_array( $request['attribute'], wc_get_attribute_taxonomy_names(), true ) ) {
			$tax_query[] = array(
				'taxonomy' => $request['attribute'],
				'field'    => 'term_id',
				'terms'    => $request['attribute_term'],
			);
		}
	}

	if ( ! empty( $tax_query ) ) {
		$args['tax_query'] = $tax_query; // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query -- Product taxonomy filters use WordPress's canonical indexed taxonomy tables.
	}

	// Filter featured.
	if ( is_bool( $request['featured'] ) ) {
		$args['tax_query'][] = array(
			'taxonomy' => 'product_visibility',
			'field'    => 'name',
			'terms'    => 'featured',
			'operator' => true === $request['featured'] ? 'IN' : 'NOT IN',
		);
	}

	// Filter by sku.
	if ( ! empty( $request['sku'] ) ) {
		$skus = explode( ',', $request['sku'] );
		// Include the current string as a SKU too.
		if ( 1 < count( $skus ) ) {
			$skus[] = $request['sku'];
		}

		$args['meta_query'] = $this->add_meta_query( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query -- Exact _sku match; wc_product_meta_lookup truncates SKUs to 100 chars and is empty while it regenerates, so it is not an equivalent path.
			$args,
			array(
				'key'     => '_sku',
				'value'   => $skus,
				'compare' => 'IN',
			)
		);
	}

	// Filter by tax class.
	if ( ! empty( $request['tax_class'] ) ) {
		$args['meta_query'] = $this->add_meta_query( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query -- wc_product_meta_lookup.tax_class is unindexed and holds NULL where _tax_class is unset, so it is neither faster nor equivalent.
			$args,
			array(
				'key'   => '_tax_class',
				'value' => 'standard' !== $request['tax_class'] ? $request['tax_class'] : '',
			)
		);
	}

	// Price filter.
	if ( ! empty( $request['min_price'] ) || ! empty( $request['max_price'] ) ) {
		$args['meta_query'] = $this->add_meta_query( $args, wc_get_min_max_price_meta_query( $request ) );  // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query -- _price is stored once per child price; wc_product_meta_lookup keeps only min/max and would match a wider range.
	}

	// Filter product in stock or out of stock.
	if ( is_bool( $request['in_stock'] ) ) {
		$args['meta_query'] = $this->add_meta_query( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query -- Postmeta stays correct while wc_product_meta_lookup is stale or regenerating; the _stock_status meta_key index bounds the scan.
			$args,
			array(
				'key'   => '_stock_status',
				'value' => true === $request['in_stock'] ? ProductStockStatus::IN_STOCK : ProductStockStatus::OUT_OF_STOCK,
			)
		);
	}

	// Filter by on sale products.
	if ( is_bool( $request['on_sale'] ) ) {
		$on_sale_ids = wc_get_product_ids_on_sale();

		// Use 0 when there's no on sale products to avoid return all products.
		$on_sale_ids = empty( $on_sale_ids ) ? array( 0 ) : $on_sale_ids;

		if ( $request['on_sale'] || ! empty( $args['post__in'] ) ) {
			$post_in = empty( $args['post__in'] ) ? $on_sale_ids : $args['post__in'];

			// WP_Query ignores post__not_in when post__in is set, so the on_sale filter and exclude are applied to post__in.
			if ( $request['on_sale'] ) {
				$post_in = array_intersect( $post_in, $on_sale_ids );
			} else {
				$post_in = array_diff( $post_in, $on_sale_ids );
			}
			$post_in = array_diff( $post_in, (array) $args['post__not_in'] );

			$args['post__in'] = empty( $post_in ) ? array( 0 ) : array_values( $post_in );
		} else {
			$args['post__not_in'] = array_merge( (array) $args['post__not_in'], $on_sale_ids );
		}
	}

	// Force the post_type argument, since it's not a user input variable.
	if ( ! empty( $request['sku'] ) ) {
		$args['post_type'] = array( 'product', 'product_variation' );
	} else {
		$args['post_type'] = $this->post_type;
	}

	return $args;
}