WC_REST_Posts_Controller::get_item PHP Method

get_item() public method

Get a single item.
public get_item ( WP_REST_Request $request ) : WP_Error | WP_REST_Response
$request WP_REST_Request Full details about the request.
return WP_Error | WP_REST_Response
    public function get_item($request)
    {
        $id = (int) $request['id'];
        $post = get_post($id);
        if (!empty($post->post_type) && 'product_variation' === $post->post_type && 'product' === $this->post_type) {
            return new WP_Error("woocommerce_rest_invalid_{$this->post_type}_id", __('To manipulate product variations you should use the /products/<product_id>/variations/<id> endpoint.', 'woocommerce'), array('status' => 404));
        } elseif (empty($id) || empty($post->ID) || $post->post_type !== $this->post_type) {
            return new WP_Error("woocommerce_rest_invalid_{$this->post_type}_id", __('Invalid ID.', 'woocommerce'), array('status' => 404));
        }
        $data = $this->prepare_item_for_response($post, $request);
        $response = rest_ensure_response($data);
        if ($this->public) {
            $response->link_header('alternate', get_permalink($id), array('type' => 'text/html'));
        }
        return $response;
    }