Skip to content

Commit

Permalink
Fix yithemes#25: Product duplicates in wishlist after user login
Browse files Browse the repository at this point in the history
  • Loading branch information
ankhy-dev committed Jun 9, 2023
1 parent 4300f26 commit 40c3211
Showing 1 changed file with 35 additions and 17 deletions.
52 changes: 35 additions & 17 deletions includes/data-stores/class-yith-wcwl-wishlist-data-store.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) . '%';
Expand Down Expand Up @@ -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
)
)';
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)
);
}
}
}

Expand Down

0 comments on commit 40c3211

Please sign in to comment.