DivanteLtd / magento2-external-checkout

The project is now maintained by VENDIC: https://github.com/Vendic/magento2-external-checkout
https://github.com/Vendic/magento2-external-checkout
MIT License
11 stars 24 forks source link

Thank You page redirect #7

Open pkarw opened 6 years ago

pkarw commented 6 years ago

We need to add the configurable URL in module config where the user will be redirected after placing an Magento Order. It will be used to redirect back to Vue Storefront to clear the VS cart. Because it's not cleared after placing an order it's possible to use the same cart id second time - and get the nasty error instead #4 :)

filrak commented 6 years ago

@pkarw is it done?

pkarw commented 6 years ago

I'm not sure, didn't have a chance to test it yet

Tjitse-E commented 6 years ago

@pkarw @filrak i've just tested this and it's working. You can enter the thank you redirect url in the Magento 2 backend: configuration___settings___stores___magento_admin-2

How can we clear the cart in vue storefront?

pkarw commented 6 years ago

It's partially done. What You need to add - is to expose the special ThankYouPage url in the vue-storefornt (adding it as a theme route or extension route) and in the component which is processing this route please add:

  rootStore.dispatch('cart/clear', {}, { root: true })
  rootStore.dispatch('cart/serverCreate', { guestCart: false }, { root: true })

in the beforeMount event handler

Tjitse-E commented 6 years ago

@pkarw , i've tried a custom route with the following component:

<template>
  <h1>Cart cleared</h1>
</template>

<script>
import rootStore from 'core/store'

export default {
  beforeMount () {
    rootStore.dispatch('cart/clear', {}, { root: true })
    console.log('Cart cleared')
  }
}
</script>

But this doesn't seem to work reliably. Any ideas on why the cart isn't cleared? devtools_-_localhost_3000_thank-you-external_en_default_theme_-_vue_storefront

Ik can see that beforeMount () is triggered correctly.

By the way rootStore.dispatch('cart/serverCreate', { guestCart: false }, { root: true }) seems to double the cart quantities.

pkarw commented 6 years ago

It may be the case that the cart is loaded after that ... :) I mean - beforeMount is called before cart is being loaded.

Please try this method instead:

        global.$VS.db.cartsCollection.setItem('current-cart-token', '').catch((reason) => {
          console.error(reason)
        })
        global.$VS.db.cartsCollection.setItem('current-cart', null).catch((reason) => {
          console.error(reason) // it doesn't work on SSR
        }) // populate cache
Tjitse-E commented 6 years ago

@pkarw thanks for your suggestion! The approach suggested in your last comment works if you refresh a few times, but not if you're being redirected from Magento 2 to VS. Does this make any sense to you?

pkarw commented 6 years ago

OK :) Last suggestion that comes to my mind:

  mounted () {
    this.$bus.$on('cart-after-loaded', (payload) => {
    rootStore.dispatch('cart/clear', {}, { root: true })
})
  },
Tjitse-E commented 6 years ago

Until we have a better/cleaner solution we're now using this:

  beforeMount () {
    this.$bus.$on('application-after-loaded', (payload) => {
      if (document.getElementById('thank_you_external') != null) {
        this.clearTheCart()
      }
    })
    this.$bus.$on('cart-after-itemchanged', (payload) => {
      if (document.getElementById('thank_you_external') != null) {
        this.clearTheCart()
      }
    })
  },
  methods: {
    clearTheCart () {
      let cartItemsQuantity = this.$store.state.cart.cartItems.length
      if (cartItemsQuantity > 0) {
        rootStore.dispatch('cart/clear', {}, { root: true })
      }
    }
  }
pkarw commented 6 years ago

Please create a PR from it!

Igloczek commented 5 years ago

I did it simpler, but disabling some cart feature which behave kinda weird (in my opinion is just broken / not well made / logic is too optimistic)

My solution:

  1. In config/local.json set cart.serverMergeByDefault to false
  2. In custom success page Vue file add
    mounted () {
    this.$store.dispatch('cart/clear', {}, { root: true })
    }

It will unconditionaly clear the cart right after entering the success page.