From 4a725f3f3e69ed0443411ddfb23cb52bb3c75350 Mon Sep 17 00:00:00 2001 From: Tatu <17417945+rekkitatu@users.noreply.github.com> Date: Mon, 13 May 2019 02:14:52 +0300 Subject: [PATCH 1/9] Delete guest session after user logs in Logging in to the user account makes a copy of the guest session and possible cart content. This causes conflicts in situations where there are only single quantities of products left and real time product availability checks are in place. While this kind of single quantity items are at time in two carts for the same visitor the checking of product availability fails. Deleting the same visitors guest cart solves this issue. --- includes/class-wc-session-handler.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/includes/class-wc-session-handler.php b/includes/class-wc-session-handler.php index 4d2a8a9e3162..8b7751cecddd 100644 --- a/includes/class-wc-session-handler.php +++ b/includes/class-wc-session-handler.php @@ -94,9 +94,10 @@ public function init_session_cookie() { // If the user logs in, update session. if ( is_user_logged_in() && get_current_user_id() !== $this->_customer_id ) { + $guest_session_id = $this->_customer_id; $this->_customer_id = get_current_user_id(); $this->_dirty = true; - $this->save_data(); + $this->save_data($guest_session_id); $this->set_customer_session_cookie( true ); } @@ -236,7 +237,7 @@ private function get_cache_prefix() { /** * Save data. */ - public function save_data() { + public function save_data($old_session_key = false) { // Dirty if something changed - prevents saving nothing new. if ( $this->_dirty && $this->has_session() ) { global $wpdb; @@ -253,6 +254,9 @@ public function save_data() { wp_cache_set( $this->get_cache_prefix() . $this->_customer_id, $this->_data, WC_SESSION_CACHE_GROUP, $this->_session_expiration - time() ); $this->_dirty = false; + if (get_current_user_id() !== $old_session_key) { + $this->delete_session( $old_session_key ); + } } } From 4266cc9eaa0862bd1ff2f767187313cfa9349fdb Mon Sep 17 00:00:00 2001 From: Tatu <17417945+rekkitatu@users.noreply.github.com> Date: Mon, 13 May 2019 12:27:01 +0300 Subject: [PATCH 2/9] Update class-wc-session-handler.php --- includes/class-wc-session-handler.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/class-wc-session-handler.php b/includes/class-wc-session-handler.php index 8b7751cecddd..70ba41d8e2f8 100644 --- a/includes/class-wc-session-handler.php +++ b/includes/class-wc-session-handler.php @@ -97,7 +97,7 @@ public function init_session_cookie() { $guest_session_id = $this->_customer_id; $this->_customer_id = get_current_user_id(); $this->_dirty = true; - $this->save_data($guest_session_id); + $this->save_data( $guest_session_id ); $this->set_customer_session_cookie( true ); } @@ -254,7 +254,7 @@ public function save_data($old_session_key = false) { wp_cache_set( $this->get_cache_prefix() . $this->_customer_id, $this->_data, WC_SESSION_CACHE_GROUP, $this->_session_expiration - time() ); $this->_dirty = false; - if (get_current_user_id() !== $old_session_key) { + if ( get_current_user_id() !== $old_session_key ) { $this->delete_session( $old_session_key ); } } From bfad881f32ffd9ec3878c2498848a5f24fef98f9 Mon Sep 17 00:00:00 2001 From: Tatu <17417945+rekkitatu@users.noreply.github.com> Date: Mon, 13 May 2019 14:27:28 +0300 Subject: [PATCH 3/9] Removes the $old_session_key default value Fix for the issue #23686 --- includes/class-wc-session-handler.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/class-wc-session-handler.php b/includes/class-wc-session-handler.php index 70ba41d8e2f8..22171e0202f0 100644 --- a/includes/class-wc-session-handler.php +++ b/includes/class-wc-session-handler.php @@ -237,7 +237,7 @@ private function get_cache_prefix() { /** * Save data. */ - public function save_data($old_session_key = false) { + public function save_data( $old_session_key ) { // Dirty if something changed - prevents saving nothing new. if ( $this->_dirty && $this->has_session() ) { global $wpdb; From 71e94e9c08ed42d9353161c9d1f973aa2afcdeff Mon Sep 17 00:00:00 2001 From: Tatu <17417945+rekkitatu@users.noreply.github.com> Date: Mon, 13 May 2019 15:20:39 +0300 Subject: [PATCH 4/9] Add mention of $old_session_key parameter --- includes/class-wc-session-handler.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/includes/class-wc-session-handler.php b/includes/class-wc-session-handler.php index 22171e0202f0..77487b0db53b 100644 --- a/includes/class-wc-session-handler.php +++ b/includes/class-wc-session-handler.php @@ -235,9 +235,11 @@ private function get_cache_prefix() { } /** - * Save data. + * Save data and delete guest session. + * + * @param int $old_session_key session ID before user logs in. */ - public function save_data( $old_session_key ) { + public function save_data( $old_session_key = 0 ) { // Dirty if something changed - prevents saving nothing new. if ( $this->_dirty && $this->has_session() ) { global $wpdb; From 44c6d0b36ccb561cb7c01dd9784df8a613fcff48 Mon Sep 17 00:00:00 2001 From: Tatu <17417945+rekkitatu@users.noreply.github.com> Date: Tue, 14 May 2019 11:24:21 +0300 Subject: [PATCH 5/9] Compatibility for persistent cart disabled case If woocommerce_persistent_cart_enabled filter was set to false the cart would be emptied while logged in. This fixes also that case. Now the guest session is removed from the woocommerce_sessions table after successful login whether the persistent cart is set enabled or disabled and the guest session cart is carried over to logged in user session. --- includes/class-wc-session-handler.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/class-wc-session-handler.php b/includes/class-wc-session-handler.php index 77487b0db53b..2fca04ec2a9d 100644 --- a/includes/class-wc-session-handler.php +++ b/includes/class-wc-session-handler.php @@ -256,7 +256,7 @@ public function save_data( $old_session_key = 0 ) { wp_cache_set( $this->get_cache_prefix() . $this->_customer_id, $this->_data, WC_SESSION_CACHE_GROUP, $this->_session_expiration - time() ); $this->_dirty = false; - if ( get_current_user_id() !== $old_session_key ) { + if ( (bool) get_user_meta( get_current_user_id(), '_woocommerce_load_saved_cart_after_login', true ) && get_current_user_id() !== $old_session_key ) { $this->delete_session( $old_session_key ); } } From 968a0561bb992e50af345cc528bd71a52f9ab6d0 Mon Sep 17 00:00:00 2001 From: Tatu <17417945+rekkitatu@users.noreply.github.com> Date: Tue, 14 May 2019 12:47:06 +0300 Subject: [PATCH 6/9] Add additional check for cart persistence --- includes/class-wc-session-handler.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/includes/class-wc-session-handler.php b/includes/class-wc-session-handler.php index 2fca04ec2a9d..83a5038e6e6d 100644 --- a/includes/class-wc-session-handler.php +++ b/includes/class-wc-session-handler.php @@ -256,7 +256,8 @@ public function save_data( $old_session_key = 0 ) { wp_cache_set( $this->get_cache_prefix() . $this->_customer_id, $this->_data, WC_SESSION_CACHE_GROUP, $this->_session_expiration - time() ); $this->_dirty = false; - if ( (bool) get_user_meta( get_current_user_id(), '_woocommerce_load_saved_cart_after_login', true ) && get_current_user_id() !== $old_session_key ) { + $allow_guest_session_removal = apply_filters( 'woocommerce_persistent_cart_enabled', true ) ? true : (bool) get_user_meta( get_current_user_id(), '_woocommerce_load_saved_cart_after_login', true ); + if ( $allow_guest_session_removal && get_current_user_id() !== $old_session_key ) { $this->delete_session( $old_session_key ); } } From c52e7bd75ab2a407419be18883cb8465e1638785 Mon Sep 17 00:00:00 2001 From: Tatu <17417945+rekkitatu@users.noreply.github.com> Date: Thu, 11 Jul 2019 23:40:56 +0300 Subject: [PATCH 7/9] Dropped redundant persistent cart check code Changing the not equal check between current_user_id and old_session_key to loose type allowed leaving out persistent cart related check. --- includes/class-wc-session-handler.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/includes/class-wc-session-handler.php b/includes/class-wc-session-handler.php index 83a5038e6e6d..32e36c38f721 100644 --- a/includes/class-wc-session-handler.php +++ b/includes/class-wc-session-handler.php @@ -239,7 +239,7 @@ private function get_cache_prefix() { * * @param int $old_session_key session ID before user logs in. */ - public function save_data( $old_session_key = 0 ) { + public function save_data( $old_session_key ) { // Dirty if something changed - prevents saving nothing new. if ( $this->_dirty && $this->has_session() ) { global $wpdb; @@ -256,8 +256,7 @@ public function save_data( $old_session_key = 0 ) { wp_cache_set( $this->get_cache_prefix() . $this->_customer_id, $this->_data, WC_SESSION_CACHE_GROUP, $this->_session_expiration - time() ); $this->_dirty = false; - $allow_guest_session_removal = apply_filters( 'woocommerce_persistent_cart_enabled', true ) ? true : (bool) get_user_meta( get_current_user_id(), '_woocommerce_load_saved_cart_after_login', true ); - if ( $allow_guest_session_removal && get_current_user_id() !== $old_session_key ) { + if ( get_current_user_id() != $old_session_key ) { $this->delete_session( $old_session_key ); } } From d9254cedb23a01de856c6e30f2b5284b50d3c367 Mon Sep 17 00:00:00 2001 From: Tatu <17417945+rekkitatu@users.noreply.github.com> Date: Fri, 12 Jul 2019 22:53:01 +0300 Subject: [PATCH 8/9] Prevent conflict with multiple logged in users Previous code would delete the session of previously logged in user in the rare case that multiple users would be logged in on the same browser. Added check to see if there is user account exists with the old_session_key and if not then the old session is from a guest user which can be deleted from database during logging in. Also returned old_session_key as optional parameter to prevent issues. --- includes/class-wc-session-handler.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/class-wc-session-handler.php b/includes/class-wc-session-handler.php index 32e36c38f721..ce581b237167 100644 --- a/includes/class-wc-session-handler.php +++ b/includes/class-wc-session-handler.php @@ -239,7 +239,7 @@ private function get_cache_prefix() { * * @param int $old_session_key session ID before user logs in. */ - public function save_data( $old_session_key ) { + public function save_data( $old_session_key = 0 ) { // Dirty if something changed - prevents saving nothing new. if ( $this->_dirty && $this->has_session() ) { global $wpdb; @@ -256,7 +256,7 @@ public function save_data( $old_session_key ) { wp_cache_set( $this->get_cache_prefix() . $this->_customer_id, $this->_data, WC_SESSION_CACHE_GROUP, $this->_session_expiration - time() ); $this->_dirty = false; - if ( get_current_user_id() != $old_session_key ) { + if ( get_current_user_id() != $old_session_key && !is_object( get_user_by( 'id', $old_session_key ) ) ) { $this->delete_session( $old_session_key ); } } From ed7f98a584a06c157b648fcc66f6bd72ab889b8e Mon Sep 17 00:00:00 2001 From: Tatu <17417945+rekkitatu@users.noreply.github.com> Date: Fri, 12 Jul 2019 23:18:29 +0300 Subject: [PATCH 9/9] Coding standards fix --- includes/class-wc-session-handler.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/class-wc-session-handler.php b/includes/class-wc-session-handler.php index ce581b237167..9ad390645fee 100644 --- a/includes/class-wc-session-handler.php +++ b/includes/class-wc-session-handler.php @@ -256,7 +256,7 @@ public function save_data( $old_session_key = 0 ) { wp_cache_set( $this->get_cache_prefix() . $this->_customer_id, $this->_data, WC_SESSION_CACHE_GROUP, $this->_session_expiration - time() ); $this->_dirty = false; - if ( get_current_user_id() != $old_session_key && !is_object( get_user_by( 'id', $old_session_key ) ) ) { + if ( get_current_user_id() != $old_session_key && ! is_object( get_user_by( 'id', $old_session_key ) ) ) { $this->delete_session( $old_session_key ); } }