Skip to content

Commit a662113

Browse files
authored
Merge pull request #1471 from w3bdesign/dev
Fix checkout
2 parents 233e7e9 + 6c9ca8d commit a662113

File tree

1 file changed

+34
-14
lines changed

1 file changed

+34
-14
lines changed

components/Checkout/CheckoutForm.vue

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
<script setup>
3737
import { Form, Field, ErrorMessage } from "vee-validate";
3838
import { uid } from "uid";
39+
import { onMounted } from "vue";
40+
import { useCart } from "@/store/useCart";
3941
4042
import { BILLING_FIELDS, BILLING_SCHEMA } from "./constants/BILLING_FIELDS";
4143
@@ -51,11 +53,25 @@ const upperCaseFirstChar = (input) =>
5153
input.charAt(0).toUpperCase() + input.slice(1);
5254
5355
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+
});
5470
5571
/**
5672
* Handles the submission of a checkout form with the provided user information.
5773
*/
58-
const handleSubmit = ({
74+
const handleSubmit = async ({
5975
firstName,
6076
lastName,
6177
address1,
@@ -92,28 +108,32 @@ const handleSubmit = ({
92108
transactionId: "hjkhjkhsdsdiui",
93109
};
94110
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+
});
96117
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();
102121
await navigateTo("/success");
103122
} else {
104123
alert("Error, order not placed. Please try again.");
124+
await cart.refetch();
105125
}
106-
});
107-
108-
onError((error) => {
126+
} catch (error) {
109127
console.error("Checkout error:", error);
128+
110129
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.");
114132
} else {
115133
alert("An unexpected error occurred. Please try again.");
116134
}
117-
});
135+
136+
await cart.refetch();
137+
}
118138
};
119139
</script>

0 commit comments

Comments
 (0)