-1

I am developing an android application which is linked with Woo-commerce. I hit API on Postman it added the product and gave total item in cart but when I hit same API with same parameter ,I am getting 0 item in cart. I am adding product through mobile app. I am totally confused. I am using volley library. So I thought it's happening because of cache, so I tried clearing the cache but got same response in Json. Help me. Do I need to integrate Woo-commerce in android. If yes then How ?

[Postman response total item in cart][1]

JSON response on android studio

Add to cart :--
{"status":1,"item_key":"c6a01432c8138d46ba39957a8250e027","cart_content":{"products":[{"id":3129,"title":"Surya Toast-O Toaster","qty":1,"price":"1195","regular_price":"1495","sale_price":"1195","image":"http://www.electrisia.com/wp-content/uploads/2015/11/Surya-10-Sizzle-10-L-SDL647886232-1-a142d16-200x150.jpg"}],"count":1,"line_count":1},"msg":"Product added to cart successfully"}

Total item in cart response {"status":1,"Total Items in a cart":0}

I know woo-commerce managing a kind of cache for each user on website like in postman man every time when i hit add to cart API it automatically add item on my previous cart but in mobile it doesn't. every time i click add to cart i get 0 on total item in cart.

code of total item in cart :-

 StringRequest request = new StringRequest(Request.Method.GET,TotalItemCart,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {

                    Log.d("Response",response);
                    ProgressUtils.cancelProgressDialog();

                    try {
                        JSONObject object = new JSONObject(response);

                      /*  if(object.has("count"))
                        {
                            cartItemCount = object.getString("count");
                            tv.setText(cartItemCount);
                        }*/

                        if(object.has("status")) {
                            if (object.getInt("status") == 1) {


                                if(object.has("Total Items in a cart"))
                                {

                                    cartItemCount = object.getString("Total Items in a cart");
                                    tv.setText(cartItemCount);


                                }

                            }else {

                                Toast.makeText(ItemDescriptionActivity.this,"failed to add item",Toast.LENGTH_SHORT).show();

                            }
                        }


                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            },

            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                    ProgressUtils.cancelProgressDialog();
                    Toast.makeText(ItemDescriptionActivity.this, "Cart not updated", Toast.LENGTH_SHORT).show();

                }
            });

    queue.add(request);

Add to cart API response postman

Add to cart code

 ProgressUtils.showProgressDialog(ItemDescriptionActivity.this);
    StringRequest request = new StringRequest(Request.Method.POST,AppConstant.AddToCart,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {

                    Log.d("Response",response);
                    ProgressUtils.cancelProgressDialog();

                    if(response != null)
                    {
                        try {
                            JSONObject responseOBJ = new JSONObject(response);

                            if(responseOBJ.has("status"))
                            {
                                if(responseOBJ.getInt("status")==1)
                                {
                                    Toast.makeText(ItemDescriptionActivity.this,"Item added successfully",Toast.LENGTH_SHORT).show();
                                    ItemCartCount();
                                }else {

                                    if(responseOBJ.has("msg"))
                                    {
                                        Toast.makeText(ItemDescriptionActivity.this,""+responseOBJ.getString("msg"),Toast.LENGTH_SHORT).show();
                                    }

                                }
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }

                }
            },

            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                    ProgressUtils.cancelProgressDialog();
                    Toast.makeText(ItemDescriptionActivity.this, "Someting went wrong...", Toast.LENGTH_SHORT).show();

                }
            }){

        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String,String> params = new HashMap<String, String>();
            params.put("product_id", String.valueOf(AppConstant.Product_ID));
            params.put("qty","1");
            params.put("secret_key",sec_key);

            return params;
        }

         //no semicolon or coma



        };

    queue.add(request);
Rohit Singh
  • 403
  • 4
  • 9

2 Answers2

1

Firstly, you should post the code here, otherwise nobody here could help you.Also you need to check both the requests,there must be missing something when you pass the parameters from the app because if Postman shows it right then there must be some coding mistake.

Neha
  • 389
  • 4
  • 8
  • 24
  • i have updated my post please check. have you work with woo-commerce website and convert it a native application ?? – Rohit Singh May 12 '18 at 15:03
0

You need check the request and response. There are several ways. 1. log in app, before you do request and after response. you can see here Volley JsonObjectRequest Post request not working 2. use charles to check the request you actually make, and the response the server return. It need some time to set it, but worth.

jayzhou215
  • 111
  • 1
  • 3