diff --git a/plugins/woocommerce/changelog/update-clear-unique-id b/plugins/woocommerce/changelog/update-clear-unique-id new file mode 100644 index 00000000000..4da0b7f4abe --- /dev/null +++ b/plugins/woocommerce/changelog/update-clear-unique-id @@ -0,0 +1,4 @@ +Significance: minor +Type: update + +Clear global_unique_id when restoring a product that doesn't have an unique id diff --git a/plugins/woocommerce/includes/class-wc-post-data.php b/plugins/woocommerce/includes/class-wc-post-data.php index c1e3aa6b5f0..f1563c5d976 100644 --- a/plugins/woocommerce/includes/class-wc-post-data.php +++ b/plugins/woocommerce/includes/class-wc-post-data.php @@ -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. *