Skip to content

Commit e2ff4a5

Browse files
authored
Merge pull request #1472 from w3bdesign/dev
Fix checkout
2 parents a662113 + f926bdb commit e2ff4a5

File tree

2 files changed

+54
-5
lines changed

2 files changed

+54
-5
lines changed

components/Checkout/CheckoutForm.vue

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,13 @@
3636
<script setup>
3737
import { Form, Field, ErrorMessage } from "vee-validate";
3838
import { uid } from "uid";
39-
import { onMounted } from "vue";
39+
import { ref, onMounted, watch } from "vue";
4040
import { useCart } from "@/store/useCart";
4141
4242
import { BILLING_FIELDS, BILLING_SCHEMA } from "./constants/BILLING_FIELDS";
4343
4444
import CHECKOUT_MUTATION from "@/apollo/mutations/CHECKOUT_MUTATION.gql";
45+
import GET_CART_QUERY from "@/apollo/queries/GET_CART_QUERY.gql";
4546
4647
/**
4748
* Returns an input string with its first character capitalized.
@@ -54,6 +55,7 @@ const upperCaseFirstChar = (input) =>
5455
5556
const paymentMethod = "cod";
5657
const cart = useCart();
58+
const hasSession = ref(false);
5759
5860
// Clear WooCommerce session
5961
const clearWooCommerceSession = () => {
@@ -63,9 +65,45 @@ const clearWooCommerceSession = () => {
6365
}
6466
};
6567
66-
// Ensure cart is loaded on mount
68+
// Check if we have a valid session
69+
const checkSession = () => {
70+
if (process.client) {
71+
const sessionData = localStorage.getItem("woo-session");
72+
if (sessionData) {
73+
try {
74+
const parsed = JSON.parse(sessionData);
75+
hasSession.value = !!(parsed && parsed.token);
76+
} catch (e) {
77+
hasSession.value = false;
78+
}
79+
} else {
80+
hasSession.value = false;
81+
}
82+
}
83+
};
84+
85+
// Use query to establish session
86+
const { result: cartResult, refetch: refetchCartQuery } = useQuery(
87+
GET_CART_QUERY,
88+
null,
89+
{
90+
notifyOnNetworkStatusChange: true,
91+
fetchPolicy: "network-only",
92+
}
93+
);
94+
95+
// Watch for cart result to update session status
96+
watch(cartResult, (newResult) => {
97+
if (newResult && newResult.cart) {
98+
checkSession();
99+
// The cart store will automatically update via its own watcher
100+
}
101+
});
102+
103+
// Ensure cart is loaded and session established on mount
67104
onMounted(async () => {
68-
await cart.refetch();
105+
await refetchCartQuery();
106+
checkSession();
69107
});
70108
71109
/**

pages/checkout.vue

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
<template>
22
<div>
3-
<CartContents />
4-
<CheckoutForm />
3+
<div v-if="cart.cart.length > 0">
4+
<CartContents />
5+
<CheckoutForm />
6+
</div>
7+
<div v-else>
8+
<h1 class="text-2xl m-12 mt-24 font-bold text-center">
9+
Ingen produkter i handlekurven
10+
</h1>
11+
</div>
512
</div>
613
</template>
714

815
<script setup>
16+
import { useCart } from "@/store/useCart";
17+
18+
const cart = useCart();
19+
920
useHead({
1021
title: "Checkout",
1122
titleTemplate: "%s - Nuxt 3 Woocommerce",

0 commit comments

Comments
 (0)