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_nopriv_*, wp_ajax_* and whatnot).

The problem is when I try to use the WooCommerce API (not the REST API, just the regular PHP API).

I'm calling:

$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 WC() instead.

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.

closed as off-topic by fuxia May 7 '16 at 18:12

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.


Finally solved the problem. The item wasn't being added because the product didn't have a price. That's all.

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