Automattic\WooCommerce\StoreApi\Routes\V1\AI

Products::get_route_post_response()protectedWC 1.0

Generate the content for the products.

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

Хуков нет.

Возвращает

true|false|Строку|\WP_Error|\WP_REST_Response.

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

// protected - в коде основоного (родительского) или дочернего класса
$result = $this->get_route_post_response( $request );
$request(\WP_REST_Request) (обязательный)
Request object.

Код Products::get_route_post_response() WC 8.7.0

protected function get_route_post_response( \WP_REST_Request $request ) {
	$allow_ai_connection = get_option( 'woocommerce_blocks_allow_ai_connection' );

	if ( ! $allow_ai_connection ) {
		return rest_ensure_response(
			$this->error_to_response(
				new \WP_Error(
					'ai_connection_not_allowed',
					__( 'AI content generation is not allowed on this store. Update your store settings if you wish to enable this feature.', 'woocommerce' )
				)
			)
		);
	}

	$business_description = sanitize_text_field( wp_unslash( $request['business_description'] ) );

	if ( empty( $business_description ) ) {
		$business_description = get_option( 'woo_ai_describe_store_description' );
	}

	$ai_connection = new Connection();

	$site_id = $ai_connection->get_site_id();

	if ( is_wp_error( $site_id ) ) {
		return $this->error_to_response( $site_id );
	}

	$token = $ai_connection->get_jwt_token( $site_id );

	if ( is_wp_error( $token ) ) {
		return $this->error_to_response( $token );
	}

	$images = $request['images'];

	$populate_products = ( new UpdateProducts() )->generate_content( $ai_connection, $token, $images, $business_description );

	if ( is_wp_error( $populate_products ) ) {
		return $this->error_to_response( $populate_products );
	}

	if ( ! isset( $populate_products['product_content'] ) ) {
		return $this->error_to_response( new \WP_Error( 'product_content_not_found', __( 'Product content not found.', 'woocommerce' ) ) );
	}

	$product_content = $populate_products['product_content'];

	$item = array(
		'ai_content_generated' => true,
		'product_content'      => $product_content,
	);

	return rest_ensure_response( $item );
}