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.
This commit is contained in:
Tatu 2019-05-13 02:14:52 +03:00 committed by GitHub
parent f4e7ffd180
commit 4a725f3f3e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 6 additions and 2 deletions

View File

@ -94,9 +94,10 @@ class WC_Session_Handler extends WC_Session {
// If the user logs in, update session. // If the user logs in, update session.
if ( is_user_logged_in() && get_current_user_id() !== $this->_customer_id ) { 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->_customer_id = get_current_user_id();
$this->_dirty = true; $this->_dirty = true;
$this->save_data(); $this->save_data($guest_session_id);
$this->set_customer_session_cookie( true ); $this->set_customer_session_cookie( true );
} }
@ -236,7 +237,7 @@ class WC_Session_Handler extends WC_Session {
/** /**
* Save data. * Save data.
*/ */
public function save_data() { public function save_data($old_session_key = false) {
// Dirty if something changed - prevents saving nothing new. // Dirty if something changed - prevents saving nothing new.
if ( $this->_dirty && $this->has_session() ) { if ( $this->_dirty && $this->has_session() ) {
global $wpdb; global $wpdb;
@ -253,6 +254,9 @@ class WC_Session_Handler extends WC_Session {
wp_cache_set( $this->get_cache_prefix() . $this->_customer_id, $this->_data, WC_SESSION_CACHE_GROUP, $this->_session_expiration - time() ); wp_cache_set( $this->get_cache_prefix() . $this->_customer_id, $this->_data, WC_SESSION_CACHE_GROUP, $this->_session_expiration - time() );
$this->_dirty = false; $this->_dirty = false;
if (get_current_user_id() !== $old_session_key) {
$this->delete_session( $old_session_key );
}
} }
} }