add product to checkout when added to cart

This commit is contained in:
thomas
2020-02-24 20:08:56 +01:00
parent 5ea6dcfaa1
commit ef8c22855f
6 changed files with 75 additions and 63 deletions
+12 -4
View File
@@ -1,16 +1,24 @@
export default { export default {
computed: {
checkout () {
return this.$store.state.checkout
},
cart () {
return this.$store.state.cart
}
},
methods: { methods: {
addToCart (product, checkout) { addToCart (product, checkout) {
// add to cart // add to cart
this.$store.dispatch('addItemToCart', product).then((item) => { this.$store.dispatch('addItemToCart', product).then((item) => {
// eslint-disable-next-line no-console
console.log('item : ', item)
const lineItemsToAdd = { const lineItemsToAdd = {
variantId: item.variants[0].id, variantId: item.variants[0].id,
quantity: item.count quantity: 1
} }
// Add an item to the checkout // Add an item to the checkout
this.$shopify.checkout.addLineItems(checkout.id, lineItemsToAdd) this.$shopify.checkout.addLineItems(checkout.id, lineItemsToAdd).then((checkout) => {
this.$store.commit('setCheckout', checkout)
})
this.$buefy.toast.open({ this.$buefy.toast.open({
message: 'Product added', message: 'Product added',
type: 'is-success' type: 'is-success'
+8 -22
View File
@@ -11470,8 +11470,7 @@
}, },
"ansi-regex": { "ansi-regex": {
"version": "2.1.1", "version": "2.1.1",
"bundled": true, "bundled": true
"optional": true
}, },
"aproba": { "aproba": {
"version": "1.2.0", "version": "1.2.0",
@@ -11508,8 +11507,7 @@
}, },
"code-point-at": { "code-point-at": {
"version": "1.1.0", "version": "1.1.0",
"bundled": true, "bundled": true
"optional": true
}, },
"concat-map": { "concat-map": {
"version": "0.0.1", "version": "0.0.1",
@@ -11518,8 +11516,7 @@
}, },
"console-control-strings": { "console-control-strings": {
"version": "1.1.0", "version": "1.1.0",
"bundled": true, "bundled": true
"optional": true
}, },
"core-util-is": { "core-util-is": {
"version": "1.0.2", "version": "1.0.2",
@@ -11622,8 +11619,7 @@
}, },
"inherits": { "inherits": {
"version": "2.0.4", "version": "2.0.4",
"bundled": true, "bundled": true
"optional": true
}, },
"ini": { "ini": {
"version": "1.3.5", "version": "1.3.5",
@@ -11633,7 +11629,6 @@
"is-fullwidth-code-point": { "is-fullwidth-code-point": {
"version": "1.0.0", "version": "1.0.0",
"bundled": true, "bundled": true,
"optional": true,
"requires": { "requires": {
"number-is-nan": "^1.0.0" "number-is-nan": "^1.0.0"
} }
@@ -11659,7 +11654,6 @@
"minipass": { "minipass": {
"version": "2.9.0", "version": "2.9.0",
"bundled": true, "bundled": true,
"optional": true,
"requires": { "requires": {
"safe-buffer": "^5.1.2", "safe-buffer": "^5.1.2",
"yallist": "^3.0.0" "yallist": "^3.0.0"
@@ -11676,7 +11670,6 @@
"mkdirp": { "mkdirp": {
"version": "0.5.1", "version": "0.5.1",
"bundled": true, "bundled": true,
"optional": true,
"requires": { "requires": {
"minimist": "0.0.8" "minimist": "0.0.8"
} }
@@ -11757,8 +11750,7 @@
}, },
"number-is-nan": { "number-is-nan": {
"version": "1.0.1", "version": "1.0.1",
"bundled": true, "bundled": true
"optional": true
}, },
"object-assign": { "object-assign": {
"version": "4.1.1", "version": "4.1.1",
@@ -11768,7 +11760,6 @@
"once": { "once": {
"version": "1.4.0", "version": "1.4.0",
"bundled": true, "bundled": true,
"optional": true,
"requires": { "requires": {
"wrappy": "1" "wrappy": "1"
} }
@@ -11844,8 +11835,7 @@
}, },
"safe-buffer": { "safe-buffer": {
"version": "5.1.2", "version": "5.1.2",
"bundled": true, "bundled": true
"optional": true
}, },
"safer-buffer": { "safer-buffer": {
"version": "2.1.2", "version": "2.1.2",
@@ -11875,7 +11865,6 @@
"string-width": { "string-width": {
"version": "1.0.2", "version": "1.0.2",
"bundled": true, "bundled": true,
"optional": true,
"requires": { "requires": {
"code-point-at": "^1.0.0", "code-point-at": "^1.0.0",
"is-fullwidth-code-point": "^1.0.0", "is-fullwidth-code-point": "^1.0.0",
@@ -11893,7 +11882,6 @@
"strip-ansi": { "strip-ansi": {
"version": "3.0.1", "version": "3.0.1",
"bundled": true, "bundled": true,
"optional": true,
"requires": { "requires": {
"ansi-regex": "^2.0.0" "ansi-regex": "^2.0.0"
} }
@@ -11932,13 +11920,11 @@
}, },
"wrappy": { "wrappy": {
"version": "1.0.2", "version": "1.0.2",
"bundled": true, "bundled": true
"optional": true
}, },
"yallist": { "yallist": {
"version": "3.1.1", "version": "3.1.1",
"bundled": true, "bundled": true
"optional": true
} }
} }
}, },
+42 -20
View File
@@ -1,6 +1,40 @@
<template> <template>
<div> <div>
<h1>Panier</h1> <div class="columns">
<div class="column">
<h1 class="is-size-1">
Panier
</h1>
</div>
</div>
<div class="columns">
<div class="column">
<b-table :data="cart">
<template slot-scope="props">
<b-table-column field="image" label="Image">
<figure class="image is-96x96">
<img class="is-rounded" :src="props.row.images[0].src" :alt="props.row.description">
</figure>
</b-table-column>
<b-table-column field="title" label="Title">
<p class="title">{{ props.row.title }}</p>
</b-table-column>
<b-table-column field="description" label="Description">
<p class="subtitle">{{ props.row.description }}</p>
</b-table-column>
<b-table-column field="price" label="Price">
{{ props.row.variants[0].price }} €
</b-table-column>
<b-table-column field="quantity" label="Quantity">
{{ props.row.count }}
</b-table-column>
<b-table-column field="total" label="Total">
{{ getItemTotal(props.row) }} €
</b-table-column>
</template>
</b-table>
</div>
</div>
<b-button <b-button
type="is-primary" type="is-primary"
@click="confirmCheckout" @click="confirmCheckout"
@@ -11,29 +45,17 @@
</template> </template>
<script> <script>
import cartMixins from '../../mixins/cartMixins'
export default { export default {
name: 'Index', name: 'Index',
computed: { mixins: [cartMixins],
checkout () {
return this.$store.state.checkout
}
},
methods: { methods: {
confirmCheckout () { confirmCheckout () {
this.$shopify.checkout.create().then((checkout) => { window.open(this.$store.state.checkout.webUrl)
// eslint-disable-next-line no-console },
console.log(checkout) getItemTotal (item) {
return item.variants[0].price * item.count
this.$store.state.cart.forEach((item) => {
const lineItemsToAdd = {
variantId: item.variants[0].id,
quantity: item.count
}
// Add an item to the checkout
this.$shopify.checkout.addLineItems(checkout.id, lineItemsToAdd).then((checkout) => {
})
})
})
} }
} }
} }
+5 -5
View File
@@ -5,7 +5,7 @@
<b-button <b-button
icon-left="cart" icon-left="cart"
class="mx-2" class="mx-2"
@click="addToCart(product)" @click="addToCart(product, checkout)"
> >
Ajouter Ajouter
</b-button> </b-button>
@@ -20,14 +20,14 @@ export default {
mixins: [ mixins: [
cartMixins cartMixins
], ],
async asyncData ({ params, $shopify }) {
const product = await $shopify.product.fetchByHandle(params.handle)
return { product }
},
data () { data () {
return { return {
product: {} product: {}
} }
},
async asyncData ({ params, $shopify }) {
const product = await $shopify.product.fetchByHandle(params.handle)
return { product }
} }
} }
</script> </script>
+5 -7
View File
@@ -30,7 +30,7 @@
<b-button <b-button
type="is-primary" type="is-primary"
icon-left="cart" icon-left="cart"
@click="addToCart(product)" @click="addToCart(product, checkout)"
> >
Buy Buy
</b-button> </b-button>
@@ -49,16 +49,14 @@ export default {
mixins: [ mixins: [
cartMixins cartMixins
], ],
async asyncData ({ $shopify, store }) {
const products = await $shopify.product.fetchAll()
store.commit('setProducts', products)
},
computed: { computed: {
products () { products () {
return this.$store.state.products return this.$store.state.products
} }
},
async asyncData ({ $shopify, store }) {
const products = await $shopify.product.fetchAll()
// eslint-disable-next-line no-console
console.log(products)
store.commit('setProducts', products)
} }
} }
</script> </script>
+3 -5
View File
@@ -11,7 +11,7 @@ export const mutations = {
setCheckout (state, data) { setCheckout (state, data) {
state.checkout = data state.checkout = data
}, },
incrementCart (sate, index) { incrementCart (state, index) {
state.cart[index].count++ state.cart[index].count++
}, },
addCart (state, item) { addCart (state, item) {
@@ -27,13 +27,11 @@ export const getters = {
} }
export const actions = { export const actions = {
addItemToCart ({ commit, getters }, payload) { addItemToCart ({ commit, getters, state }, payload) {
return new Promise((resolve) => { return new Promise((resolve) => {
const element = getters.getCartItem(payload.id) const element = getters.getCartItem(payload.id)
// eslint-disable-next-line no-console
console.log(element)
if (element.length > 0) { if (element.length > 0) {
commit('incrementCart', payload.id) commit('incrementCart', state.cart.indexOf(element[0]))
} else { } else {
commit('addCart', payload) commit('addCart', payload)
} }