Automattic\WooCommerce\Blocks\BlockTypes
ProductImage::render_image
Render Image.
Method of the class: ProductImage{}
Hooks from the method
Returns
String.
Usage
// private - for code of main (parent) class only $result = $this->render_image( $product, $attributes, $image_id );
- $product(WC_Product) (required)
- Product object.
- $attributes(array) (required)
- Parsed attributes.
- $image_id(int|null)
- Optional image ID from context.
Default:null
ProductImage::render_image() ProductImage::render image code WC 10.7.0
private function render_image( $product, $attributes, $image_id = null ) {
$image_size = 'single' === $attributes['imageSizing'] ? 'woocommerce_single' : 'woocommerce_thumbnail';
$image_style = '';
if ( ! empty( $attributes['height'] ) ) {
$image_style .= sprintf( 'height:%s;', $attributes['height'] );
}
if ( ! empty( $attributes['width'] ) ) {
$image_style .= sprintf( 'width:%s;', $attributes['width'] );
}
if ( ! empty( $attributes['scale'] ) ) {
$image_style .= sprintf( 'object-fit:%s;', $attributes['scale'] );
}
// Keep this aspect ratio for backward compatibility.
if ( ! empty( $attributes['aspectRatio'] ) ) {
$image_style .= sprintf( 'aspect-ratio:%s;', $attributes['aspectRatio'] );
}
if ( ! empty( $attributes['style']['dimensions']['aspectRatio'] ) ) {
$image_style .= sprintf( 'aspect-ratio:%s;', $attributes['style']['dimensions']['aspectRatio'] );
}
if ( ! empty( $attributes['style']['dimensions']['minHeight'] ) ) {
$image_style .= sprintf( 'min-height:%s;', $attributes['style']['dimensions']['minHeight'] );
}
$featured_image_id = (int) $product->get_image_id();
$provided_image_id_is_valid = false;
if ( $image_id ) {
$gallery_image_ids = ProductGalleryUtils::get_all_image_ids( $product );
$available_image_ids = array_merge( [ $featured_image_id ], $gallery_image_ids );
$provided_image_id_is_valid = in_array( $image_id, $available_image_ids, true );
}
$target_image_id = $provided_image_id_is_valid ? $image_id : $featured_image_id;
if ( ! $target_image_id ) {
return wc_placeholder_img( $image_size, array( 'style' => $image_style ) );
}
$alt_text = get_post_meta( $target_image_id, '_wp_attachment_image_alt', true );
/**
* Filters the loading attribute for product images.
*
* Allowed values are 'lazy', 'eager', and 'auto'. Any other value will result in default browser behavior.
*
* @since 10.6.0
*
* @param string $loading_attr The loading attribute. Default 'lazy'.
* @param int $image_id Target image ID.
*/
$loading_attr = apply_filters(
'woocommerce_product_image_loading_attr',
'lazy',
$target_image_id,
);
$loading_attr = is_string( $loading_attr ) ? strtolower( trim( $loading_attr ) ) : '';
$allowed_loading = array( 'lazy', 'eager', 'auto' );
if ( ! in_array( $loading_attr, $allowed_loading, true ) ) {
$loading_attr = '';
}
$attr = array(
'alt' => empty( $alt_text ) ? $product->get_title() : $alt_text,
'data-testid' => 'product-image',
'data-image-id' => $target_image_id,
'style' => $image_style,
);
if ( ! empty( $loading_attr ) ) {
$attr['loading'] = $loading_attr;
}
return $provided_image_id_is_valid ? wp_get_attachment_image( $image_id, $image_size, false, $attr ) : $product->get_image( $image_size, $attr );
}