WC_REST_Products_V2_Controller::get_images()protectedWC 1.0

Get the images for a product or product variation.

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

Хуков нет.

Возвращает

Массив.

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

// protected - в коде основоного (родительского) или дочернего класса
$result = $this->get_images( $product );
$product(WC_Product|WC_Product_Variation) (обязательный)
Product instance.

Код WC_REST_Products_V2_Controller::get_images() WC 9.3.3

protected function get_images( $product ) {
	$images         = array();
	$attachment_ids = array();

	// Add featured image.
	if ( $product->get_image_id() ) {
		$attachment_ids[] = $product->get_image_id();
	}

	// Add gallery images.
	$attachment_ids = array_merge( $attachment_ids, $product->get_gallery_image_ids() );

	// Build image data.
	foreach ( $attachment_ids as $position => $attachment_id ) {
		$attachment_post = get_post( $attachment_id );
		if ( is_null( $attachment_post ) ) {
			continue;
		}

		$attachment = wp_get_attachment_image_src( $attachment_id, 'full' );
		if ( ! is_array( $attachment ) ) {
			continue;
		}

		$images[] = array(
			'id'                => (int) $attachment_id,
			'date_created'      => wc_rest_prepare_date_response( $attachment_post->post_date, false ),
			'date_created_gmt'  => wc_rest_prepare_date_response( strtotime( $attachment_post->post_date_gmt ) ),
			'date_modified'     => wc_rest_prepare_date_response( $attachment_post->post_modified, false ),
			'date_modified_gmt' => wc_rest_prepare_date_response( strtotime( $attachment_post->post_modified_gmt ) ),
			'src'               => current( $attachment ),
			'name'              => get_the_title( $attachment_id ),
			'alt'               => get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ),
			'position'          => (int) $position,
		);
	}

	// Set a placeholder image if the product has no images set.
	if ( empty( $images ) ) {
		$images[] = array(
			'id'                => 0,
			'date_created'      => wc_rest_prepare_date_response( current_time( 'mysql' ), false ), // Default to now.
			'date_created_gmt'  => wc_rest_prepare_date_response( time() ), // Default to now.
			'date_modified'     => wc_rest_prepare_date_response( current_time( 'mysql' ), false ),
			'date_modified_gmt' => wc_rest_prepare_date_response( time() ),
			'src'               => wc_placeholder_img_src(),
			'name'              => __( 'Placeholder', 'woocommerce' ),
			'alt'               => __( 'Placeholder', 'woocommerce' ),
			'position'          => 0,
		);
	}

	return $images;
}