$cart = WC()->cart gives Uncaught Error: Call to a member function get_cart() on null

function tars_agg_part_load_charge() {
    
    include_once WC_ABSPATH . 'includes/wc-cart-functions.php';
    include_once WC_ABSPATH . 'includes/class-wc-cart.php';

    $cart = WC()->cart;
    
    if(empty($cart)){
        
        return true; //this is insane because the $cart object is populated but for some reason it thinks its null, so we're just returning true
    }

    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
        return 'admin';
    }

    $part_load_tax = 0;

    foreach ( $cart->get_cart() as $cart_item ) {
        if ( tars_is_delivery( $cart_item['product_id'] ) && $cart_item['quantity'] < 16 ) {
            $charge = tars_get_part_load_charge( $cart_item['tars_delivery_data']['address_postcode'] );
            $data = $cart_item['data']->get_data();
            $product_name = $data['name'];
            $cart->add_fee( __('Part Load Charge '.$product_name, 'woocommerce'), intval($charge) );

            $part_load_tax = 57.6; // Set part load tax for applicable items
        }
    }

    foreach ( $cart->taxes as $tax_rate => &$tax ) {
        $tax += $part_load_tax; // Apply part load tax to existing taxes
    }

    return $part_load_tax;
}

I have an issue with my woocommerce cart. For some reason, $cart = WC()->cart; is apparently empty, but if I var_dump it, I get a massive object, so it’s not empty. I can’t run the rest of the code, because if I remove the if(empty($cart)){return true; } part of the code, then I get the error:

Uncaught Error: Call to a member function get_cart() on null...

So somehow $cart exists and doesn’t exist at the same time? Looking around, there was some advice about the cart only being initialised on the front end and I should include include_once WC_ABSPATH . 'includes/wc-cart-functions.php'; include_once WC_ABSPATH . 'includes/class-wc-cart.php', but this has not helped. How can Initialise and use the cart in my code? Hooks don’t work, as I need to return a variable.