WC_REST_Products_V1_Controller::prepare_item_for_response()
Prepare a single product output for response.
Method of the class: WC_REST_Products_V1_Controller{}
Hooks from the method
Return
WP_REST_Response
.
Usage
$WC_REST_Products_V1_Controller = new WC_REST_Products_V1_Controller(); $WC_REST_Products_V1_Controller->prepare_item_for_response( $post, $request );
- $post(WP_Post) (required)
- Post object.
- $request(WP_REST_Request) (required)
- Request object.
WC_REST_Products_V1_Controller::prepare_item_for_response() WC REST Products V1 Controller::prepare item for response code WC 9.7.1
public function prepare_item_for_response( $post, $request ) { $product = wc_get_product( $post ); $data = $this->get_product_data( $product ); // Add variations to variable products. if ( $product->is_type( ProductType::VARIABLE ) && $product->has_child() ) { $data['variations'] = $this->get_variation_data( $product ); } // Add grouped products data. if ( $product->is_type( ProductType::GROUPED ) && $product->has_child() ) { $data['grouped_products'] = $product->get_children(); } $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; $data = $this->add_additional_fields_to_object( $data, $request ); $data = $this->filter_response_by_context( $data, $context ); // Wrap the data in a response object. $response = rest_ensure_response( $data ); $response->add_links( $this->prepare_links( $product, $request ) ); /** * Filter the data for a response. * * The dynamic portion of the hook name, $this->post_type, refers to post_type of the post being * prepared for the response. * * @param WP_REST_Response $response The response object. * @param WP_Post $post Post object. * @param WP_REST_Request $request Request object. */ return apply_filters( "woocommerce_rest_prepare_{$this->post_type}", $response, $post, $request ); }