-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathnewsletter.ts
32 lines (30 loc) · 988 Bytes
/
newsletter.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import i18n from '@vue-storefront/i18n'
import EventBus from '@vue-storefront/core/compatibility/plugins/event-bus'
import { KlarnaPlugin } from '../types'
const plugin: KlarnaPlugin = {
name: 'newsletter',
beforeCreate: ({ order }) => {
const { options } = order
if (options && options.additional_checkboxes) {
options.additional_checkboxes.forEach(checkbox => {
if (checkbox.id === 'newsletter_opt_in') {
checkbox.text = i18n.t(checkbox.text)
}
})
}
order.options = options
return order
},
onConfirmation: ({ result }) => {
const checkboxes = result.merchant_requested && result.merchant_requested.additional_checkboxes
if (checkboxes) {
const newsletter = checkboxes.find(({ id }) => id === 'newsletter_opt_in')
if (newsletter && newsletter.checked) {
EventBus.$emit('newsletter-signup', {
email: result.billing_address.email
})
}
}
}
}
export default plugin