在订单确认页面获取订单ID

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

在脚本中,我想读取当前订单详细信息,所以我试试这个

<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>

回答

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


以上是在订单确认页面获取订单ID的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>