$woocommerce->cart is null inside WordPress Rest API

Yes, this is true because WooCommerce cart is only initialized on the front-end (or if it’s a front-end request):

But it fails because $woocommerce->cart is always null.

So in WooCommerce 3.6.4 (current release as of writing) or later, you can manually initialize the cart like so:

// Load cart functions which are loaded only on the front-end.
include_once WC_ABSPATH . 'includes/wc-cart-functions.php';
include_once WC_ABSPATH . 'includes/class-wc-cart.php';

// wc_load_cart() does two things:
// 1. Initialize the customer and cart objects and setup customer saving on shutdown.
// 2. Initialize the session class.
if ( is_null( WC()->cart ) ) {
    wc_load_cart();
}

So your add_to_cart() might look like so:

function add_to_cart() {
    defined( 'WC_ABSPATH' ) || exit;

    // Load cart functions which are loaded only on the front-end.
    include_once WC_ABSPATH . 'includes/wc-cart-functions.php';
    include_once WC_ABSPATH . 'includes/class-wc-cart.php';

    if ( is_null( WC()->cart ) ) {
        wc_load_cart();
    }

    // I'm simply returning the cart item key. But you can return anything you want...
    return WC()->cart->add_to_cart( 15 );
}

Notes

  1. For older WooCommerce 3.6.x releases, this article might help you.

  2. As you could see, the above code is simple (and it worked well for me); however, there’s actually an existing solution that you can try: CoCart.

  3. You should always use WC() to access the global $woocommerce variable/object.

  4. I’m assuming this is just a typo in the question: 'callback' => [ 'add_to_cart' ] because that results in an error, and it should be one of these:

    'callback' => 'add_to_cart'
    'callback' => [ $this, 'add_to_cart' ]
    'callback' => [ $my_class, 'add_to_cart' ]
    'callback' => [ 'My_Class', 'add_to_cart' ]