Skip to content

Commit

Permalink
Merge pull request #28 from eliascarella/feat-set-currency
Browse files Browse the repository at this point in the history
feat: added support for the setCurrency method
  • Loading branch information
flozero authored Jun 15, 2021
2 parents aa56656 + 7f32f8a commit 67bf3b0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
14 changes: 11 additions & 3 deletions example/pages/index.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<template>
<div>
Items Number : <span class="snipcart-items-count" />
Items price: <span class="snipcart-total-price" />
Items Number : <span class="snipcart-items-count" /> Items price:
<span class="snipcart-total-price" />
<button
class="snipcart-add-item"
v-bind="{
Expand All @@ -23,6 +23,12 @@
<button class="switch-lang" @click="switchLang">
switch Lang
</button>
<button class="switch-currency" @click="switchCurrency('eur')">
Change Currency to euros
</button>
<button class="switch-currency" @click="switchCurrency('usd')">
Change Currency to dollars
</button>
</div>
</template>

Expand Down Expand Up @@ -85,7 +91,6 @@ export default {
name: 'Engraving',
placeholder: 'ex: John Doe'
}
]
},
addItemEvent: null
Expand All @@ -104,6 +109,9 @@ export default {
switchLang () {
this.lang = this.lang === 'fr' ? 'en' : 'fr'
this.$snipcart.setLanguage(this.lang)
},
switchCurrency (currency) {
this.$snipcart.setCurrency(currency)
}
}
}
Expand Down
14 changes: 11 additions & 3 deletions lib/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ const initEvents = () => {
})
})
}
const setCurrency = (currency) => {
if (typeof window === 'undefined' || !window.Snipcart) {
throw new Error('window.Snipcart not accessible')
}
window.Snipcart.api.session.setCurrency(currency)
}

const setLanguage = (lang, options) => {
if (typeof window === 'undefined' || !window.Snipcart) {
Expand All @@ -48,8 +54,7 @@ const bindProduct = (product) => {
}

Object.keys(product).forEach((key) => {
ret[`data-item-${key.toString().toLowerCase()}`] =
product[key]
ret[`data-item-${key.toString().toLowerCase()}`] = product[key]
})
return ret
}
Expand Down Expand Up @@ -86,13 +91,16 @@ export default (_, inject) => {
const snipcart = {
customfields: customFields,
setLanguage,
setCurrency,
bindProduct
}

inject('snipcart', snipcart)

// not execute the second part on sever
if (process.server) { return }
if (process.server) {
return
}

onLoad(() => {
initEvents()
Expand Down

0 comments on commit 67bf3b0

Please sign in to comment.