Fix handling the error from the `product_image_upload` and `update_product_content` functions (#43793)

* Fix handling the error from the `product_image_upload` function

* Add changefile(s) from automation for the following project(s): woocommerce

* Fix linting errors

---------

Co-authored-by: github-actions <github-actions@github.com>
This commit is contained in:
Alba Rincón 2024-01-22 09:59:51 +01:00 committed by GitHub
parent 7dabf2b031
commit 95ed989ab4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 7 additions and 9 deletions

View File

@ -0,0 +1,4 @@
Significance: patch
Type: update
Return an error instead of a string with the error from the `product_image_upload` and `update_product_content` functions to be able to handle the errors.

View File

@ -275,7 +275,7 @@ class UpdateProducts {
*
* @param array $ai_generated_product_content The AI-generated product content.
*
* @return string|void
* @return void|WP_Error
*/
public function update_product_content( $ai_generated_product_content ) {
if ( ! isset( $ai_generated_product_content['product_id'] ) ) {
@ -295,7 +295,7 @@ class UpdateProducts {
$product_image_id = $this->product_image_upload( $product->get_id(), $ai_generated_product_content['image']['src'], $ai_generated_product_content['image']['alt'] );
if ( is_wp_error( $product_image_id ) ) {
return $product_image_id->get_error_message();
return new \WP_Error( 'error_uploading_image', $product_image_id->get_error_message() );
}
$this->product_update( $product, $product_image_id, $ai_generated_product_content['title'], $ai_generated_product_content['description'], $ai_generated_product_content['price'] );
@ -321,13 +321,7 @@ class UpdateProducts {
set_time_limit( 150 );
wp_raise_memory_limit( 'image' );
$product_image_id = media_sideload_image( $image_src, $product_id, $image_alt, 'id' );
if ( is_wp_error( $product_image_id ) ) {
return $product_image_id->get_error_message();
}
return $product_image_id;
return media_sideload_image( $image_src, $product_id, $image_alt, 'id' );
}
/**