[CYS] Fix regression and ensure AI-generated content is assigned to products after the third attempt (https://github.com/woocommerce/woocommerce-blocks/pull/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>
This commit is contained in:
Patricia Hillebrandt 2023-12-04 09:58:01 +01:00 committed by GitHub
parent af23649365
commit 5f4c8e2f7c
1 changed files with 45 additions and 30 deletions

View File

@ -88,6 +88,12 @@ class ProductUpdater {
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 );
@ -111,7 +117,6 @@ class ProductUpdater {
$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--;
@ -300,19 +305,30 @@ class ProductUpdater {
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'] ) ) {
$update_product_image = $this->update_product_image( $product, $ai_generated_product_content );
if ( is_wp_error( $update_product_image ) ) {
return $update_product_image;
}
$this->create_hash_for_ai_modified_product( $product );
}
/**
* 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';
@ -331,9 +347,8 @@ class ProductUpdater {
$product->set_image_id( $product_image_id );
$product->save();
}
$this->create_hash_for_ai_modified_product( $product );
return true;
}
/**