Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
Store Customization > Ensure the price for products is also AI-genera…
Browse files Browse the repository at this point in the history
…ted (#11859)

* Generate the product price with AI

* Fine tune the prompt to take into account the store currency

* Improve the price prompt
  • Loading branch information
albarin committed Nov 23, 2023
1 parent c9f99af commit 30c1f35
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/Patterns/ProductUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ public function update_product_content( $ai_generated_product_content ) {

$product->set_name( $ai_generated_product_content['title'] );
$product->set_description( $ai_generated_product_content['description'] );
$product->set_regular_price( $ai_generated_product_content['price'] );

require_once ABSPATH . 'wp-admin/includes/media.php';
require_once ABSPATH . 'wp-admin/includes/file.php';
Expand Down Expand Up @@ -292,6 +293,7 @@ public function assign_ai_selected_images_to_dummy_products( $dummy_products_to_
$products_information_list[] = [
'title' => 'A product title',
'description' => 'A product description',
'price' => 'The product price',
'image' => [
'src' => esc_url( $image_src ),
'alt' => esc_attr( $image_alt ),
Expand Down Expand Up @@ -329,12 +331,16 @@ public function assign_ai_generated_content_to_dummy_products( $ai_connection, $

$expected_results_format = [];
foreach ( $products_information_list as $index => $product ) {
$expected_results_format[ $index ] = '';
$expected_results_format[ $index ] = [
'title' => '',
'price' => '',
];
}

$formatted_prompt = sprintf(
"Generate two-words titles for products using the following prompts for each one of them: '%s'. Ensure each entry is unique and does not repeat the given examples. Do not include backticks or the word json in the response. Here's an example format: '%s'.",
"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 format: '%s'.",
wp_json_encode( $prompts ),
get_woocommerce_currency(),
wp_json_encode( $expected_results_format )
);

Expand Down Expand Up @@ -381,7 +387,8 @@ public function assign_ai_generated_content_to_dummy_products( $ai_connection, $
}

foreach ( $products_information_list as $index => $product_information ) {
$products_information_list[ $index ]['title'] = str_replace( '"', '', $completion[ $index ] );
$products_information_list[ $index ]['title'] = str_replace( '"', '', $completion[ $index ]['title'] );
$products_information_list[ $index ]['price'] = $completion[ $index ]['price'];
}

$success = true;
Expand Down

0 comments on commit 30c1f35

Please sign in to comment.