I'm working on a Woocommerce SPA (Single-Page Application) and I'm trying to add a product to Woocommerce's cart but it just doesn't work.
I'm doing this in a custom plugin. In a function that responds to an AJAX call using Wordpress' regular workflow for AJAX calls (
wp_ajax_* and whatnot).
The problem is when I try to use the WooCommerce API (not the REST API, just the regular PHP API).
$string = $woocommerce->cart->add_to_cart($id);
And I get
false as a response.
I'm also calling:
$response['cart'] = $woocommerce->cart->get_cart();
And I get an empty array in response.
I do have
global $woocommerce; at the top of that function and I also tried using
None of that worked.
Tried clearing all caches too using WP Super Cache. Still nothing.
Keep in mind I'm quite inexperienced in both WP and WC, so I might be committing some very elementary mistake.
Thanks for your time.