Troubleshooting a PHP Code Block: Unexpected JSON Token Error

I’ve created a page from scratch. I inserted a Section and a Code Block. The code block contains the following PHP code:

<?php
if (function_exists('WC')) {
    echo WC()->cart->get_cart_total();
}
?>

This seems quite legitimate. However, I’m unable to save the page due to the following error:
Publishing failed. Unexpected token 'E', "Error: Cal"... is not valid JSON

EDIT:

The code in the header returns a Cwicly error but the frontend display is ok with the correct amount and format.

Environment info

  • WordPress version: 6.2.2
  • Cwicly Plugin version: 1.2.9.7.2.1

The cart doesn’t exist on the backend, so probably this can be resolved with a simple null check:

<?php
if (function_exists('WC')) {
    if ( !is_null( WC()->cart ) ) {
        echo WC()->cart->get_cart_total();
    }
}
?>

Obviously this can be simplified to put all the conditions into a single if statement if the code remains this straightforward.

Thank you @StrangeTech for support.
The code runs well and the error is no longer displayed.