i am converting a woocommerce store into phonegap app. Now i am stuck. how to add product into cart and display cart into the phonegap app. So how to do it with the help of rest api.
Thanks in advance
This related GitHub issue might help.
It seems that there will be no API for sessions or cart. It is suggested to use the order endpoint if you want to use the API. There is a way to add a product to the cart without the API though, you can do that with an URL like: http://yourproducturl.com/checkout/?add-to-cart=%ID%.
Some years later, we have a working REST API endpoint at /wp-json/wc/store/cart/add-item.
It requires a product or variant id and a quantity. It also requires a X-WC-Store-API-Nonce header that is set to what wordpress gives you when calling wp_create_nonce('wc_store_api'). You can even remove items with a negative quantity, neat.
Here is a working jQuery example for my variant id 378:
<script>
const _nonce = "<?= wp_create_nonce('wc_store_api') ?>";
$.ajax({
type: 'POST',
url: '/wp-json/wc/store/cart/add-item',
dataType: 'json',
headers: {
'X-WC-Store-API-Nonce': _nonce
},
data: {
id: 378,
quantity: 1
}
});
</script>
Tested on WooCommerce 4.5.2, 4.6.1.
WooCommerce docs
GitHub thread
on Wordpress 5.4.1, Woocommerce 4.1.0 you have endpoints under this namespace for managing cart and others
https://yoursite.com/wp-json/wc/store/
check it out.
Just in case someone finds this, there is CoCart available in the meantime which solves OPs problem: https://wordpress.org/plugins/cart-rest-api-for-woocommerce/