From 582e8574a2d8d601ca5453eb2397bcf49e2e68d2 Mon Sep 17 00:00:00 2001 From: Mike Moore Date: Tue, 6 Aug 2024 16:35:27 -0400 Subject: [PATCH] Tokenized Cart - update customer ID when user logs in (#9218) --- .../fix-9145-tokenized-cart-customer-id-mismatch | 5 +++++ ...ss-wc-payments-payment-request-session-handler.php | 11 ++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 changelog/fix-9145-tokenized-cart-customer-id-mismatch diff --git a/changelog/fix-9145-tokenized-cart-customer-id-mismatch b/changelog/fix-9145-tokenized-cart-customer-id-mismatch new file mode 100644 index 00000000000..34dd0a9da8a --- /dev/null +++ b/changelog/fix-9145-tokenized-cart-customer-id-mismatch @@ -0,0 +1,5 @@ +Significance: patch +Type: fix +Comment: Fix for unreleased tokenized cart feature + + diff --git a/includes/class-wc-payments-payment-request-session-handler.php b/includes/class-wc-payments-payment-request-session-handler.php index 0e1d5673dc0..ff19ad708c4 100644 --- a/includes/class-wc-payments-payment-request-session-handler.php +++ b/includes/class-wc-payments-payment-request-session-handler.php @@ -66,10 +66,19 @@ public function init_session_cookie() { // If an account has been created after the session has been initialized, update the session. // This method is called directly by WC blocks when an account is created right before placing an order. $previous_session_data = null; + parent::init_session_cookie(); + if ( is_user_logged_in() && strval( get_current_user_id() ) !== $this->_customer_id ) { $previous_session_data = $this->_data; + /** + * This is borrowed from WooCommerce core, which also converts the user ID to a string. + * https://github.com/woocommerce/woocommerce/blob/f01e9452045e2d483649670adc2f042391774e38/plugins/woocommerce/includes/class-wc-session-handler.php#L107 + * + * @psalm-suppress InvalidPropertyAssignmentValue + */ + $this->_customer_id = strval( get_current_user_id() ); } - parent::init_session_cookie(); + $this->init_session_from_token(); if ( ! empty( $previous_session_data ) ) { $this->_data = $previous_session_data;