-2

Is there any woocommerce API for add to cart So that I can add product to cart via mobile app.

Please help me.

2 Answers2

2

Did not found any "add to cart" functionality in the woocommerce api.

What had been done is that my "add to cart" button will save the product id and its quantity(and other properties) to the shared preferences object.

Firstly getting the products json object from the already shared preferences object and find out that the to-be-added product already is present on the shared preferences object.If it is present already then the execution returns.Otherwise it will advance to add the product property to the shared preferences object.

                SharedPreferences pref = getSharedPreferences("CartPref", 0);
                String strJson = pref.getString("productCartJson","[]");
                JSONArray productsSaveDetailJsonArray = new JSONArray(strJson);

                //checking if the product-to-be-added is already present in the shared preferences object
                for(int i=0;i<productsSaveDetailJsonArray.length();i++){
                    if(productsSaveDetailJsonArray.getJSONObject(i).getString("product_id").contentEquals(productIdForDetailsPage)){
                        strJson = pref.getString("productCartJson","0");
                        Log.d("strJson",""+strJson);
                        //if already present then returns.
                        return;
                    }
                }
                JSONObject productSaveDetailJsonObject = new JSONObject();
                productSaveDetailJsonObject.put("product_id",""+productIdForDetailsPage);
                productSaveDetailJsonObject.put("quantity","1");

                productsSaveDetailJsonArray.put(productSaveDetailJsonObject);
                SharedPreferences.Editor editor = pref.edit();
                editor.putString("productCartJson", ""+productsSaveDetailJsonArray);
                editor.apply();
                strJson = pref.getString("productCartJson","0");
                Log.d("strJson",""+strJson);

If any doubt please comment.

This works for me.Hope it works for you as well.

debo.stackoverflow
  • 911
  • 1
  • 10
  • 30
0

Please find the following steps to achieve this:

  1. Get the persistent cart. It may be _woocommerce_persistent_cart or _woocommerce_persistent_cart_1; check in the user_meta table.
  2. Create a new cart object.
  3. Add the old cart data to the newly created cart object.
  4. Add the product and quantities coming in from the request to the new cart object.
  5. If there is a current session cart, overwrite it with the new cart.
  6. Update the wp_session table with the updated cart data.
  7. Overwrite the persistent cart with the new cart data.

Please check my answer to the following question with integration of these steps in working code:

Rest API to store products in cart based on the user id in woocommerce

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
Karan Batra
  • 161
  • 1
  • 2
  • 10