corregidos bugs de: ret, vs delivery, efectivo vs link, charsets, price query

This commit is contained in:
Lucas Tettamanti
2026-01-26 23:27:47 -03:00
parent 53293ce9b3
commit 493f26af17
13 changed files with 757 additions and 193 deletions

View File

@@ -188,7 +188,7 @@ function mapAddress(address) {
};
}
export async function createOrder({ tenantId, wooCustomerId, basket, address, run_id }) {
export async function createOrder({ tenantId, wooCustomerId, basket, address, shippingMethod, paymentMethod, run_id }) {
const lockKey = `${tenantId}:${wooCustomerId || "anon"}`;
return withLock(lockKey, async () => {
const client = await getWooClient({ tenantId });
@@ -200,9 +200,15 @@ export async function createOrder({ tenantId, wooCustomerId, basket, address, ru
customer_id: wooCustomerId || undefined,
line_items: lineItems,
...(addr ? { billing: addr, shipping: addr } : {}),
// Si es cash, usar payment_method "cod" (Cash On Delivery) de WooCommerce
...(paymentMethod === "cash" ? { payment_method: "cod", payment_method_title: "Efectivo" } : {}),
meta_data: [
{ key: "source", value: "whatsapp" },
...(run_id ? [{ key: "run_id", value: run_id }] : []),
// Guardar shipping_method como metadata para poder leerlo después
...(shippingMethod ? [{ key: "shipping_method", value: shippingMethod }] : []),
// Guardar payment_method como metadata también
...(paymentMethod ? [{ key: "payment_method_wa", value: paymentMethod }] : []),
],
};
const url = `${client.base}/orders`;
@@ -313,17 +319,34 @@ export async function listRecentOrders({ tenantId, limit = 20 }) {
const source = sourceMeta?.value || "web";
// Método de envío (shipping)
// 1. Primero intentar leer de metadata (para pedidos de WhatsApp)
const metaShippingMethod = order.meta_data?.find(m => m.key === "shipping_method")?.value || null;
// 2. Fallback a shipping_lines de WooCommerce (para pedidos web)
const shippingLines = order.shipping_lines || [];
const shippingMethod = shippingLines[0]?.method_title || shippingLines[0]?.method_id || null;
const isDelivery = shippingMethod ?
!shippingMethod.toLowerCase().includes("retiro") &&
!shippingMethod.toLowerCase().includes("pickup") &&
!shippingMethod.toLowerCase().includes("local") : false;
const wooShippingMethod = shippingLines[0]?.method_title || shippingLines[0]?.method_id || null;
// 3. Usar metadata si existe, sino WooCommerce
const shippingMethod = metaShippingMethod || wooShippingMethod;
// 4. Determinar isDelivery
let isDelivery = false;
if (metaShippingMethod) {
// Si viene de metadata, "delivery" = true, "pickup" = false
isDelivery = metaShippingMethod === "delivery";
} else if (wooShippingMethod) {
// Si viene de WooCommerce, detectar por nombre
isDelivery = !wooShippingMethod.toLowerCase().includes("retiro") &&
!wooShippingMethod.toLowerCase().includes("pickup") &&
!wooShippingMethod.toLowerCase().includes("local");
}
// Método de pago
// 1. Primero intentar leer de metadata (para pedidos de WhatsApp)
const metaPaymentMethod = order.meta_data?.find(m => m.key === "payment_method_wa")?.value || null;
// 2. Luego de los campos estándar de WooCommerce
const paymentMethod = order.payment_method || null;
const paymentMethodTitle = order.payment_method_title || null;
const isCash = paymentMethod === "cod" ||
// 3. Determinar si es cash
const isCash = metaPaymentMethod === "cash" ||
paymentMethod === "cod" ||
paymentMethodTitle?.toLowerCase().includes("efectivo") ||
paymentMethodTitle?.toLowerCase().includes("cash");