Automattic\WooCommerce\Blocks\BlockTypes

ProductImage::renderprotectedWC 1.0

Include and render the block

Method of the class: ProductImage{}

No Hooks.

Returns

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.

ProductImage::render() code WC 10.4.3

protected function render( $attributes, $content, $block ) {
	$parsed_attributes  = $this->parse_attributes( $attributes );
	$classes_and_styles = StyleAttributesUtils::get_classes_and_styles_by_attributes( $attributes, array(), array( 'extra_classes' ) );
	$post_id            = isset( $block->context['postId'] ) ? $block->context['postId'] : '';
	$image_id           = isset( $block->context['imageId'] ) ? (int) $block->context['imageId'] : null;
	$product            = wc_get_product( $post_id );
	$aspect_ratio       = $parsed_attributes['aspectRatio'] ?? $parsed_attributes['style']['dimensions']['aspectRatio'] ?? 'auto';
	$aspect_ratio_class = 'wc-block-components-product-image--aspect-ratio-' . str_replace( '/', '-', $aspect_ratio );

	$classes = implode(
		' ',
		array_filter(
			array(
				'wc-block-components-product-image wc-block-grid__product-image',
				$aspect_ratio_class,
				esc_attr( $classes_and_styles['classes'] ),
			)
		)
	);

	$wrapper_attributes = get_block_wrapper_attributes(
		array(
			'class' => esc_attr( $classes ),
			'style' => esc_attr( $classes_and_styles['styles'] ),
		)
	);

	if ( $product ) {
		$inner_content = $this->render_anchor(
			$product,
			$this->render_on_sale_badge( $product, $parsed_attributes ),
			$this->render_image( $product, $parsed_attributes, $image_id ),
			$attributes,
			$content
		);

		return sprintf(
			'<div %1$s>%2$s</div>',
			$wrapper_attributes,
			$inner_content
		);
	}

	return '';
}