[FIX] website_sale_aplicoop: Arreglar botón Home Delivery en shop (añadir/remover producto de entrega al carrito)
This commit is contained in:
parent
4a668b3240
commit
130a5ff6c4
3 changed files with 79 additions and 52 deletions
|
|
@ -1463,6 +1463,18 @@ class AplicoopWebsiteSale(WebsiteSale):
|
||||||
session_key = f"eskaera_{order_id}"
|
session_key = f"eskaera_{order_id}"
|
||||||
cart = request.session.get(session_key, {})
|
cart = request.session.get(session_key, {})
|
||||||
|
|
||||||
|
# Get delivery product from group_order (configured per group order)
|
||||||
|
delivery_product = group_order.delivery_product_id
|
||||||
|
delivery_product_id = delivery_product.id if delivery_product else None
|
||||||
|
# Get translated product name based on current language
|
||||||
|
if delivery_product:
|
||||||
|
delivery_product_translated = delivery_product.with_context(
|
||||||
|
lang=request.env.lang
|
||||||
|
)
|
||||||
|
delivery_product_name = delivery_product_translated.name
|
||||||
|
else:
|
||||||
|
delivery_product_name = "Home Delivery"
|
||||||
|
|
||||||
# Get translated labels for JavaScript (same as checkout)
|
# Get translated labels for JavaScript (same as checkout)
|
||||||
labels = self.get_checkout_labels()
|
labels = self.get_checkout_labels()
|
||||||
|
|
||||||
|
|
@ -1482,6 +1494,11 @@ class AplicoopWebsiteSale(WebsiteSale):
|
||||||
"product_supplier_info": product_supplier_info,
|
"product_supplier_info": product_supplier_info,
|
||||||
"product_price_info": product_price_info,
|
"product_price_info": product_price_info,
|
||||||
"product_display_info": product_display_info,
|
"product_display_info": product_display_info,
|
||||||
|
"delivery_product_id": delivery_product_id,
|
||||||
|
"delivery_product_name": delivery_product_name,
|
||||||
|
"delivery_product_price": (
|
||||||
|
delivery_product.list_price if delivery_product else 5.74
|
||||||
|
),
|
||||||
"labels": labels,
|
"labels": labels,
|
||||||
"labels_json": json.dumps(labels, ensure_ascii=False),
|
"labels_json": json.dumps(labels, ensure_ascii=False),
|
||||||
"lazy_loading_enabled": lazy_loading_enabled,
|
"lazy_loading_enabled": lazy_loading_enabled,
|
||||||
|
|
|
||||||
|
|
@ -14,10 +14,13 @@
|
||||||
homeDeliveryEnabled: false,
|
homeDeliveryEnabled: false,
|
||||||
|
|
||||||
init: function () {
|
init: function () {
|
||||||
// Get delivery product info from data attributes
|
// Get delivery product info from data attributes (from checkout or shop page)
|
||||||
var checkoutPage = document.querySelector(".eskaera-checkout-page");
|
var checkoutPage = document.querySelector(".eskaera-checkout-page");
|
||||||
if (checkoutPage) {
|
var shopPage = document.querySelector(".eskaera-shop-page");
|
||||||
this.deliveryProductId = checkoutPage.getAttribute("data-delivery-product-id");
|
var page = checkoutPage || shopPage;
|
||||||
|
|
||||||
|
if (page) {
|
||||||
|
this.deliveryProductId = page.getAttribute("data-delivery-product-id");
|
||||||
console.log(
|
console.log(
|
||||||
"[HomeDelivery] deliveryProductId from attribute:",
|
"[HomeDelivery] deliveryProductId from attribute:",
|
||||||
this.deliveryProductId,
|
this.deliveryProductId,
|
||||||
|
|
@ -25,13 +28,13 @@
|
||||||
typeof this.deliveryProductId
|
typeof this.deliveryProductId
|
||||||
);
|
);
|
||||||
|
|
||||||
var price = checkoutPage.getAttribute("data-delivery-product-price");
|
var price = page.getAttribute("data-delivery-product-price");
|
||||||
if (price) {
|
if (price) {
|
||||||
this.deliveryProductPrice = parseFloat(price);
|
this.deliveryProductPrice = parseFloat(price);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get translated product name from data attribute (auto-translated by Odoo server)
|
// Get translated product name from data attribute (auto-translated by Odoo server)
|
||||||
var productName = checkoutPage.getAttribute("data-delivery-product-name");
|
var productName = page.getAttribute("data-delivery-product-name");
|
||||||
if (productName) {
|
if (productName) {
|
||||||
this.deliveryProductName = productName;
|
this.deliveryProductName = productName;
|
||||||
console.log(
|
console.log(
|
||||||
|
|
@ -41,14 +44,16 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if home delivery is enabled for this order
|
// Check if home delivery is enabled for this order
|
||||||
var homeDeliveryAttr = checkoutPage.getAttribute("data-home-delivery-enabled");
|
var homeDeliveryAttr = page.getAttribute("data-home-delivery-enabled");
|
||||||
this.homeDeliveryEnabled =
|
this.homeDeliveryEnabled =
|
||||||
homeDeliveryAttr === "true" || homeDeliveryAttr === "True";
|
homeDeliveryAttr === "true" || homeDeliveryAttr === "True";
|
||||||
console.log("[HomeDelivery] Home delivery enabled:", this.homeDeliveryEnabled);
|
console.log("[HomeDelivery] Home delivery enabled:", this.homeDeliveryEnabled);
|
||||||
|
|
||||||
// Show/hide home delivery section based on configuration
|
// Show/hide home delivery section based on configuration (only on checkout)
|
||||||
|
if (checkoutPage) {
|
||||||
this.toggleHomeDeliverySection();
|
this.toggleHomeDeliverySection();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Get order ID from confirm button
|
// Get order ID from confirm button
|
||||||
var confirmBtn = document.getElementById("confirm-order-btn");
|
var confirmBtn = document.getElementById("confirm-order-btn");
|
||||||
|
|
@ -73,6 +78,52 @@
|
||||||
|
|
||||||
// Check if delivery product is already in cart on page load
|
// Check if delivery product is already in cart on page load
|
||||||
this.checkDeliveryInCart();
|
this.checkDeliveryInCart();
|
||||||
|
|
||||||
|
// Vincular botón Home Delivery en el shop (cart header)
|
||||||
|
this.bindShopHomeDeliveryButton();
|
||||||
|
},
|
||||||
|
|
||||||
|
bindShopHomeDeliveryButton: function () {
|
||||||
|
var self = this;
|
||||||
|
var homeDeliveryBtn = document.getElementById("home-delivery-btn");
|
||||||
|
|
||||||
|
if (homeDeliveryBtn) {
|
||||||
|
console.log("[HomeDelivery] Binding shop home delivery button");
|
||||||
|
|
||||||
|
homeDeliveryBtn.addEventListener("click", function () {
|
||||||
|
var cart = self.getCart();
|
||||||
|
var isInCart = cart[self.deliveryProductId];
|
||||||
|
|
||||||
|
if (isInCart) {
|
||||||
|
// Remove delivery product
|
||||||
|
console.log("[HomeDelivery] Removing delivery product from shop cart");
|
||||||
|
self.removeDeliveryProduct();
|
||||||
|
homeDeliveryBtn.classList.remove("active", "btn-warning");
|
||||||
|
homeDeliveryBtn.classList.add("btn-outline-warning");
|
||||||
|
} else {
|
||||||
|
// Add delivery product
|
||||||
|
console.log("[HomeDelivery] Adding delivery product from shop cart");
|
||||||
|
self.addDeliveryProduct();
|
||||||
|
homeDeliveryBtn.classList.remove("btn-outline-warning");
|
||||||
|
homeDeliveryBtn.classList.add("active", "btn-warning");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Trigger cart reload to update UI
|
||||||
|
if (typeof window.renderCheckoutSummary === "function") {
|
||||||
|
window.renderCheckoutSummary();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Set initial button state
|
||||||
|
var cart = self.getCart();
|
||||||
|
if (cart[self.deliveryProductId]) {
|
||||||
|
homeDeliveryBtn.classList.add("active", "btn-warning");
|
||||||
|
homeDeliveryBtn.classList.remove("btn-outline-warning");
|
||||||
|
} else {
|
||||||
|
homeDeliveryBtn.classList.add("btn-outline-warning");
|
||||||
|
homeDeliveryBtn.classList.remove("active", "btn-warning");
|
||||||
|
}
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
toggleHomeDeliverySection: function () {
|
toggleHomeDeliverySection: function () {
|
||||||
|
|
@ -133,48 +184,10 @@
|
||||||
localStorage.setItem(cartKey, JSON.stringify(cart));
|
localStorage.setItem(cartKey, JSON.stringify(cart));
|
||||||
|
|
||||||
// Re-render checkout summary without reloading
|
// Re-render checkout summary without reloading
|
||||||
var self = this;
|
|
||||||
setTimeout(function () {
|
setTimeout(function () {
|
||||||
// Use the global function from checkout_labels.js
|
// Use the global function from checkout_labels.js
|
||||||
if (typeof window.renderCheckoutSummary === "function") {
|
if (typeof window.renderCheckoutSummary === "function") {
|
||||||
window.renderCheckoutSummary();
|
window.renderCheckoutSummary();
|
||||||
|
|
||||||
// Vincular botón Entrega a Casa en carrito (shop)
|
|
||||||
var homeDeliveryBtn = document.getElementById("home-delivery-btn");
|
|
||||||
if (homeDeliveryBtn) {
|
|
||||||
homeDeliveryBtn.addEventListener("click", function () {
|
|
||||||
// Simular click en checkbox de checkout si existe
|
|
||||||
if (checkbox) {
|
|
||||||
checkbox.checked = !checkbox.checked;
|
|
||||||
var event = new Event("change", { bubbles: true });
|
|
||||||
checkbox.dispatchEvent(event);
|
|
||||||
} else {
|
|
||||||
// Alternar home delivery en localStorage/cart
|
|
||||||
// Aquí puedes implementar lógica para shop
|
|
||||||
console.log("[HomeDelivery] Toggle home delivery from cart header");
|
|
||||||
// TODO: Actualizar carrito y mostrar info
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Cargar borrador automáticamente al entrar en shop
|
|
||||||
var cartPage = document.querySelector(".eskaera-shop-page");
|
|
||||||
if (cartPage) {
|
|
||||||
var orderId = cartPage.getAttribute("data-order-id") || this.orderId;
|
|
||||||
var cartKey = "eskaera_" + orderId + "_cart";
|
|
||||||
var savedCart = localStorage.getItem(cartKey);
|
|
||||||
if (savedCart) {
|
|
||||||
try {
|
|
||||||
var cart = JSON.parse(savedCart);
|
|
||||||
var event = new CustomEvent("cartLoaded", { detail: { cart: cart } });
|
|
||||||
document.dispatchEvent(event);
|
|
||||||
console.log("[SHOP AUTO-LOAD] Cart loaded from localStorage");
|
|
||||||
} catch (e) {
|
|
||||||
console.error("[SHOP AUTO-LOAD] Error parsing cart:", e);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
console.log("[SHOP AUTO-LOAD] No cart found in localStorage");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}, 50);
|
}, 50);
|
||||||
},
|
},
|
||||||
|
|
|
||||||
|
|
@ -212,7 +212,7 @@
|
||||||
</template>
|
</template>
|
||||||
<template id="eskaera_shop" name="Eskaera Shop">
|
<template id="eskaera_shop" name="Eskaera Shop">
|
||||||
<t t-call="website.layout">
|
<t t-call="website.layout">
|
||||||
<div id="wrap" class="eskaera-shop-page oe_structure oe_empty" data-name="Eskaera Shop">
|
<div id="wrap" class="eskaera-shop-page oe_structure oe_empty" data-name="Eskaera Shop" t-attf-data-delivery-product-id="{{ delivery_product_id }}" t-attf-data-delivery-product-name="{{ delivery_product_name }}" t-attf-data-delivery-product-price="{{ delivery_product_price }}" t-attf-data-home-delivery-enabled="{{ 'true' if group_order.home_delivery else 'false' }}">
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<div class="row mb-4">
|
<div class="row mb-4">
|
||||||
<div class="col-lg-12">
|
<div class="col-lg-12">
|
||||||
|
|
@ -352,9 +352,6 @@
|
||||||
<button type="button" class="btn btn-primary cart-btn-compact" id="save-cart-btn" t-attf-data-order-id="{{ group_order.id }}" data-bs-title="Save Cart" data-bs-toggle="tooltip">
|
<button type="button" class="btn btn-primary cart-btn-compact" id="save-cart-btn" t-attf-data-order-id="{{ group_order.id }}" data-bs-title="Save Cart" data-bs-toggle="tooltip">
|
||||||
<i class="fa fa-save cart-icon-size" />
|
<i class="fa fa-save cart-icon-size" />
|
||||||
</button>
|
</button>
|
||||||
<button type="button" class="btn btn-warning cart-btn-compact" id="home-delivery-btn" t-attf-data-order-id="{{ group_order.id }}" data-bs-title="Home Delivery" data-bs-toggle="tooltip">
|
|
||||||
<i class="fa fa-truck cart-icon-size" />
|
|
||||||
</button>
|
|
||||||
<a t-attf-href="/eskaera/{{ group_order.id }}/checkout" class="btn btn-success cart-btn-compact" aria-label="Proceed to checkout" data-bs-title="Proceed to Checkout" data-bs-toggle="tooltip">
|
<a t-attf-href="/eskaera/{{ group_order.id }}/checkout" class="btn btn-success cart-btn-compact" aria-label="Proceed to checkout" data-bs-title="Proceed to Checkout" data-bs-toggle="tooltip">
|
||||||
<i class="fa fa-check cart-icon-size" aria-hidden="true" />
|
<i class="fa fa-check cart-icon-size" aria-hidden="true" />
|
||||||
</a>
|
</a>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue