Automattic\WooCommerce\Blocks\AIContent
UpdateProducts::assign_ai_generated_content_to_dummy_products()
Generate the product content.
Method of the class: UpdateProducts{}
No Hooks.
Return
Array|Int|String|\WP_Error
.
Usage
$UpdateProducts = new UpdateProducts(); $UpdateProducts->assign_ai_generated_content_to_dummy_products( $ai_connection, $token, $products_information_list, $business_description, $search_term );
- $ai_connection(Connection) (required)
- The AI connection.
- $token(string) (required)
- The JWT token.
- $products_information_list(array) (required)
- The products information list.
- $business_description(string) (required)
- The business description.
- $search_term(string) (required)
- The search term.
UpdateProducts::assign_ai_generated_content_to_dummy_products() UpdateProducts::assign ai generated content to dummy products code WC 9.8.1
public function assign_ai_generated_content_to_dummy_products( $ai_connection, $token, $products_information_list, $business_description, $search_term ) { $business_description = ContentProcessor::summarize_business_description( $business_description, $ai_connection, $token, 100 ); if ( is_wp_error( $business_description ) ) { return $business_description; } $prompts = []; foreach ( $products_information_list as $product_information ) { if ( ! empty( $product_information['image']['alt'] ) ) { $prompts[] = sprintf( 'Considering that you are the owner of a store with the following description "%s", create the title for a product that is related to "%s" and to an image described as "%s". Do not include any adjectives or descriptions of the qualities of the product and always refer to objects or services, not humans.', $business_description, $search_term, $product_information['image']['alt'] ); } else { $prompts[] = sprintf( 'You are the owner of a business described as: "%s". Create the title for a product that could be sold on your store. Do not include any adjectives or descriptions of the qualities of the product and always refer to objects or services, not humans.', $business_description ); } } $expected_results_format = []; foreach ( $products_information_list as $index => $product ) { $expected_results_format[ $index ] = [ 'title' => '', 'price' => '', ]; } $formatted_prompt = sprintf( "Generate two-words titles and price for products using the following prompts for each one of them: '%s'. Ensure each entry is unique and does not repeat the given examples. It should be a number and it's not too low or too high for the corresponding product title being advertised. Convert the price to this currency: '%s'. Do not include backticks or the word json in the response. Here's an example of the expected output format in JSON: '%s'.", wp_json_encode( $prompts ), get_woocommerce_currency(), wp_json_encode( $expected_results_format ) ); $ai_request_retries = 0; $success = false; while ( $ai_request_retries < 5 && ! $success ) { ++$ai_request_retries; $ai_response = $ai_connection->fetch_ai_response( $token, $formatted_prompt, 30 ); if ( is_wp_error( $ai_response ) ) { continue; } if ( empty( $ai_response ) ) { continue; } if ( ! isset( $ai_response['completion'] ) ) { continue; } $completion = json_decode( $ai_response['completion'], true ); if ( ! is_array( $completion ) ) { continue; } $diff = array_diff_key( $expected_results_format, $completion ); if ( ! empty( $diff ) ) { continue; } $empty_results = false; foreach ( $completion as $completion_item ) { if ( empty( $completion_item ) ) { $empty_results = true; break; } } if ( $empty_results ) { continue; } foreach ( $products_information_list as $index => $product_information ) { $products_information_list[ $index ]['title'] = str_replace( '"', '', $completion[ $index ]['title'] ); $products_information_list[ $index ]['price'] = $completion[ $index ]['price']; } $success = true; } if ( ! $success ) { return new WP_Error( 'failed_to_fetch_ai_responses', __( 'Failed to fetch AI responses for products.', 'woocommerce' ) ); } return array( 'product_content' => $products_information_list, ); }