|
1
|
|
|
import type { Module } from 'vuex'; |
|
2
|
|
|
import type { AxiosResponse } from 'axios'; |
|
3
|
|
|
import type { |
|
4
|
|
|
CalculatedPrice, |
|
5
|
|
|
Cart, |
|
6
|
|
|
CartError, |
|
7
|
|
|
ContextSwitchParameters, |
|
8
|
|
|
Currency, |
|
9
|
|
|
Customer, |
|
10
|
|
|
LineItem, |
|
11
|
|
|
PaymentMethod, |
|
12
|
|
|
PromotionCodeTag, |
|
13
|
|
|
SalesChannel, |
|
14
|
|
|
SalesChannelContext, |
|
15
|
|
|
ShippingMethod, |
|
16
|
|
|
} from '../order.types'; |
|
17
|
|
|
|
|
18
|
|
|
const { Service } = Shopware; |
|
19
|
|
|
|
|
20
|
|
|
function filterEmptyLineItems(items: LineItem[]) { |
|
21
|
|
|
return items.filter(item => item.label === ''); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
function reverseLineItems(items: LineItem[]) { |
|
25
|
|
|
return items.slice().reverse(); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
function mergeEmptyAndExistingLineItems(emptyLineItems: LineItem[], lineItems: LineItem[]) { |
|
29
|
|
|
// Reverse the lineItems so the newly added are at the top for better UX |
|
30
|
|
|
reverseLineItems(lineItems); |
|
31
|
|
|
|
|
32
|
|
|
return [...emptyLineItems, ...lineItems]; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
interface SwOrderState { |
|
36
|
|
|
cart: Cart; |
|
37
|
|
|
disabledAutoPromotion: boolean, |
|
38
|
|
|
promotionCodes: PromotionCodeTag[], |
|
39
|
|
|
defaultSalesChannel: SalesChannel | null, |
|
40
|
|
|
context: SalesChannelContext, |
|
41
|
|
|
customer: Customer | null, |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @deprecated tag:v6.5.0 - Use `context.currency` instead |
|
45
|
|
|
*/ |
|
46
|
|
|
currency: Currency, |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
const SwOrderStore: Module<SwOrderState, VuexRootState> = { |
|
50
|
|
|
namespaced: true, |
|
51
|
|
|
|
|
52
|
|
|
state: (): SwOrderState => ({ |
|
53
|
|
|
customer: null, |
|
54
|
|
|
defaultSalesChannel: null, |
|
55
|
|
|
cart: { |
|
56
|
|
|
token: null, |
|
57
|
|
|
lineItems: [], |
|
58
|
|
|
price: { |
|
59
|
|
|
totalPrice: null, |
|
60
|
|
|
}, |
|
61
|
|
|
deliveries: [], |
|
62
|
|
|
} as unknown as Cart, |
|
63
|
|
|
currency: { |
|
64
|
|
|
shortName: 'EUR', |
|
65
|
|
|
symbol: '€', |
|
66
|
|
|
totalRounding: { |
|
67
|
|
|
decimals: 2, |
|
68
|
|
|
}, |
|
69
|
|
|
} as unknown as Currency, |
|
70
|
|
|
context: { |
|
71
|
|
|
token: '', |
|
72
|
|
|
customer: null, |
|
73
|
|
|
paymentMethod: { |
|
74
|
|
|
translated: { |
|
75
|
|
|
distinguishableName: '', |
|
76
|
|
|
}, |
|
77
|
|
|
} as PaymentMethod, |
|
78
|
|
|
shippingMethod: { |
|
79
|
|
|
translated: { |
|
80
|
|
|
name: '', |
|
81
|
|
|
}, |
|
82
|
|
|
} as ShippingMethod, |
|
83
|
|
|
currency: { |
|
84
|
|
|
shortName: 'EUR', |
|
85
|
|
|
symbol: '€', |
|
86
|
|
|
totalRounding: { |
|
87
|
|
|
decimals: 2, |
|
88
|
|
|
}, |
|
89
|
|
|
} as Currency, |
|
90
|
|
|
salesChannel: { |
|
91
|
|
|
id: '', |
|
92
|
|
|
} as SalesChannel, |
|
93
|
|
|
context: { |
|
94
|
|
|
currencyId: '', |
|
95
|
|
|
languageIdChain: [], |
|
96
|
|
|
}, |
|
97
|
|
|
}, |
|
98
|
|
|
promotionCodes: [], |
|
99
|
|
|
disabledAutoPromotion: false, |
|
100
|
|
|
}), |
|
101
|
|
|
|
|
102
|
|
|
mutations: { |
|
103
|
|
|
setCustomer(state: SwOrderState, customer: Customer) { |
|
104
|
|
|
state.context.customer = customer; |
|
105
|
|
|
state.customer = customer; |
|
106
|
|
|
}, |
|
107
|
|
|
|
|
108
|
|
|
setDefaultSalesChannel(state: SwOrderState, salesChannel: SalesChannel) { |
|
109
|
|
|
state.defaultSalesChannel = salesChannel; |
|
110
|
|
|
}, |
|
111
|
|
|
|
|
112
|
|
|
setCartToken(state: SwOrderState, token: string) { |
|
113
|
|
|
state.cart.token = token; |
|
114
|
|
|
}, |
|
115
|
|
|
|
|
116
|
|
|
setCart(state: SwOrderState, cart: Cart) { |
|
117
|
|
|
const emptyLineItems = filterEmptyLineItems(state.cart.lineItems); |
|
118
|
|
|
state.cart = cart; |
|
119
|
|
|
state.cart.lineItems = mergeEmptyAndExistingLineItems(emptyLineItems, state.cart.lineItems); |
|
120
|
|
|
}, |
|
121
|
|
|
|
|
122
|
|
|
setCartLineItems(state: SwOrderState, lineItems: LineItem[]) { |
|
123
|
|
|
state.cart.lineItems = lineItems; |
|
124
|
|
|
}, |
|
125
|
|
|
|
|
126
|
|
|
setCurrency(state: SwOrderState, currency: Currency) { |
|
127
|
|
|
state.context.currency = currency; |
|
128
|
|
|
state.currency = currency; |
|
129
|
|
|
}, |
|
130
|
|
|
|
|
131
|
|
|
setContext(state: SwOrderState, context: SalesChannelContext) { |
|
132
|
|
|
state.context = context; |
|
133
|
|
|
}, |
|
134
|
|
|
|
|
135
|
|
|
setPromotionCodes(state: SwOrderState, promotionCodes: PromotionCodeTag[]) { |
|
136
|
|
|
state.promotionCodes = promotionCodes; |
|
137
|
|
|
}, |
|
138
|
|
|
|
|
139
|
|
|
removeEmptyLineItem(state: SwOrderState, emptyLineItemKey: string) { |
|
140
|
|
|
state.cart.lineItems = state.cart.lineItems.filter(item => item.id !== emptyLineItemKey); |
|
141
|
|
|
}, |
|
142
|
|
|
|
|
143
|
|
|
removeInvalidPromotionCodes(state: SwOrderState) { |
|
144
|
|
|
state.promotionCodes = state.promotionCodes.filter(item => !item.isInvalid); |
|
145
|
|
|
}, |
|
146
|
|
|
|
|
147
|
|
|
setDisabledAutoPromotion(state: SwOrderState, disabledAutoPromotion: boolean) { |
|
148
|
|
|
state.disabledAutoPromotion = disabledAutoPromotion; |
|
149
|
|
|
}, |
|
150
|
|
|
}, |
|
151
|
|
|
|
|
152
|
|
|
getters: { |
|
153
|
|
|
isCustomerActive(state: SwOrderState): boolean { |
|
154
|
|
|
return !!state?.context.customer?.active; |
|
155
|
|
|
}, |
|
156
|
|
|
|
|
157
|
|
|
isCartTokenAvailable(state: SwOrderState): boolean { |
|
158
|
|
|
return !!state?.cart?.token; |
|
159
|
|
|
}, |
|
160
|
|
|
|
|
161
|
|
|
currencyId(state: SwOrderState): string { |
|
162
|
|
|
return state?.context.context.currencyId ?? ''; |
|
163
|
|
|
}, |
|
164
|
|
|
|
|
165
|
|
|
invalidPromotionCodes(state: SwOrderState): PromotionCodeTag[] { |
|
166
|
|
|
return state.promotionCodes.filter(item => item.isInvalid); |
|
167
|
|
|
}, |
|
168
|
|
|
|
|
169
|
|
|
cartErrors(state: SwOrderState): CartError[] { |
|
170
|
|
|
return state?.cart?.errors ?? null; |
|
171
|
|
|
}, |
|
172
|
|
|
}, |
|
173
|
|
|
|
|
174
|
|
|
actions: { |
|
175
|
|
|
selectExistingCustomer({ commit }, { customer }: { customer: Customer }) { |
|
176
|
|
|
commit('setCustomer', customer); |
|
177
|
|
|
commit('setDefaultSalesChannel', { ...(customer?.salesChannel ?? null) }); |
|
178
|
|
|
}, |
|
179
|
|
|
|
|
180
|
|
|
createCart({ commit }, { salesChannelId }: { salesChannelId: string }) { |
|
181
|
|
|
return Service('cartStoreService') |
|
182
|
|
|
.createCart(salesChannelId) |
|
183
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access |
|
184
|
|
|
.then((response: AxiosResponse): string => { |
|
185
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access |
|
186
|
|
|
const token = response.data.token as string; |
|
187
|
|
|
commit('setCartToken', token); |
|
188
|
|
|
return token; |
|
189
|
|
|
}) |
|
190
|
|
|
.then((contextToken) => { |
|
191
|
|
|
return Service('contextStoreService') |
|
192
|
|
|
.getSalesChannelContext(salesChannelId, contextToken) |
|
193
|
|
|
.then((response: AxiosResponse) => commit('setContext', response.data)); |
|
194
|
|
|
}); |
|
195
|
|
|
}, |
|
196
|
|
|
|
|
197
|
|
|
getCart({ commit }, { salesChannelId, contextToken }: { salesChannelId: string, contextToken: string }) { |
|
198
|
|
|
if (Shopware.Feature.isActive('FEATURE_NEXT_7530') && (`${contextToken}`).length !== 32) { |
|
199
|
|
|
throw new Error('Invalid context token'); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
return Promise.all([ |
|
203
|
|
|
Service('cartStoreService') |
|
204
|
|
|
.getCart(salesChannelId, contextToken) |
|
205
|
|
|
.then((response: AxiosResponse) => commit('setCart', response.data)), |
|
206
|
|
|
Service('contextStoreService') |
|
207
|
|
|
.getSalesChannelContext(salesChannelId, contextToken) |
|
208
|
|
|
.then((response: AxiosResponse) => commit('setContext', response.data)), |
|
209
|
|
|
]); |
|
210
|
|
|
}, |
|
211
|
|
|
|
|
212
|
|
|
cancelCart(_, { salesChannelId, contextToken }: { salesChannelId: string, contextToken: string }) { |
|
213
|
|
|
if (Shopware.Feature.isActive('FEATURE_NEXT_7530') && (`${contextToken}`).length !== 32) { |
|
214
|
|
|
throw new Error('Invalid context token'); |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
return Service('cartStoreService').cancelCart(salesChannelId, contextToken); |
|
218
|
|
|
}, |
|
219
|
|
|
|
|
220
|
|
|
updateCustomerContext(_, { customerId, salesChannelId, contextToken }: |
|
221
|
|
|
{ customerId: string, salesChannelId: string, contextToken: string }) { |
|
222
|
|
|
return Service('contextStoreService') |
|
223
|
|
|
.updateCustomerContext(customerId, salesChannelId, contextToken); |
|
224
|
|
|
}, |
|
225
|
|
|
|
|
226
|
|
|
updateOrderContext(_, { context, salesChannelId, contextToken }: |
|
227
|
|
|
{ context: ContextSwitchParameters, salesChannelId: string, contextToken: string }) { |
|
228
|
|
|
return Service('contextStoreService') |
|
229
|
|
|
.updateContext(context, salesChannelId, contextToken); |
|
230
|
|
|
}, |
|
231
|
|
|
|
|
232
|
|
|
getContext(_, { salesChannelId, contextToken }: { salesChannelId: string, contextToken: string }) { |
|
233
|
|
|
return Service('contextStoreService') |
|
234
|
|
|
.getSalesChannelContext(salesChannelId, contextToken); |
|
235
|
|
|
}, |
|
236
|
|
|
|
|
237
|
|
|
saveOrder(_, { salesChannelId, contextToken }: { salesChannelId: string, contextToken: string }) { |
|
238
|
|
|
return Service('checkoutStoreService') |
|
239
|
|
|
.checkout(salesChannelId, contextToken); |
|
240
|
|
|
}, |
|
241
|
|
|
|
|
242
|
|
|
removeLineItems( |
|
243
|
|
|
{ commit }, |
|
244
|
|
|
{ salesChannelId, contextToken, lineItemKeys }: |
|
245
|
|
|
{ salesChannelId: string, contextToken: string, lineItemKeys: string[] }, |
|
246
|
|
|
) { |
|
247
|
|
|
return Service('cartStoreService') |
|
248
|
|
|
.removeLineItems(salesChannelId, contextToken, lineItemKeys) |
|
249
|
|
|
.then((response: AxiosResponse) => commit('setCart', response.data)); |
|
250
|
|
|
}, |
|
251
|
|
|
|
|
252
|
|
|
saveLineItem( |
|
253
|
|
|
{ commit }, |
|
254
|
|
|
{ salesChannelId, contextToken, item }: { salesChannelId: string, contextToken: string, item: LineItem }, |
|
255
|
|
|
) { |
|
256
|
|
|
return Service('cartStoreService') |
|
257
|
|
|
.saveLineItem(salesChannelId, contextToken, item) |
|
258
|
|
|
.then((response: AxiosResponse) => commit('setCart', response.data)); |
|
259
|
|
|
}, |
|
260
|
|
|
|
|
261
|
|
|
saveMultipleLineItems( |
|
262
|
|
|
{ commit }, |
|
263
|
|
|
{ salesChannelId, contextToken, items }: { salesChannelId: string, contextToken: string, items: LineItem[] }, |
|
264
|
|
|
) { |
|
265
|
|
|
return Service('cartStoreService') |
|
266
|
|
|
.addMultipleLineItems(salesChannelId, contextToken, items) |
|
267
|
|
|
.then((response: AxiosResponse) => commit('setCart', response.data)); |
|
268
|
|
|
}, |
|
269
|
|
|
|
|
270
|
|
|
addPromotionCode( |
|
271
|
|
|
{ commit }, |
|
272
|
|
|
{ salesChannelId, contextToken, code }: { salesChannelId: string, contextToken: string, code: string }, |
|
273
|
|
|
): Promise<void> { |
|
274
|
|
|
return Service('cartStoreService') |
|
275
|
|
|
.addPromotionCode(salesChannelId, contextToken, code) |
|
276
|
|
|
.then(response => commit('setCart', response.data)); |
|
277
|
|
|
}, |
|
278
|
|
|
|
|
279
|
|
|
modifyShippingCosts( |
|
280
|
|
|
{ commit }, |
|
281
|
|
|
{ salesChannelId, contextToken, shippingCosts }: |
|
282
|
|
|
{ salesChannelId: string, contextToken: string, shippingCosts: CalculatedPrice }, |
|
283
|
|
|
) { |
|
284
|
|
|
return Service('cartStoreService') |
|
285
|
|
|
.modifyShippingCosts(salesChannelId, contextToken, shippingCosts) |
|
286
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access |
|
287
|
|
|
.then((response: AxiosResponse) => commit('setCart', response.data.data)); |
|
288
|
|
|
}, |
|
289
|
|
|
|
|
290
|
|
|
remindPayment(_, { orderTransactionId }: { orderTransactionId: string }) { |
|
291
|
|
|
return Service('orderStateMachineService') |
|
292
|
|
|
.transitionOrderTransactionState(orderTransactionId, 'remind'); |
|
293
|
|
|
}, |
|
294
|
|
|
}, |
|
295
|
|
|
}; |
|
296
|
|
|
|
|
297
|
|
|
/** |
|
298
|
|
|
* @private |
|
299
|
|
|
*/ |
|
300
|
|
|
export default SwOrderStore; |
|
301
|
|
|
|
|
302
|
|
|
/** |
|
303
|
|
|
* @private |
|
304
|
|
|
*/ |
|
305
|
|
|
export type { |
|
306
|
|
|
SwOrderState, |
|
307
|
|
|
}; |
|
308
|
|
|
|
|
309
|
|
|
|