Automattic\WooCommerce\StoreApi\Schemas\V1

ImageAttachmentSchema::get_item_responsepublicWC 1.0

Convert a WooCommerce product into an object suitable for the response.

Method of the class: ImageAttachmentSchema{}

No Hooks.

Returns

Object|null.

Usage

$ImageAttachmentSchema = new ImageAttachmentSchema();
$ImageAttachmentSchema->get_item_response( $attachment_id );
$attachment_id(int) (required)
Image attachment ID.

ImageAttachmentSchema::get_item_response() code WC 10.7.0

public function get_item_response( $attachment_id ) {
	if ( ! $attachment_id ) {
		return null;
	}

	$attachment = wp_get_attachment_image_src( $attachment_id, 'full' );

	if ( ! is_array( $attachment ) ) {
		return null;
	}

	$thumbnail = wp_get_attachment_image_src( $attachment_id, 'woocommerce_thumbnail' );

	return (object) [
		'id'               => (int) $attachment_id,
		'src'              => current( $attachment ),
		'thumbnail'        => current( $thumbnail ),
		'srcset'           => (string) wp_get_attachment_image_srcset( $attachment_id, 'full' ),
		'sizes'            => (string) wp_get_attachment_image_sizes( $attachment_id, 'full' ),
		'thumbnail_srcset' => (string) wp_get_attachment_image_srcset( $attachment_id, 'woocommerce_thumbnail' ),
		'thumbnail_sizes'  => (string) wp_get_attachment_image_sizes( $attachment_id, 'woocommerce_thumbnail' ),
		'name'             => get_the_title( $attachment_id ),
		'alt'              => get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ),
	];
}