WC_API_Products::get_product()publicWC 2.1

Get the product for the given ID

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

Хуки из метода

Возвращает

Массив|WP_Error.

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

$WC_API_Products = new WC_API_Products();
$WC_API_Products->get_product( $id, $fields );
$id(int) (обязательный)
the product ID
$fields(строка)
-
По умолчанию: null

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

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

Код WC_API_Products::get_product() WC 8.9.3

public function get_product( $id, $fields = null ) {

	$id = $this->validate_request( $id, 'product', 'read' );

	if ( is_wp_error( $id ) ) {
		return $id;
	}

	$product = wc_get_product( $id );

	// add data that applies to every product type
	$product_data = $this->get_product_data( $product );

	// add variations to variable products
	if ( $product->is_type( 'variable' ) && $product->has_child() ) {
		$product_data['variations'] = $this->get_variation_data( $product );
	}

	// add the parent product data to an individual variation
	if ( $product->is_type( 'variation' ) && $product->get_parent_id() ) {
		$product_data['parent'] = $this->get_product_data( $product->get_parent_id() );
	}

	// Add grouped products data
	if ( $product->is_type( 'grouped' ) && $product->has_child() ) {
		$product_data['grouped_products'] = $this->get_grouped_products_data( $product );
	}

	if ( $product->is_type( 'simple' ) ) {
		$parent_id = $product->get_parent_id();
		if ( ! empty( $parent_id ) ) {
			$_product               = wc_get_product( $parent_id );
			$product_data['parent'] = $this->get_product_data( $_product );
		}
	}

	return array( 'product' => apply_filters( 'woocommerce_api_product_response', $product_data, $product, $fields, $this->server ) );
}