Vendic / vsf-external-checkout

VueStorefront External Checkout module
MIT License
25 stars 10 forks source link

Execute `cart/sync` (1.11) or `cart/serverPull` (1.10) before redirect #19

Closed pkarw closed 4 years ago

pkarw commented 5 years ago

Please do modify the beforeEach.ts in order to make sure the sync process has been fully executed BEFORE redirecting the user to Magento checkout.

Suggested change:

export function beforeEach(to: Route, from: Route, next) {
  const cartToken: string = rootStore.state.cart.cartServerToken;
  const userToken: string = rootStore.state.user.token;
  const externalCheckoutConfig = {...config.externalCheckout};
  const cmsUrl: string = externalCheckoutConfig.cmsUrl;
  const stores = externalCheckoutConfig.stores;
  const storeCode = currentStoreView().storeCode
  const multistoreEnabled: boolean = config.storeViews.multistore

  if (multistoreEnabled) {
    **await rootStore.dispatch('cart/sync')**
    if (storeCode in stores && to.name === storeCode + '-checkout') {
      window.location.replace(stores[storeCode].cmsUrl + '/vue/cart/sync/token/' + userToken + '/cart/' + cartToken)
    } else if (storeCode in stores && to.name === 'checkout' && stores[storeCode].cmsUrl !== undefined) {
      window.location.replace(stores[storeCode].cmsUrl + '/vue/cart/sync/token/' + userToken + '/cart/' + cartToken)
    } else {
      next()
    }
  } else {
    if (to.name === 'checkout') {
      window.location.replace(cmsUrl + '/vue/cart/sync/token/' + userToken + '/cart/' + cartToken)
    } else {
      next()
    }
  }
}

Otherwise there could be a situation when the sync hasn't been fully executed and user getting to the Magento checkout sees some discrepancies between Magento and Vue Storefront carts. For example - product added to the VSF cart hasn't been yet added to Magento cart.

The action to be called:

pkarw commented 4 years ago

@Tjitse-E I just wanted to follow up on this issue. Any chances to get it fixed?:)

Tjitse-E commented 4 years ago

@pkarw I really don't have time for this at the moment. I've added the 'help wanted' label, hopefully someone else can prepare a PR!

RakowskiPrzemyslaw commented 4 years ago

Hi, here is PR that should solve the problem for version 3.0.0: https://github.com/Vendic/vsf-external-checkout/pull/24