Automattic\WooCommerce\Blocks\BlockTypes

ProductGallery::resolve_product_image_aspect_ratio │ private │ WC 1.0

Resolve the gallery aspect ratio from the nested Product Image block.

Method of the class: ProductGallery{}

No Hooks.

Returns

string|null. CSS aspect ratio value, or null when no ratio should be applied.

Usage

// private - for code of main (parent) class only
$result = $this->resolve_product_image_aspect_ratio( $block );
$block(WP_Block) (required)
The Product Gallery block.

ProductGallery::resolve_product_image_aspect_ratio() code WC 11.3.0

private function resolve_product_image_aspect_ratio( $block ) {
	$attributes = $this->get_product_image_attributes( $block->inner_blocks );

	if ( empty( $attributes ) ) {
		return null;
	}

	if ( ! empty( $attributes['style']['dimensions']['aspectRatio'] ) ) {
		return $attributes['style']['dimensions']['aspectRatio'];
	}

	if ( ! empty( $attributes['aspectRatio'] ) ) {
		return $attributes['aspectRatio'];
	}

	$image_sizing = $attributes['imageSizing'] ?? 'single';

	if ( 'thumbnail' === $image_sizing || 'cropped' === $image_sizing ) {
		return $this->get_store_thumbnail_aspect_ratio();
	}

	return null;
}