From 87ed737308a23c02d8997e6b0617c1a74947cc69 Mon Sep 17 00:00:00 2001 From: Andriy Khyliuk Date: Fri, 9 Jun 2023 12:53:59 +0300 Subject: [PATCH] Fix #25: Product duplicates in wishlist after user login --- .../class-yith-wcwl-wishlist-data-store.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/includes/data-stores/class-yith-wcwl-wishlist-data-store.php b/includes/data-stores/class-yith-wcwl-wishlist-data-store.php index 081a374..e38b313 100644 --- a/includes/data-stores/class-yith-wcwl-wishlist-data-store.php +++ b/includes/data-stores/class-yith-wcwl-wishlist-data-store.php @@ -1018,6 +1018,7 @@ public function assign_to_user( $session_id, $user_id ) { if ( ! empty( $items ) ) { $items_string = implode( ',', array_map( 'esc_sql', $items ) ); $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->yith_wcwl_items} SET user_id = %d WHERE ID IN ({$items_string})", $user_id ) ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared + $this->clear_up_duplicate_items($user_id); } // set user id for any session wishlist, and remove session data. @@ -1292,6 +1293,24 @@ protected function clear_caches( &$wishlist ) { wp_cache_delete( 'user-wishlists-' . $session_id, 'wishlists' ); } } + + protected function clear_up_duplicate_items( int $user_id ) + { + global $wpdb; + + $wpdb->query( + $wpdb->prepare('DELETE FROM ' . $wpdb->yith_wcwl_items . + ' WHERE user_id = %1$d AND ID NOT IN ( + SELECT min_id + FROM ( + SELECT user_id, prod_id, wishlist_id, MIN(ID) AS min_id + FROM ' . $wpdb->yith_wcwl_items . + ' WHERE user_id = %1$d + GROUP BY user_id, wishlist_id, prod_id + ) AS subquery + )', $user_id) + ); + } } }