top of page

Wix 網頁設計 - storeUtils

  • 作家相片: 作 振
    作 振
  • 2025年6月13日
  • 讀畢需時 1 分鐘
// backend/storeUtils.jsw
import wixData from 'wix-data';
import { currentCart } from 'wix-ecom-backend';
import { authentication } from 'wix-members-backend';

export async function updateCartDeliveryTime(deliveryDate) {
    console.log("後端 storeUtils: updateCartDeliveryTime 被調用!接收日期:", deliveryDate);

    try {
        if (!deliveryDate) {
            console.warn("後端 storeUtils: 未提供有效的預計出貨日期。");
            throw new Error("預計出貨日期不能為空。");
        }

        const cart = await currentCart.getCurrentCart();
        const cartId = cart._id; // 獲取購物車的 ID
        const checkoutId = cart.checkoutId; // 獲取結帳 ID

        // !!!關鍵日誌:觀察這些 ID!!!
        console.log("後端 storeUtils: 當前購物車 ID (cart._id):", cartId);
        console.log("後端 storeUtils: 當前結帳 ID (cart.checkoutId):", checkoutId);

        if (!cartId) {
            console.error("後端 storeUtils: 無法獲取購物車 ID,無法保存日期。");
            throw new Error("無法獲取購物車識別符。");
        }

        const currentMemberId =  null; 
        const dateToStore = new Date(deliveryDate); 

        // !!! 核心修改:移除 upsert 邏輯,每次都直接插入一條新記錄 !!!
        // 這些記錄在訂單生成前都是「草稿」狀態
        const dataToSave = {
            cartId: cartId,           // 儲存購物車 ID
            checkoutId: checkoutId,   // 儲存結帳 ID
            deliveryDate: dateToStore,
            memberId: currentMemberId,
            orderId: null,            // 初始時 orderId 為空,等待 events.jsw 填充
            status: "pending"         // 標記為待處理(草稿)狀態
        };
        const insertedItem = await wixData.insert("OrderDeliveryDates", dataToSave);
        console.log("後端 storeUtils: 預計出貨日期草稿已成功保存到資料庫:", insertedItem);
        return { success: true, message: "出貨日期草稿已保存。" };

    } catch (error) {
        console.error("後端 storeUtils: 操作出貨日期到資料庫失敗:", error);
        throw new Error(`操作出貨日期失敗。錯誤詳情: ${error.message || error}`);
    }
}

最新文章

查看全部
Wix 網頁設計 - mailgunService.jsw

// backend/mailgunService.jsw // 導入 Wix Velo 內建的 fetch API,用於發送 HTTP 請求 import { fetch } from 'wix-fetch'; // 導入 Wix Secrets...

 
 
 
Wix 網頁設計 - event.js

// backend/events.jsw // 導入 Wix 資料庫模組,用於查詢和更新 CMS 集合 import wixData from 'wix-data'; // 從後端服務檔案導入您自定義的 Mailgun 郵件發送函數 import {...

 
 
 

留言


創客爸爸.png

TRUNK DADDY

​歡迎訂閱

感謝你的訂閱,你將不會錯過任何一個最新的訊息

創客爸爸

© 2024 by TRUNKDADDY

bottom of page