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

Commit

Permalink
[CYS] Fix regression and ensure AI-generated content is assigned to p…
Browse files Browse the repository at this point in the history
…roducts after the third attempt (#12016)

* Fix restriction preventing the update of products with AI-generated content after the 3rd attempt.

* use set_slug rather than wp_update_post for updating the product permalink.

---------

Co-authored-by: Alba Rincón <alba.rincon@automattic.com>
  • Loading branch information
nefeline and albarin committed Dec 4, 2023
1 parent 5a73912 commit d51816f
Showing 1 changed file with 43 additions and 28 deletions.
71 changes: 43 additions & 28 deletions src/Patterns/ProductUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ public function generate_content( $ai_connection, $token, $images, $business_des
return $dummy_products_to_update;
}

if ( empty( $dummy_products_to_update ) ) {
return array(
'product_content' => array(),
);
}

$products_information_list = $this->assign_ai_selected_images_to_dummy_products( $dummy_products_to_update, $images );

return $this->assign_ai_generated_content_to_dummy_products( $ai_connection, $token, $products_information_list, $business_description );
Expand All @@ -111,7 +117,6 @@ public function fetch_dummy_products_to_update() {
$dummy_products = $this->fetch_product_ids( 'dummy' );
$dummy_products_count = count( $dummy_products );
$products_to_create = max( 0, 6 - $real_products_count - $dummy_products_count );

while ( $products_to_create > 0 ) {
$this->create_new_product( self::DUMMY_PRODUCTS[ $products_to_create - 1 ] );
$products_to_create--;
Expand Down Expand Up @@ -300,40 +305,50 @@ public function update_product_content( $ai_generated_product_content ) {
return;
}

wp_update_post(
array(
'ID' => $product->get_id(),
'post_title' => $ai_generated_product_content['title'],
'post_content' => $ai_generated_product_content['description'],
'post_name' => sanitize_title( $ai_generated_product_content['title'] ),
'meta_input' => array(
'_regular_price' => $ai_generated_product_content['price'],
),
)
);
$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'] );
$product->set_slug( sanitize_title( $ai_generated_product_content['title'] ) );
$product->save();

if ( ! empty( $ai_generated_product_content['image']['src'] ) ) {
require_once ABSPATH . 'wp-admin/includes/media.php';
require_once ABSPATH . 'wp-admin/includes/file.php';
require_once ABSPATH . 'wp-admin/includes/image.php';
$update_product_image = $this->update_product_image( $product, $ai_generated_product_content );

// Since the media_sideload_image function is expensive and can take longer to complete
// the process of downloading the external image and uploading it to the media library,
// here we are increasing the time limit to avoid any issues.
set_time_limit( 150 );
wp_raise_memory_limit( 'image' );
if ( is_wp_error( $update_product_image ) ) {
return $update_product_image;
}

$product_image_id = media_sideload_image( $ai_generated_product_content['image']['src'], $product->get_id(), $ai_generated_product_content['image']['alt'], 'id' );
$this->create_hash_for_ai_modified_product( $product );
}

if ( is_wp_error( $product_image_id ) ) {
return $product_image_id->get_error_message();
}
/**
* Update the product images with the AI-generated image.
*
* @param \WC_Product $product The product.
* @param array $ai_generated_product_content The AI-generated product content.
*
* @return string|true
*/
public function update_product_image( $product, $ai_generated_product_content ) {
require_once ABSPATH . 'wp-admin/includes/media.php';
require_once ABSPATH . 'wp-admin/includes/file.php';
require_once ABSPATH . 'wp-admin/includes/image.php';

// Since the media_sideload_image function is expensive and can take longer to complete
// the process of downloading the external image and uploading it to the media library,
// here we are increasing the time limit to avoid any issues.
set_time_limit( 150 );
wp_raise_memory_limit( 'image' );

$product->set_image_id( $product_image_id );
$product->save();
$product_image_id = media_sideload_image( $ai_generated_product_content['image']['src'], $product->get_id(), $ai_generated_product_content['image']['alt'], 'id' );

if ( is_wp_error( $product_image_id ) ) {
return $product_image_id->get_error_message();
}

$this->create_hash_for_ai_modified_product( $product );
$product->set_image_id( $product_image_id );
$product->save();

return true;
}

/**
Expand Down

0 comments on commit d51816f

Please sign in to comment.