use-shopping-cart icon indicating copy to clipboard operation
use-shopping-cart copied to clipboard

loadCart not merging correctly

Open amirhemm opened this issue 3 years ago • 6 comments

Hi,

I'm trying to use this method to sync my DB with local storage with the loadCart method and merging to true, but it seems that if you have the same product with the same ID added and you are trying to load it from DB and it already exists in the local storage, the current behavior is that the cartCount and totalPrice update accordingly but the cartItem quantity is not being updated.

Can you verify that this issue exists and if it dose I would suggest one of the these two approaches to fix it:

  1. Update the quantity of the cartItem.
  2. Merge the item with the same ID without updating the cartCount or totalPrice.

Thanks

amirhemm avatar Apr 12 '21 20:04 amirhemm

hey @amirhemm, which version of USC are you using? The beta or the latest release?

dayhaysoos avatar Apr 15 '21 16:04 dayhaysoos

I'm using the latest release.

amirhemm avatar Apr 20 '21 07:04 amirhemm

Hey @amirhemm, sorry for taking so long to get back to you. Did you end up finding a workaround by any chance? I am thinking of taking the merge approach you proposed, but there were some other issues/questions that came about regarding loadCart so I didn't get around to it.

dayhaysoos avatar Jul 04 '21 15:07 dayhaysoos

Hi @dayhaysoos, no worries, I know you got a lot on your plate. Well I did manage to set something up for my use case. In a nutshell, if they user is logged in I merge (not by using the utility function, I made my own helper function) their DB cart with local storage cart. Everything works fine if user adds a new item to the cart. But once a user adds an item which is already added before to the DB cart, I set the quantity of the remote cart as the source of truth. I don't add the quantities. I just overwrite the whole remote item with the local item. I hope that help. Let me know know if you have a question. I also have a test site up. Let me know if you are interested to check it out.

amirhemm avatar Jul 04 '21 20:07 amirhemm

@amirhemm I would love to see it! I would definitely like some direction on how to implement this with loadCart if you don't mind. I have yet to test USC with a backend so your perspective is very valuable

dayhaysoos avatar Jul 04 '21 22:07 dayhaysoos

@dayhaysoos you can check out a sample implementation here. Let me know if come across any questions, I'll be more that happy to help if needed.

amirhemm avatar Jul 05 '21 08:07 amirhemm