17

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

Shijin TR
  • 7,516
  • 10
  • 55
  • 122
Sanjay Rathod
  • 1,083
  • 5
  • 18
  • 45

4 Answers4

14

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%.

Martin van Driel
  • 1,397
  • 2
  • 12
  • 20
9

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.

tewshi
  • 615
  • 5
  • 9
  • it seems the cart API is added in the new version but there is no documentation for how to use it (add product or remove product) unfortunately – Hamed mayahian Sep 06 '20 at 15:42
  • you can add to cart, or remove from cart, or even apply coupon, when you open yours like this example https://yoursite.com/wp-json/wc/store/ expand the endpoints, you'll see the allowed methods, and the payload expected. – tewshi Sep 17 '20 at 19:59
7

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>
var _nonce = "<?php echo 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) (Github thread)

Ti Hausmann
  • 926
  • 8
  • 21
2

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/

DonMB
  • 2,550
  • 3
  • 28
  • 59