Skip to content

Fix checkout #1471

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 28, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 34 additions & 14 deletions components/Checkout/CheckoutForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
<script setup>
import { Form, Field, ErrorMessage } from "vee-validate";
import { uid } from "uid";
import { onMounted } from "vue";
import { useCart } from "@/store/useCart";

import { BILLING_FIELDS, BILLING_SCHEMA } from "./constants/BILLING_FIELDS";

Expand All @@ -51,11 +53,25 @@ const upperCaseFirstChar = (input) =>
input.charAt(0).toUpperCase() + input.slice(1);

const paymentMethod = "cod";
const cart = useCart();

// Clear WooCommerce session
const clearWooCommerceSession = () => {
if (process.client) {
localStorage.removeItem("woo-session");
localStorage.removeItem("woocommerce-cart");
}
};

// Ensure cart is loaded on mount
onMounted(async () => {
await cart.refetch();
});

/**
* Handles the submission of a checkout form with the provided user information.
*/
const handleSubmit = ({
const handleSubmit = async ({
firstName,
lastName,
address1,
Expand Down Expand Up @@ -92,28 +108,32 @@ const handleSubmit = ({
transactionId: "hjkhjkhsdsdiui",
};

const { mutate, onDone, onError } = useMutation(CHECKOUT_MUTATION);
try {
const { mutate } = useMutation(CHECKOUT_MUTATION);

const result = await mutate({
input: checkoutData,
});

mutate({ input: checkoutData });

onDone(async (result) => {
const { data } = result;
if (data?.checkout?.result === "success") {
if (result?.data?.checkout?.result === "success") {
clearWooCommerceSession();
await cart.refetch();
await navigateTo("/success");
} else {
alert("Error, order not placed. Please try again.");
await cart.refetch();
}
});

onError((error) => {
} catch (error) {
console.error("Checkout error:", error);

if (error.message.includes("session")) {
alert(
"Your session has expired. Please refresh the page and try again.",
);
clearWooCommerceSession();
alert("Your session has expired. Please refresh the page and try again.");
} else {
alert("An unexpected error occurred. Please try again.");
}
});

await cart.refetch();
}
};
</script>