iT邦幫忙

2025 iThome 鐵人賽

DAY 14
0
Modern Web

用 LINE OA 打造中小企業訂單系統:從零開始的 30 天實作紀錄系列 第 14

顧客也要安心:訂單狀態查詢與回覆訊息

  • 分享至 

  • xImage
  •  

昨天我們為店家實作了訂單狀態管理(Pending → In Progress → Completed)。

但顧客也需要隨時知道自己的訂單進度。

今天要實作「查詢訂單狀態」功能,讓顧客輸入指令或點選選單後,系統能即時回覆目前的訂單狀態。

這樣能提升顧客的信任感與使用體驗,也讓訂單流程更透明。


為什麼要開放顧客查詢?

  • 減少顧客重複詢問「我的訂單好了嗎?」。

  • 提升顧客對系統的信任感。

  • 減少店家溝通成本。


查詢設計思路

  1. 使用者輸入「查詢訂單」。

  2. 系統辨識使用者的 userId

  3. 到 MongoDB 撈出「該使用者最新的訂單」。

  4. 回覆文字告知狀態。


API 與邏輯

  • EndpointGET /orders/latest?userId=xxx

  • 功能:回傳使用者最近一筆訂單。

  • Webhook 行為:當使用者輸入「查詢訂單」時,呼叫此 API。

  • 若無訂單:回覆「您目前沒有訂單」。


資料流示意

https://ithelp.ithome.com.tw/upload/images/20250928/20178868YmhWvVgkCa.png


程式碼範例

API:查詢使用者最新訂單

// routes/orders.js
router.get("/latest", async (req, res) => {
  try {
    const { lineUserId } = req.query;  // ← 改成 lineUserId
    if (!lineUserId) {
      return res.status(400).json({ success: false, message: "缺少 lineUserId" });
    }

    // 先找/建 User,拿到 ObjectId
    const user = await User.findOne({ lineUserId }).exec();
    if (!user) {
      return res.json({ success: true, order: null });
    }

    const order = await Order.findOne({ userId: user._id })
      .sort({ createdAt: -1 })
      .exec();

    return res.json({ success: true, order });

  } catch (err) {
    console.error(err);
    res.status(500).json({ success: false, message: "查詢訂單失敗" });
  }
});

Webhook:回覆查詢結果

if (event.type === "message" && event.message.type === "text") {
  if (event.message.text === "查詢訂單") {
    const { data } = await axios.get(
        `${process.env.API_BASE_URL || "http://localhost:3000"}/orders/latest`,
        { params: { lineUserId: event.source.userId } } // ← 傳 lineUserId
      );

    if (!data.order) {
      return client.replyMessage(event.replyToken, {
        type: "text",
        text: "您目前沒有訂單紀錄。",
      });
    }

    const order = data.order;
    const itemsText = order.items.map(it => `${it.productName} x ${it.quantity}`).join("\n");
    return client.replyMessage(event.replyToken, {
      type: "text",
      text: `📦 您最近的一筆訂單:\n${itemsText}\n狀態:${order.status}\n建立時間:${new Date(order.createdAt).toLocaleString()}`,
    });
  }
}

最終成果

https://ithelp.ithome.com.tw/upload/images/20250928/20178868vlDijFiN7j.png


未來擴充方向

  • 查詢「所有歷史訂單」。

  • 查詢「特定訂單編號」。

  • 使用 Flex Message 加入進度條視覺化。

  • 使用 Rich Menu 讓使用者直接點擊。


重點回顧

  • 顧客可以透過 LINE Bot 即時查詢訂單狀態。

  • 這功能降低顧客焦慮與店家溝通成本。

  • 我們完成了「從下單 → 狀態管理 → 顧客查詢」的閉環流程。


上一篇
讓訂單會呼吸:訂單狀態管理設計與實作
下一篇
從聊天到表單:設計 LIFF 訂單頁面(UX + 驗證)
系列文
用 LINE OA 打造中小企業訂單系統:從零開始的 30 天實作紀錄15
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言