【问题标题】:Get OrderId in Order Confirmation page在订单确认页面获取 OrderId
【发布时间】:2021-06-22 17:02:47
【问题描述】:

我正在为 BigCommerce 开发一个应用程序,我必须将 Script 注入订单确认页面。

在脚本中,我想阅读current order detail,所以我试试这个

<script>
 function getOrderDetail(orderId) {
    return fetch("/api/storefront/order/" + orderId, {
      credentials: "include",
    }).then(function (response) {
      return response.json();
    });
  }
let ORDER_ID=123;
getOrderDetail(ORDER_ID).then(data=>{
// do this ,do that,
})

</script>

我没有找到任何与获取当前 Order_ID 相关的文档,我检查了 HTML 代码并尝试了

function getOrderId() {
    const orderIdRegex = /\s+orderId:\s'(\d+)',/;
    return document.body.innerHTML.match(orderIdRegex)[1];
  }

我知道如果 UI 发生变化,代码可能会中断。

Shopify中,有一个全局变量window.Shopify

我想知道是否有更好的解决方案,或者类似于 Shopify。

更新为正确答案

感谢@MattCoy

<script>
 function getOrderDetail(){
    return fetch("/api/storefront/order/{{checkout.order.id}}", {
      credentials: "include",
    }).then(function (response) {
      return response.json();
    });
  }

 getOrderDetail().then(data=>{
   // do this ,do that,
 })

</script>

【问题讨论】:

    标签: bigcommerce bigcommerce-checkout-sdk


    【解决方案1】:

    如果这是脚本管理器中的脚本,您应该可以通过 Handlebars 访问所有 Stencil 主题对象。在这种情况下,请尝试{{checkout.order.id}}

    【讨论】:

    • 嗨@Matt,有没有办法在order_confirmation_page 的ScriptManager 中获得完整的OrderDetail 而不是调用fetch("/api/storefront/order/" + orderId)?因为调用 API 会稍微延迟我的逻辑。
    • @vanduc1102,不幸的是,没有。我能想到的唯一其他方法是在结帐页面上使用 Stencil 购物车对象设置 cookie/localStorage 变量。虽然这有点 hacky。
    猜你喜欢
    • 2010-12-26
    • 1970-01-01
    • 2016-01-04
    • 2022-12-29
    • 1970-01-01
    • 1970-01-01
    • 2019-02-14
    • 2021-06-12
    • 1970-01-01
    相关资源
    最近更新 更多