0

I'm trying to add an item to cart inside a WordPress REST API.

This is my code so far:

add_action( 'rest_api_init', function () {
  register_rest_route( 'lufc/v1', '/add-to-cart', array(
    'methods' => 'GET',
    'callback' => [ 'add_to_cart' ],
  ) );
} );

function add_to_cart() {
  global $woocommerce;
  $woocommerce->cart->add_to_cart( 15 );
  die();
}

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

Any suggestions?

3

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' ]
    
  • 1
    Excellent. This works perfectly in my implantation. Thank you for your help. – Jack Robson Jun 26 at 14:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.