Skip to content

Commit e73f2ca

Browse files
committed
@NataliaTepluhina - please add this to the docs as well: Starting with Vue Storefront v1.6 we changed the default order-placing behaviour. Currently the `config.orders.directBackendSync` is set to `true` be default. With this option enabled - if the user is online, Vue Storefront tries to pass the order immediately and synchronously (waiting for result) to the eCommerce backend. This option gives immediate and direct feedback to the user. If there is an app-level error (for example validation error on Magento side) user will be notified immediately. If there is transmission issue (no connection, servers are down etc) the order is being put into queue (as it was prior to 1.6). If `config.orders.directBackendSync` is set to false - then the legacy behaviour with queuing all the orders is being used. With `directBackendSync` set to true we do have access to the server confirmation (with backend orderId) in `store.state.order.last_order_confirmation`
1 parent 878c656 commit e73f2ca

13 files changed

Lines changed: 71 additions & 20 deletions

File tree

config/default.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@
184184
}
185185
},
186186
"orders": {
187+
"directBackendSync": true,
187188
"endpoint": "http://localhost:8080/api/order",
188189
"payment_methods_mapping": {
189190
},

core/app.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export function createApp (ssrContext, config): { app: Vue, router: any, store:
7777
store.state.config = config
7878
store.state.__DEMO_MODE__ = (config.demomode === true) ? true : false
7979
if(ssrContext) Vue.prototype.$ssrRequestContext = ssrContext
80-
80+
Vue.prototype.$coreRouter = router
8181
if (!store.state.config) store.state.config = buildTimeConfig // if provided from SSR, don't replace it
8282

8383
// depreciated

core/i18n/resource/i18n/en-US.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,4 @@
6565
"to account","to account"
6666
"Are you sure you would like to remove this item from the shopping cart?","Are you sure you would like to remove this item from the shopping cart?"
6767
"The product or category is not available in Offline mode. Redirecting to Home.","The product or category is not available in Offline mode. Redirecting to Home."
68+
"Processing order...","Processing order..."

core/modules/cart/store/actions.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ import Vue from 'vue'
22
import { ActionTree } from 'vuex'
33
import * as types from './mutation-types'
44
import rootStore from '@vue-storefront/store'
5-
// import router from '@vue-storefront/core/router'
65
import i18n from '@vue-storefront/i18n'
76
import { sha3_224 } from 'js-sha3'
8-
import { currentStoreView } from '@vue-storefront/store/lib/multistore'
7+
import { currentStoreView, localizedRoute} from '@vue-storefront/store/lib/multistore'
98
import omit from 'lodash-es/omit'
109
import RootState from '@vue-storefront/store/types/RootState'
1110
import CartState from '../types/CartState'
@@ -179,11 +178,11 @@ const actions: ActionTree<CartState, RootState> = {
179178
const commit = context.commit
180179
const state = context.state
181180

182-
if (!state.shipping.method_code) {
181+
if (!state.shipping || !state.shipping.method_code) {
183182
let shippingMethod = context.rootGetters['shipping/shippingMethods'].find(item => item.default)
184183
commit(types.CART_UPD_SHIPPING, shippingMethod)
185184
}
186-
if (!state.payment.code) {
185+
if (!state.payment || !state.payment.code) {
187186
let paymentMethod = context.rootGetters['payment/paymentMethods'].find(item => item.default)
188187
commit(types.CART_UPD_PAYMENT, paymentMethod)
189188
}
@@ -214,7 +213,7 @@ const actions: ActionTree<CartState, RootState> = {
214213
return state.cartItems.find(p => p.sku === sku)
215214
},
216215
goToCheckout (context) {
217-
// router.push(localizedRoute('/checkout', currentStoreView().storeCode))
216+
Vue.prototype.$coreRouter.push(localizedRoute('/checkout', currentStoreView().storeCode))
218217
},
219218
addItem ({ commit, dispatch, state }, { productToAdd, forceServerSilence = false }) {
220219
let productsToAdd = []

core/modules/checkout/store/checkout/actions.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@ const actions: ActionTree<CheckoutState, RootState> = {
1414
*/
1515
placeOrder (context, { order }) {
1616
try {
17-
context.dispatch('order/placeOrder', order, {root: true}).then(result => {
18-
Vue.prototype.$db.usersCollection.setItem('last-cart-bypass-ts', new Date().getTime())
19-
context.dispatch('cart/clear', {}, {root: true})
20-
if (context.state.personalDetails.createAccount) {
21-
context.commit(types.CHECKOUT_DROP_PASSWORD)
17+
return context.dispatch('order/placeOrder', order, {root: true}).then(result => {
18+
if (!result.resultCode || result.resultCode === 200) {
19+
Vue.prototype.$db.usersCollection.setItem('last-cart-bypass-ts', new Date().getTime())
20+
context.dispatch('cart/clear', {}, {root: true})
21+
if (context.state.personalDetails.createAccount) {
22+
context.commit(types.CHECKOUT_DROP_PASSWORD)
23+
}
2224
}
2325
})
2426
} catch (e) {

core/modules/order/store/actions.ts

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import { ActionTree } from 'vuex'
66
import RootState from '@vue-storefront/store/types/RootState'
77
import OrderState from '../types/OrderState'
88
const Ajv = require('ajv') // json validator
9+
import rootStore from '@vue-storefront/store'
10+
import { isOnline } from '@vue-storefront/store/lib/search'
11+
import i18n from '@vue-storefront/i18n'
912

1013
const actions: ActionTree<OrderState, RootState> = {
1114
/**
@@ -28,10 +31,41 @@ const actions: ActionTree<OrderState, RootState> = {
2831
throw new ValidationError(validate.errors)
2932
} else {
3033
Vue.prototype.$bus.$emit('order-before-placed', { order: order })
31-
commit(types.ORDER_PLACE_ORDER, order)
32-
Vue.prototype.$bus.$emit('order-after-placed', { order: order })
33-
34-
return true
34+
if (!rootStore.state.config.orders.directBackendSync || !isOnline())
35+
{
36+
commit(types.ORDER_PLACE_ORDER, order)
37+
Vue.prototype.$bus.$emit('order-after-placed', { order: order })
38+
return Promise.resolve(true)
39+
} else {
40+
Vue.prototype.$bus.$emit('notification-progress-start', i18n.t('Processing order...'))
41+
return rootStore.dispatch('sync/execute', { url: rootStore.state.config.orders.endpoint, // sync the order
42+
payload: {
43+
method: 'POST',
44+
headers: { 'Content-Type': 'application/json' },
45+
mode: 'cors',
46+
body: JSON.stringify(order)
47+
},
48+
}, { root: true }).then(task => {
49+
Vue.prototype.$bus.$emit('notification-progress-stop')
50+
if (task.resultCode !== 500) {
51+
order.transmited = true
52+
commit(types.ORDER_PLACE_ORDER, order) // archive this order but not trasmit it second time
53+
commit(types.ORDER_LAST_ORDER_WITH_CONFIRMATION, { order: order, confirmation: task.result })
54+
Vue.prototype.$bus.$emit('order-after-placed', { order: order, confirmation: task.result })
55+
}
56+
return task.result
57+
}).catch(e => {
58+
rootStore.dispatch('notification/spawnNotification', {
59+
type: 'error',
60+
message: i18n.t('The order can not be transfered because of server error. Order has been queued'),
61+
action1: { label: i18n.t('OK') }
62+
})
63+
order.transmited = false // queue order
64+
commit(types.ORDER_PLACE_ORDER, order) // archive this order but not trasmit it second time
65+
Vue.prototype.$bus.$emit('notification-progress-stop')
66+
throw (e)
67+
})
68+
}
3569
}
3670
}
3771
}

core/modules/order/store/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import OrderState from '../types/OrderState'
77
export const module: Module<OrderState, RootState> = {
88
namespaced: true,
99
state: {
10+
last_order_confirmation: null
1011
},
1112
actions,
1213
mutations
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export const SN_ORDER = 'order'
22
export const ORDER_PLACE_ORDER = SN_ORDER + '/PLACE_ORDER'
3-
export const ORDER_PROCESS_QUEUE = SN_ORDER + '/PROCESS_QUEUE'
3+
export const ORDER_PROCESS_QUEUE = SN_ORDER + '/PROCESS_QUEUE'
4+
export const ORDER_LAST_ORDER_WITH_CONFIRMATION = SN_ORDER + '/LAST_ORDER_CONFIRMATION'

core/modules/order/store/mutations.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,21 @@ const mutations: MutationTree<OrderState> = {
1414
const ordersCollection = Vue.prototype.$db.ordersCollection
1515
const orderId = entities.uniqueEntityId(order) // timestamp as a order id is not the best we can do but it's enough
1616
order.order_id = orderId.toString()
17-
order.transmited = false
1817
order.created_at = new Date()
1918
order.updated_at = new Date()
2019

2120
ordersCollection.setItem(orderId.toString(), order, (err, resp) => {
2221
if (err) console.error(err)
23-
Vue.prototype.$bus.$emit('order/PROCESS_QUEUE', { config: rootStore.state.config }) // process checkout queue
22+
if (!order.transmited) {
23+
Vue.prototype.$bus.$emit('order/PROCESS_QUEUE', { config: rootStore.state.config }) // process checkout queue
24+
}
2425
console.info('Order placed, orderId = ' + orderId)
2526
}).catch((reason) => {
2627
console.error(reason) // it doesn't work on SSR
2728
}) // populate cache
29+
},
30+
[types.ORDER_LAST_ORDER_WITH_CONFIRMATION] (state, payload) {
31+
state.last_order_confirmation = payload
2832
}
2933
}
3034

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export default interface OrderState {
2+
last_order_confirmation: any
23
}

0 commit comments

Comments
 (0)