Skip to content

Commit

Permalink
Clear global_unique_id when restoring a product with an existing glob…
Browse files Browse the repository at this point in the history
…al_unique_id (woocommerce#50496)

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

* Check if product exists before calling methods
  • Loading branch information
nathanss authored Aug 10, 2024
1 parent 10bc399 commit 9297409
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
4 changes: 4 additions & 0 deletions plugins/woocommerce/changelog/update-clear-unique-id
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: update

Clear global_unique_id when restoring a product that doesn't have an unique id
14 changes: 14 additions & 0 deletions plugins/woocommerce/includes/class-wc-post-data.php
Original file line number Diff line number Diff line change
Expand Up @@ -405,13 +405,27 @@ public static function untrash_post( $id ) {
$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 ) {
wc_get_container()->get( ProductAttributesLookupDataStore::class )->on_product_changed( $id );
}
}

/**
* 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.
*
Expand Down

0 comments on commit 9297409

Please sign in to comment.