Automattic\WooCommerce\Blocks\BlockTypes

ProductAverageRating::render()protectedWC 1.0

Include and render the block.

Method of the class: ProductAverageRating{}

No Hooks.

Return

String. Rendered block type output.

Usage

// protected - for code of main (parent) or child class
$result = $this->render( $attributes, $content, $block );
$attributes(array) (required)
Block attributes.
Default: empty array
$content(string) (required)
Block content.
Default: empty string
$block(WP_Block) (required)
Block instance.

ProductAverageRating::render() code WC 9.0.1

protected function render( $attributes, $content, $block ) {
	$post_id = $block->context['postId'];
	$product = wc_get_product( $post_id );

	if ( ! $product || ! $product->get_review_count() ) {
		return '';
	}

	$styles_and_classes            = StyleAttributesUtils::get_classes_and_styles_by_attributes( $attributes );
	$text_align_styles_and_classes = StyleAttributesUtils::get_text_align_class_and_style( $attributes );

	return sprintf(
		'<div class="wc-block-components-product-average-rating-counter %1$s %2$s" style="%3$s">%4$s</div>',
		esc_attr( $text_align_styles_and_classes['class'] ?? '' ),
		esc_attr( $styles_and_classes['classes'] ),
		esc_attr( $styles_and_classes['styles'] ?? '' ),
		$product->get_average_rating()
	);
}