Skip to content

Commit

Permalink
Merge pull request #23687 from rekkitatu/master
Browse files Browse the repository at this point in the history
Delete guest cart session if user logs in, solves #23686
  • Loading branch information
kloon committed Jul 15, 2019
2 parents 7c0d025 + ed7f98a commit 043fd47
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions includes/class-wc-session-handler.php
Expand Up @@ -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 );
}

Expand Down Expand Up @@ -234,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() {
public function save_data( $old_session_key = 0 ) {
// Dirty if something changed - prevents saving nothing new.
if ( $this->_dirty && $this->has_session() ) {
global $wpdb;
Expand All @@ -253,6 +256,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 && ! is_object( get_user_by( 'id', $old_session_key ) ) ) {
$this->delete_session( $old_session_key );
}
}
}

Expand Down

0 comments on commit 043fd47

Please sign in to comment.