WC_API_Products::get_product() public WC 2.1
Get the product for the given ID
{} It's a method of the class: WC_API_Products{}
Hooks from the method
Return
Array|WP_Error.
Usage
$WC_API_Products = new WC_API_Products(); $WC_API_Products->get_product( $id, $fields );
- $id(int) (required)
- the product ID
- $fields(string)
- -
Changelog
Since 2.1 | Introduced. |
Code of WC_API_Products::get_product() WC API Products::get product WC 5.0.0
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_data['parent'] = $this->get_product_data( $product->get_parent_id() );
}
return array( 'product' => apply_filters( 'woocommerce_api_product_response', $product_data, $product, $fields, $this->server ) );
}