WC_REST_Product_Reviews_V1_Controller::get_items()publicWC 1.0

Get all reviews from a product.

Method of the class: WC_REST_Product_Reviews_V1_Controller{}

No Hooks.

Return

Array|WP_Error.

Usage

$WC_REST_Product_Reviews_V1_Controller = new WC_REST_Product_Reviews_V1_Controller();
$WC_REST_Product_Reviews_V1_Controller->get_items( $request );
$request(WP_REST_Request) (required)
-

WC_REST_Product_Reviews_V1_Controller::get_items() code WC 8.7.0

public function get_items( $request ) {
	$product_id = (int) $request['product_id'];

	if ( 'product' !== get_post_type( $product_id ) ) {
		return new WP_Error( 'woocommerce_rest_product_invalid_id', __( 'Invalid product ID.', 'woocommerce' ), array( 'status' => 404 ) );
	}

	$reviews = get_approved_comments( $product_id );
	$data    = array();
	foreach ( $reviews as $review_data ) {
		$review = $this->prepare_item_for_response( $review_data, $request );
		$review = $this->prepare_response_for_collection( $review );
		$data[] = $review;
	}

	return rest_ensure_response( $data );
}