I wanted to create a add to cart function inside Firebase, but I'm not quite sure on how to write the if statement for my problems.
I want to achieve a result where when user cart is empty it will create a unique id that will hold all the user item information. If user already have item in their cart, the item can just be added without creating a new unique id.
Below is my Cart database, when user already have item in their cart and they want to add a new item in their cart they can just simply add those item in their cart. The highlight ID is the cart id of the user. I have problem, every time user want to add item into their cart, it keep producing new unique key. Which I don't want it. I want the item to be put under the highlighted id. The current problem I'm having is it keep producing a new id every time user add new item.
AddCart class
String cart_id;
private void addCart() {
if (cart_id != null) {
Intent intent = getIntent();
final String cust_id = firebaseAuth.getCurrentUser().getUid();
final String pid = intent.getStringExtra("pid");
String keyB = intent.getStringExtra("keyB");
String name = pname.getText().toString().trim();
String category = pcategory.getText().toString().trim();
String price = pprice.getText().toString().trim();
String size = psize.getSelectedItem().toString().trim();
String quantity = pquantity.getText().toString().trim();
String image = intent.getStringExtra("pro_image");
final Cart cart = new Cart(cart_id, pid, cust_id, keyB, name, price, image, category, quantity, size);
databaseReference.child("Customer List").child(cust_id).child(pid).setValue(cart) //table and primary key
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
databaseReference.child("PS List").child(cust_id).child(pid).setValue(cart).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Toast.makeText(getApplicationContext(), "add Successfully", Toast.LENGTH_SHORT).show();
AddToCart.super.onBackPressed();
}
}
});
}
}
}
);
}
else
{
final String cart_id = databaseReference.push().getKey();
Intent intent = getIntent();
final String cust_id = firebaseAuth.getCurrentUser().getUid();
final String pid = intent.getStringExtra("pid");
String keyB = intent.getStringExtra("keyB");
String name = pname.getText().toString().trim();
String category = pcategory.getText().toString().trim();
String price = pprice.getText().toString().trim();
String size = psize.getSelectedItem().toString().trim();
String quantity = pquantity.getText().toString().trim();
String image = intent.getStringExtra("pro_image");
final Cart cart = new Cart(cart_id, pid, cust_id, keyB, name, price, image, category, quantity, size);
databaseReference.child("Customer List").child(cust_id).child(cart_id).child(pid).setValue(cart) //table and primary key
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
databaseReference.child("PS List").child(cust_id).child(cart_id).child(pid).setValue(cart).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Toast.makeText(getApplicationContext(), "add Successfully", Toast.LENGTH_SHORT).show();
AddToCart.super.onBackPressed();
}
}
});
}
}
}
);
}
}
Cart
public class Cart {
public String cart_id;
public String pro_id;
public String cust_id;
public String brand_id;
public String pro_name;
public String pro_price;
public String pro_image;
public String pro_category;
public String quantity;
public String size;
public Cart () {
}
public Cart(String cart_id, String pro_id, String cust_id, String brand_id, String pro_name, String pro_price, String pro_image, String pro_category, String quantity, String size) {
this.cart_id = cart_id;
this.pro_id = pro_id;
this.cust_id = cust_id;
this.brand_id = brand_id;
this.pro_name = pro_name;
this.pro_price = pro_price;
this.pro_image = pro_image;
this.pro_category = pro_category;
this.quantity = quantity;
this.size = size;
}