WC_API_Products::save_product_images()protectedWC 2.2

Save product images.

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

Хуков нет.

Возвращает

WC_Product.

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

// protected - в коде основоного (родительского) или дочернего класса
$result = $this->save_product_images( $product, $images );
$product(WC_Product) (обязательный)
-
$images(массив) (обязательный)
-

Список изменений

С версии 2.2 Введена.

Код WC_API_Products::save_product_images() WC 8.7.0

protected function save_product_images( $product, $images ) {
	if ( is_array( $images ) ) {
		$gallery = array();

		foreach ( $images as $image ) {
			if ( isset( $image['position'] ) && 0 == $image['position'] ) {
				$attachment_id = isset( $image['id'] ) ? absint( $image['id'] ) : 0;

				if ( 0 === $attachment_id && isset( $image['src'] ) ) {
					$upload = $this->upload_product_image( esc_url_raw( $image['src'] ) );

					if ( is_wp_error( $upload ) ) {
						throw new WC_API_Exception( 'woocommerce_api_cannot_upload_product_image', $upload->get_error_message(), 400 );
					}

					$attachment_id = $this->set_product_image_as_attachment( $upload, $product->get_id() );
				}

				$product->set_image_id( $attachment_id );
			} else {
				$attachment_id = isset( $image['id'] ) ? absint( $image['id'] ) : 0;

				if ( 0 === $attachment_id && isset( $image['src'] ) ) {
					$upload = $this->upload_product_image( esc_url_raw( $image['src'] ) );

					if ( is_wp_error( $upload ) ) {
						throw new WC_API_Exception( 'woocommerce_api_cannot_upload_product_image', $upload->get_error_message(), 400 );
					}

					$attachment_id = $this->set_product_image_as_attachment( $upload, $product->get_id() );
				}

				$gallery[] = $attachment_id;
			}

			// Set the image alt if present.
			if ( ! empty( $image['alt'] ) && $attachment_id ) {
				update_post_meta( $attachment_id, '_wp_attachment_image_alt', wc_clean( $image['alt'] ) );
			}

			// Set the image title if present.
			if ( ! empty( $image['title'] ) && $attachment_id ) {
				wp_update_post( array( 'ID' => $attachment_id, 'post_title' => $image['title'] ) );
			}
		}

		if ( ! empty( $gallery ) ) {
			$product->set_gallery_image_ids( $gallery );
		}
	} else {
		$product->set_image_id( '' );
		$product->set_gallery_image_ids( array() );
	}

	return $product;
}