How to insert product in cart through rest api provided by woocommerce?

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

Related posts

Leave a Reply

4 comments

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

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

  3. 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)