[FIX] website_sale_aplicoop: fix home delivery draft flow
This commit is contained in:
parent
135967019e
commit
e9809b90e9
2 changed files with 33 additions and 124 deletions
|
|
@ -1737,6 +1737,11 @@
|
|||
items: items,
|
||||
};
|
||||
|
||||
var deliveryCheckbox = document.getElementById("home-delivery-checkbox");
|
||||
if (deliveryCheckbox) {
|
||||
orderData.is_delivery = !!deliveryCheckbox.checked;
|
||||
}
|
||||
|
||||
var xhr = new XMLHttpRequest();
|
||||
xhr.open("POST", "/eskaera/save-order", true);
|
||||
xhr.setRequestHeader("Content-Type", "application/json");
|
||||
|
|
@ -1795,120 +1800,6 @@
|
|||
xhr.send(JSON.stringify(orderData));
|
||||
},
|
||||
|
||||
_confirmOrder: function () {
|
||||
console.log("=== _confirmOrder started ===");
|
||||
console.log("orderId:", this.orderId);
|
||||
|
||||
// IMPORTANT: Read cart directly from localStorage, not from this.cart
|
||||
// because home_delivery.js updates localStorage directly
|
||||
var cartKey = "eskaera_" + this.orderId + "_cart";
|
||||
var storedCart = localStorage.getItem(cartKey);
|
||||
console.log("localStorage[" + cartKey + "]:", storedCart);
|
||||
|
||||
// Parse cart from localStorage (more reliable than this.cart)
|
||||
var cart = storedCart ? JSON.parse(storedCart) : this.cart;
|
||||
console.log("Parsed cart from localStorage:", cart);
|
||||
|
||||
var self = this;
|
||||
var items = [];
|
||||
|
||||
Object.keys(cart).forEach(function (productId) {
|
||||
var item = cart[productId];
|
||||
console.log("Processing cart item: productId=" + productId + ", item=", item);
|
||||
items.push({
|
||||
product_id: productId,
|
||||
product_name: item.name,
|
||||
quantity: item.qty,
|
||||
unit_price: item.price,
|
||||
});
|
||||
});
|
||||
|
||||
console.log("Items to send to server:", items);
|
||||
console.log("Total items count:", items.length);
|
||||
|
||||
// Check if home delivery is enabled
|
||||
var deliveryCheckbox = document.getElementById("home-delivery-checkbox");
|
||||
var isDelivery = deliveryCheckbox ? deliveryCheckbox.checked : false;
|
||||
|
||||
var orderData = {
|
||||
order_id: this.orderId,
|
||||
items: items,
|
||||
is_delivery: isDelivery,
|
||||
};
|
||||
|
||||
var xhr = new XMLHttpRequest();
|
||||
xhr.open("POST", "/eskaera/confirm", true);
|
||||
xhr.setRequestHeader("Content-Type", "application/json");
|
||||
|
||||
console.log("Sending request to /eskaera/confirm with data:", orderData);
|
||||
|
||||
xhr.onload = function () {
|
||||
if (xhr.status === 200) {
|
||||
try {
|
||||
var data = JSON.parse(xhr.responseText);
|
||||
console.log("Response:", data);
|
||||
var labels = self._getLabels();
|
||||
if (data.success) {
|
||||
var message =
|
||||
data.message || labels.order_confirmed || "Order confirmed";
|
||||
self._showNotification("✓ " + message, "success", 4000);
|
||||
// Clear cart from localStorage
|
||||
localStorage.removeItem("eskaera_" + self.orderId + "_cart");
|
||||
console.log("Cart cleared from localStorage after confirmation");
|
||||
// Save confirmation message in sessionStorage to display on eskaera page
|
||||
sessionStorage.setItem("confirmation_message", message);
|
||||
// Wait a moment before redirecting to let user see the message
|
||||
setTimeout(function () {
|
||||
window.location.href = data.redirect_url || "/eskaera";
|
||||
}, 4000);
|
||||
} else {
|
||||
var unknownErrorMsg =
|
||||
labels.error_unknown || labels.error_unknown || "Unknown error";
|
||||
self._showNotification(
|
||||
"Error: " + (data.error || unknownErrorMsg),
|
||||
"danger"
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
console.error("Error parsing response:", e);
|
||||
var labels = self._getLabels();
|
||||
self._showNotification(
|
||||
labels.error_processing_response || "Error processing response",
|
||||
"danger"
|
||||
);
|
||||
}
|
||||
} else {
|
||||
if (self._isClosedOrderResponse(xhr)) {
|
||||
self._clearCurrentOrderCartSilently();
|
||||
self._updateCartDisplay();
|
||||
return;
|
||||
}
|
||||
try {
|
||||
var errorData = JSON.parse(xhr.responseText);
|
||||
console.error("HTTP error:", xhr.status, errorData);
|
||||
self._showNotification(
|
||||
"Error " + xhr.status + ": " + (errorData.error || "Request error"),
|
||||
"danger"
|
||||
);
|
||||
} catch (e) {
|
||||
console.error("HTTP error:", xhr.status, xhr.responseText);
|
||||
self._showNotification(
|
||||
"Error confirming order (HTTP " + xhr.status + ")",
|
||||
"danger"
|
||||
);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
xhr.onerror = function () {
|
||||
console.error("Error:", xhr);
|
||||
var labels = self._getLabels();
|
||||
self._showNotification(labels.connection_error || "Connection error", "danger");
|
||||
};
|
||||
|
||||
xhr.send(JSON.stringify(orderData));
|
||||
},
|
||||
|
||||
escapeHtml: function (text) {
|
||||
var div = document.createElement("div");
|
||||
div.textContent = text;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue