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.