Automattic\WooCommerce\Admin\API\AI

StoreTitle::generate_ai_title()privateWC 1.0

Generate the store title powered by AI.

Method of the class: StoreTitle{}

No Hooks.

Return

String|WP_Error|WP_REST_Response. The store title generated by AI.

Usage

// private - for code of main (parent) class only
$result = $this->generate_ai_title( $business_description );
$business_description(string) (required)
The business description for a given store.

StoreTitle::generate_ai_title() code WC 9.7.1

private function generate_ai_title( $business_description ) {
	$ai_connection = new Connection();

	$site_id = $ai_connection->get_site_id();
	if ( is_wp_error( $site_id ) ) {
		return $site_id;
	}

	$token = $ai_connection->get_jwt_token( $site_id );
	if ( is_wp_error( $token ) ) {
		return $token;
	}

	$prompt = "Generate a store title for a store that has the following: '$business_description'. The length of the title should be 1 and 3 words. The result should include only the store title without any other explanation, number or punctuation marks";

	$ai_response = $ai_connection->fetch_ai_response( $token, $prompt );
	if ( is_wp_error( $ai_response ) ) {
		return $ai_response;
	}

	if ( ! isset( $ai_response['completion'] ) ) {
		return '';
	}

	return $ai_response['completion'];
}