1
votes

I am new to WooCommerce REST API. I want to add product to cart and list all products of cart. I have added Co-Cart plugin to my server and I am using Co-Cart APIs for cart related functionality.

Issue I am facing is, I am not able to view cart products in my react native application. Here are the APIs I am using right now:

1. Add product to cart:

Method:POST

URL: https://www.myhost.com/wp-json/wc/v2/cart/add?token=user_token

Parameteres: { "product_id": "9168", "quantity": "1" }

Response:{ "key": "af086cdab7954f11a518e3af68XXXXX", "product_id": 111, "variation_id": 0, "variation": [], "quantity": 1, "data": {}, "data_hash": "b5c1d5ca8bae6d4896cf1807cdfXXXX", "line_tax_data": { "subtotal": [], "total": [] }, "line_subtotal": 50000, "line_subtotal_tax": 0, "line_total": 50000, "line_tax": 0 }

Getting proper response with status code 200 while adding product to cart from application.

2. View cart content

Method: GET

URL: https://www.myhost.com/wp-json/wc/v2/cart?token=user_token

Response: [] (Blank JSON array)

I don't know where are the products which are added to cart using same user. I don't how it manages session from mobile application or what extra parameters it requires to fetch cart items.

Please let me know where I am going wrong. I am using Co-Cart plugin API only.

**

EDIT

**

fetch(url, {
        method: methodName,
        async: true,
        crossDomain: true,
        headers: new Headers({
          "Content-Type": "application/json",
          "cache-control": "no-cache",
        }),
        body:data
      }).then((response) => {
        const statusCode = response.status;
        console.log("fetchData StatusCode : "+statusCode);
        console.log("fetchData response1 : " + JSON.stringify(response));
        console.log("fetchData response2 : " + response);
        return response.json();
      }).then((responseJson) => {
        console.log("fetchData responseJson : "+responseJson);
        var response = JSON.stringify(responseJson);
        console.log("fetchData responseJson stringify : " + response);
        response = JSON.parse(response);
        if (callBack != null) {
          callBack("", response);
        }
      }).catch((error) => {
        console.log("fetchData Api error : "+error);
        if (callBack != null) {
          console.log("fetchData Api error callBack != null 2");
          callBack("Error", "");
        }
      });

url is a URL to send, data is raw data in JSON string format and method is either GET or POST.

2

2 Answers

1
votes

Here is the answer of my own question.

It is somehow difficult to integrate WooCommerce store into Mobile Application without having prior knowledge of WooCommerce and its working criteria.

The issue was it was not adding products to my logged-in user's cart and that's why it was not getting all the products for that user as products were not added to the user's cart.

The issue why it was not syncing my cart with my current user was for cookie session for that particular user. I need to set cookie of that user in each API call header after login something like this:

fetch(url1, {
              method: methodName,
              async: true,
              crossDomain: true,
              headers: new Headers({
                "Content-Type": "application/json",
                "cache-control": "no-cache",
                "cookie":"cookie value will goes here"
              }),
              body:data1
            })

It just resolved my issue. Hope it helps!

0
votes

I check the documentation, https://co-cart.github.io/co-cart-docs/#view-cart-contents and but nothing wrong with your.

Can you check if the Pretty permalinks in Settings > Permalinks so that the custom endpoints are supported. Default permalinks will not work.