Is there any woocommerce API
for add to cart
So that I can add product to cart via mobile app.
Please help me.
Is there any woocommerce API
for add to cart
So that I can add product to cart via mobile app.
Please help me.
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.
Please find the following steps to achieve this:
_woocommerce_persistent_cart
or _woocommerce_persistent_cart_1
; check in the user_meta
table.wp_session
table with the updated 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