36
36
<script setup>
37
37
import { Form , Field , ErrorMessage } from " vee-validate" ;
38
38
import { uid } from " uid" ;
39
+ import { onMounted } from " vue" ;
40
+ import { useCart } from " @/store/useCart" ;
39
41
40
42
import { BILLING_FIELDS , BILLING_SCHEMA } from " ./constants/BILLING_FIELDS" ;
41
43
@@ -51,11 +53,25 @@ const upperCaseFirstChar = (input) =>
51
53
input .charAt (0 ).toUpperCase () + input .slice (1 );
52
54
53
55
const paymentMethod = " cod" ;
56
+ const cart = useCart ();
57
+
58
+ // Clear WooCommerce session
59
+ const clearWooCommerceSession = () => {
60
+ if (process .client ) {
61
+ localStorage .removeItem (" woo-session" );
62
+ localStorage .removeItem (" woocommerce-cart" );
63
+ }
64
+ };
65
+
66
+ // Ensure cart is loaded on mount
67
+ onMounted (async () => {
68
+ await cart .refetch ();
69
+ });
54
70
55
71
/**
56
72
* Handles the submission of a checkout form with the provided user information.
57
73
*/
58
- const handleSubmit = ({
74
+ const handleSubmit = async ({
59
75
firstName,
60
76
lastName,
61
77
address1,
@@ -92,28 +108,32 @@ const handleSubmit = ({
92
108
transactionId: " hjkhjkhsdsdiui" ,
93
109
};
94
110
95
- const { mutate , onDone , onError } = useMutation (CHECKOUT_MUTATION );
111
+ try {
112
+ const { mutate } = useMutation (CHECKOUT_MUTATION );
113
+
114
+ const result = await mutate ({
115
+ input: checkoutData,
116
+ });
96
117
97
- mutate ({ input: checkoutData });
98
-
99
- onDone (async (result ) => {
100
- const { data } = result;
101
- if (data? .checkout ? .result === " success" ) {
118
+ if (result? .data ? .checkout ? .result === " success" ) {
119
+ clearWooCommerceSession ();
120
+ await cart .refetch ();
102
121
await navigateTo (" /success" );
103
122
} else {
104
123
alert (" Error, order not placed. Please try again." );
124
+ await cart .refetch ();
105
125
}
106
- });
107
-
108
- onError ((error ) => {
126
+ } catch (error) {
109
127
console .error (" Checkout error:" , error);
128
+
110
129
if (error .message .includes (" session" )) {
111
- alert (
112
- " Your session has expired. Please refresh the page and try again." ,
113
- );
130
+ clearWooCommerceSession ();
131
+ alert (" Your session has expired. Please refresh the page and try again." );
114
132
} else {
115
133
alert (" An unexpected error occurred. Please try again." );
116
134
}
117
- });
135
+
136
+ await cart .refetch ();
137
+ }
118
138
};
119
139
< / script>
0 commit comments