18

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

4 Answers 4

15

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

Sign up to request clarification or add additional context in comments.

10 Comments

and what about removing, listing etc? do we have to write the endpoints ourselves?
Did you ever figure out how to add and remove line items from your order?
@MichaelLynch You can edit an order via the official API. See woocommerce.github.io/woocommerce-rest-api-docs/…
@VictorFerreira There is no official API for sessions or cart. I'm not sure any 3rd party library that offers that, otherwise you need to implement this yourself. But you can dig into the WooCommerce code base to see how it is done.
Thans a lot bro
|
11

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

4 Comments

Any idea on how to attach this cart to a customer that already exists? i.e customer id=5
If need to add multiple item instead of single item, how can we do that using this api @Ti ?
@AspDev18 Multiple different items seem to need multiple POSTs. For a single item simply increase the quantity. Sadly there is still no documentation on the cart endpoint.
@TiHausmann, I found the way, we can use batch api site-domain/wp-json/wc/store/v1/batch , For documentation , you can visit this link github.com/woocommerce/woocommerce-blocks/blob/trunk/src/…
10

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.

2 Comments

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
you can add to cart, or remove from cart, or even apply coupon, when you open yours like this example yoursite.com/wp-json/wc/store expand the endpoints, you'll see the allowed methods, and the payload expected.
3

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/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.