From 8fa2797a6ff1a14c5118146e39f08783d4e8f4ec Mon Sep 17 00:00:00 2001 From: Alex Florisca Date: Tue, 29 Oct 2024 14:54:26 +0000 Subject: [PATCH] Check for valid session object before calling get_customer_id in Cart Route (#52410) * Check for valid session object before calling get_customer_id in Cart Route * Add changefile(s) from automation for the following project(s): woocommerce * Harden get_cart_token function to make sure the cart is loaded before we access the session --------- Co-authored-by: github-actions --- .../changelog/52410-fix-52326-jwt-error-when-no-session | 4 ++++ .../src/StoreApi/Routes/V1/AbstractCartRoute.php | 7 +++++++ 2 files changed, 11 insertions(+) create mode 100644 plugins/woocommerce/changelog/52410-fix-52326-jwt-error-when-no-session diff --git a/plugins/woocommerce/changelog/52410-fix-52326-jwt-error-when-no-session b/plugins/woocommerce/changelog/52410-fix-52326-jwt-error-when-no-session new file mode 100644 index 00000000000..621a79272fa --- /dev/null +++ b/plugins/woocommerce/changelog/52410-fix-52326-jwt-error-when-no-session @@ -0,0 +1,4 @@ +Significance: patch +Type: fix + +Make sure session exists before calling its functions in the Cart StoreApi route \ No newline at end of file diff --git a/plugins/woocommerce/src/StoreApi/Routes/V1/AbstractCartRoute.php b/plugins/woocommerce/src/StoreApi/Routes/V1/AbstractCartRoute.php index e26cb056934..68557bbca2a 100644 --- a/plugins/woocommerce/src/StoreApi/Routes/V1/AbstractCartRoute.php +++ b/plugins/woocommerce/src/StoreApi/Routes/V1/AbstractCartRoute.php @@ -188,6 +188,13 @@ abstract class AbstractCartRoute extends AbstractRoute { * @return string */ protected function get_cart_token() { + // Ensure cart is loaded. + $this->cart_controller->load_cart(); + + if ( ! wc()->session ) { + return null; + } + return JsonWebToken::create( [ 'user_id' => wc()->session->get_customer_id(),