WC_REST_Legacy_Products_Controller::prepare_item_for_response() public WC 1.0
Deprecated from version 3.0.0. It is no longer supported and can be removed in future releases. It is recommended to replace this function with the same one.╳
Prepare a single product output for response.
{} It's a method of the class: WC_REST_Legacy_Products_Controller{}
Hooks from the method
Return
WP_REST_Response.
Usage
$WC_REST_Legacy_Products_Controller = new WC_REST_Legacy_Products_Controller(); $WC_REST_Legacy_Products_Controller->prepare_item_for_response( $post, $request );
- $post(WP_Post) (required)
- Post object.
- $request(WP_REST_Request) (required)
- Request object.
Changelog
Deprecated | 3.0.0 |
Code of WC_REST_Legacy_Products_Controller::prepare_item_for_response() WC REST Legacy Products Controller::prepare item for response WC 5.0.0
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( 'variable' ) && $product->has_child() ) {
$data['variations'] = $product->get_children();
}
// Add grouped products data.
if ( $product->is_type( '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 );
}