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(),