Automattic\WooCommerce\StoreApi\Schemas\V1

ImageAttachmentSchema::get_item_response()publicWC 1.0

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

Метод класса: ImageAttachmentSchema{}

Хуков нет.

Возвращает

Объект|null.

Использование

$ImageAttachmentSchema = new ImageAttachmentSchema();
$ImageAttachmentSchema->get_item_response( $attachment_id );
$attachment_id(int) (обязательный)
Image attachment ID.

Код ImageAttachmentSchema::get_item_response() WC 8.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' ),
		'name'      => get_the_title( $attachment_id ),
		'alt'       => get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ),
	];
}