36
36
<script setup>
37
37
import { Form , Field , ErrorMessage } from " vee-validate" ;
38
38
import { uid } from " uid" ;
39
- import { onMounted } from " vue" ;
39
+ import { ref , onMounted , watch } from " vue" ;
40
40
import { useCart } from " @/store/useCart" ;
41
41
42
42
import { BILLING_FIELDS , BILLING_SCHEMA } from " ./constants/BILLING_FIELDS" ;
43
43
44
44
import CHECKOUT_MUTATION from " @/apollo/mutations/CHECKOUT_MUTATION.gql" ;
45
+ import GET_CART_QUERY from " @/apollo/queries/GET_CART_QUERY.gql" ;
45
46
46
47
/**
47
48
* Returns an input string with its first character capitalized.
@@ -54,6 +55,7 @@ const upperCaseFirstChar = (input) =>
54
55
55
56
const paymentMethod = " cod" ;
56
57
const cart = useCart ();
58
+ const hasSession = ref (false );
57
59
58
60
// Clear WooCommerce session
59
61
const clearWooCommerceSession = () => {
@@ -63,9 +65,45 @@ const clearWooCommerceSession = () => {
63
65
}
64
66
};
65
67
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
67
104
onMounted (async () => {
68
- await cart .refetch ();
105
+ await refetchCartQuery ();
106
+ checkSession ();
69
107
});
70
108
71
109
/**
0 commit comments