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..55f58ed 100644 --- a/includes/data-stores/class-yith-wcwl-wishlist-data-store.php +++ b/includes/data-stores/class-yith-wcwl-wishlist-data-store.php @@ -597,20 +597,20 @@ public function query( $args = array() ) { } if ( ! empty( $s ) ) { - $sql .= ' AND ( - ( - umn.`meta_key` = %s AND - ums.`meta_key` = %s AND - ( + $sql .= ' AND ( + ( + umn.`meta_key` = %s AND + ums.`meta_key` = %s AND + ( u.`user_email` LIKE %s OR u.`user_login` LIKE %s OR umn.`meta_value` LIKE %s OR ums.`meta_value` LIKE %s - ) - ) OR - l.wishlist_name LIKE %s OR - l.wishlist_slug LIKE %s OR - l.wishlist_token LIKE %s + ) + ) OR + l.wishlist_name LIKE %s OR + l.wishlist_slug LIKE %s OR + l.wishlist_token LIKE %s )'; $search_value = '%' . esc_sql( $s ) . '%'; @@ -753,13 +753,13 @@ public function search_users( $args = array() ) { $sql_args = array( 0 ); if ( ! empty( $search ) ) { - $sql .= ' AND ( - umn.`meta_key` = %s AND - ums.`meta_key` = %s AND - ( - u.`user_email` LIKE %s OR - u.`user_login` LIKE %s OR - umn.`meta_value` LIKE %s OR + $sql .= ' AND ( + umn.`meta_key` = %s AND + ums.`meta_key` = %s AND + ( + u.`user_email` LIKE %s OR + u.`user_login` LIKE %s OR + umn.`meta_value` LIKE %s OR ums.`meta_value` LIKE %s ) )'; @@ -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,23 @@ 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) + ); + } } }