1
votes

I am new to WooCommerce. I want to add products to user's own cart. I googled a lot for this but it seems cart API is not added in WooCommerce default API. So I added plugin called CoCart to my WooCommerce Admin. Now I am able to add product to cart but I don't for which user product is being added into cart and how can I retrieve cart items for particular user. If its adding product to particular user session then how I can manage it from Mobile Application. Also from Mobile Application I am able to add product to cart but again same issue that for which user's cart it is being added? Whenever I retrieve items from cart it sends me blank json array [].

Here are the APIs I am using:

1. Add Product to cart:

URL: https://www.my-domain.com/wp-json/wc/v2/cart/add

Method: POST

Parameters: {
  "product_id": "1111",
  "quantity": "1"
} 

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

This API respond like this in postman and from Mobile app too.

2. Retrieve cart items:

URL: https://www.my-domain.com/wp-json/wc/v2/cart

Method: GET

Response: []

This API returns [] blank JSON array from Mobile application and from Postman it returns array of products from cart.

I don't know what is issue here. May be it works for session from web but what about mobile application? How can I use this APIs to work on Mobile platform.

  1. How can I make my cart user specific using CoCart plugin?
  2. If it requires to manage user session, how can I manage session for users from mobile app?

Thanks!

1

1 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 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
            })

Thanks!