Clear global_unique_id when restoring a product with an existing global_unique_id (#50496)

* Clear global_unique_id when restoring a product that doesn't have an unique id

* Check if product exists before calling methods
This commit is contained in:
Nathan Silveira 2024-08-10 08:16:07 -03:00 committed by GitHub
parent 10bc399c07
commit 9297409c5a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 18 additions and 0 deletions

View File

@ -0,0 +1,4 @@
Significance: minor
Type: update
Clear global_unique_id when restoring a product that doesn't have an unique id

View File

@ -405,6 +405,7 @@ class WC_Post_Data {
$data_store->untrash_variations( $id );
wc_product_force_unique_sku( $id );
self::clear_global_unique_id_if_necessary( $id );
wc_get_container()->get( ProductAttributesLookupDataStore::class )->on_product_changed( $id );
} elseif ( 'product_variation' === $post_type ) {
@ -412,6 +413,19 @@ class WC_Post_Data {
}
}
/**
* Clear global unique id if it's not unique.
*
* @param mixed $id Post ID.
*/
private static function clear_global_unique_id_if_necessary( $id ) {
$product = wc_get_product( $id );
if ( $product && ! wc_product_has_global_unique_id( $id, $product->get_global_unique_id() ) ) {
$product->set_global_unique_id( '' );
$product->save();
}
}
/**
* Get the post type for a given post.
*