vuestorefront / vue-storefront-1

The open-source frontend for any eCommerce. Built with a PWA and headless approach, using a modern JS stack. We have custom integrations with Magento, commercetools, Shopware and Shopify and total coverage is just a matter of time. The API approach also allows you to merge VSF with any third-party tool like CMS, payment gateways or analytics. Newest updates: https://blog.vuestorefront.io. Always Open Source, MIT license.
https://www.vuestorefront.io
MIT License
18 stars 13 forks source link

Duplication of item quantities in cart after reloading as logged in user. #231

Closed bloodf closed 3 years ago

bloodf commented 3 years ago

From vue-storefront created by muhammad-abubakkar: vuestorefront/vue-storefront#5743

I am experiencing an unexpected behaviour when I have some items in cart and when I refresh cart page my items quantity get doubled. It doesn't happen every time but some times.

I think there is a race condition for cart/sync action call. Following are the two execution paths I found.

  1. On load we are calling cart/load action after goes on to call cart/sync action with default value mergeQty: false
  2. On load for logged in user after authentication, user/me action gets called and which goes on to call cart/authorize action in which we are calling cart/connect with mergeQty: true which then those on to cart/sync action with mergeQty:true and doubling the items in cart.

There is an issue with this condition. What will happen if serverItem.qty and clientItem.qty is same but mergeQty is true. It will double the qty of item which can cause confusion. https://github.com/vuestorefront/vue-storefront/blob/e9d9f64f3f0467b435ec59d75ac9fb3e33283187/core/modules/cart/store/actions/mergeActions.ts#L85

The only place where mergeQty is set as true is here. https://github.com/vuestorefront/vue-storefront/blob/e9d9f64f3f0467b435ec59d75ac9fb3e33283187/core/modules/cart/store/actions/connectActions.ts#L37

I am sorry if I missed any updates or fixes.

I hope this makes sense. Please help fix this issue. Thanks.