WC_API_Products::get_product( int $id, string $fields = null )
Get the product for the given ID
Description Description
Parameters Parameters
- $id
-
(Required) the product ID
- $fields
-
(Optional)
Default value: null
Return Return
(array|WP_Error)
Source Source
File: includes/legacy/api/v2/class-wc-api-products.php
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 = wc_get_product( $product->get_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 ) ); }
Changelog Changelog
Version | Description |
---|---|
2.1 | Introduced. |