Skip to content

Commit 364391b

Browse files
committed
Fix bug with add to cart
1 parent 7b066ed commit 364391b

File tree

4 files changed

+32
-22
lines changed

4 files changed

+32
-22
lines changed

nuxt3/components/Cart/CartAddToCartButton.vue

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ const addProductToCart = (product) => {
6363
onError(() => {
6464
isLoading.value = false
6565
isError.value = true
66-
//mutate(variables)
6766
})
6867
}
6968
</script>

nuxt3/components/Layout/LayoutCart.vue

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
<script setup>
3838
import GET_CART_QUERY from '@/apollo/queries/GET_CART_QUERY.gql'
3939
40+
import { getCookie } from '@/utils/functions'
41+
4042
const cartLength = useState('cartLength', () => 0)
4143
const subTotal = useState('subTotal', '')
4244
const remoteError = useState('remoteError', () => false)
@@ -49,7 +51,7 @@ const updateCartDisplay = () => {
4951
if (!data) {
5052
return
5153
}
52-
54+
5355
cartLength.value = data.value.cart.contents.nodes.reduce(
5456
(accumulator, argument) => accumulator + argument.quantity,
5557
0
@@ -61,9 +63,11 @@ const updateCartDisplay = () => {
6163
}
6264
6365
setInterval(() => {
64-
if (!pending.value) {
65-
execute()
66-
updateCartDisplay()
66+
if (process.client) {
67+
if (!pending.value && getCookie('woo-session')) {
68+
execute()
69+
updateCartDisplay()
70+
}
6771
}
6872
}, 3000)
6973
</script>

nuxt3/plugins/apollo.js

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,12 @@ export default defineNuxtPlugin((nuxtApp) => {
2424
* If session data exist in local storage, set value as session header.
2525
*/
2626

27-
if (process.client) {
28-
const session = localStorage.getItem('woo-session') || 'test'
29-
30-
if (session && session.length > 0) {
31-
operation.setContext(() => ({
32-
headers: {
33-
'woocommerce-session': `Session ${session}`,
34-
},
35-
}))
36-
}
27+
if (process.client && cookie.value) {
28+
operation.setContext(() => ({
29+
headers: {
30+
'woocommerce-session': `Session ${cookie.value}`,
31+
},
32+
}))
3733
}
3834

3935
return forward(operation)
@@ -50,13 +46,10 @@ export default defineNuxtPlugin((nuxtApp) => {
5046
response: { headers },
5147
} = context
5248

53-
const session =
54-
headers.get('woocommerce-session') ||
55-
localStorage.getItem('woo-session')
49+
const session = headers.get('woocommerce-session') || cookie.value
5650

5751
if (process.client && session) {
5852
cookie.value = session
59-
localStorage.setItem('woo-session', session)
6053
}
6154
return response
6255
})
@@ -68,13 +61,12 @@ export default defineNuxtPlugin((nuxtApp) => {
6861
// Create the apollo client
6962
const apolloClient = new ApolloClient({
7063
link: middleware.concat(afterware.concat(httpLink)),
71-
7264
cache,
7365
})
7466

67+
provideApolloClient(apolloClient)
68+
7569
nuxtApp.hook('apollo:auth', ({ token }) => {
7670
token.value = cookie.value
77-
78-
provideApolloClient(apolloClient)
7971
})
8072
})

nuxt3/utils/functions.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,18 @@ export function createCheckoutData(form) {
5959
transactionId: 'test',
6060
}
6161
}
62+
63+
/**
64+
* Get specific cookie from document.cookie
65+
* @param {String} cName Name of cookie to return
66+
*/
67+
export function getCookie(cName) {
68+
const name = cName + '='
69+
const cDecoded = decodeURIComponent(document.cookie)
70+
const cArr = cDecoded.split('; ')
71+
let res
72+
cArr.forEach((val) => {
73+
if (val.indexOf(name) === 0) res = val.substring(name.length)
74+
})
75+
return res
76+
}

0 commit comments

Comments
 (0)