简体   繁体   English

前端和后端的购物车项目列表

[英]Shopping Cart item list across frontend and backend

I'm working on an E-Commerce store using Node.JS, Stripe and HTML.我正在使用 Node.JS、Stripe 和 HTML 开发电子商务商店。 I have the frontend JS file in which users add/remove/change the quantity of cart items which also affects a list with all the cart items to access later (Price SKUs and Quantity).我有前端 JS 文件,用户在其中添加/删除/更改购物车项目的数量,这也会影响包含稍后访问的所有购物车项目的列表(价格 SKU 和数量)。 A typical cart list would look like this: ["price_1LWgytARYjIsWfAV0lZI6mgW", "1", "price_1LWgytARYjIsWfAV0lZI6PXv", "3"]典型的购物车列表如下所示: ["price_1LWgytARYjIsWfAV0lZI6mgW", "1", "price_1LWgytARYjIsWfAV0lZI6PXv", "3"]

To get the checkout in stripe, you need to provide Node.JS with the line items of the Price SKUs and Quantities.要获得条带结帐,您需要向 Node.JS 提供价格 SKU 和数量的行项目。 I am trying to access this cart items list in the backend.我正在尝试在后端访问此购物车项目列表。 I have tried things such as local storage but it is undefined and null because it isn't running on the webpage.我尝试过诸如本地存储之类的东西,但它是未定义的,null 因为它没有在网页上运行。

Can anyone help me to access this list in the backend server?谁能帮我在后端服务器中访问此列表? Thank you谢谢

Local storage is a feature that lets you store information client-side in the browser.本地存储是一项功能,可让您将信息客户端存储在浏览器中。 An example is storing someone's preferred locale or timezone if you don't want a cookie/session server-side.如果您不想要 cookie/会话服务器端,例如存储某人的首选语言环境或时区。 So it's expected that your server-side code in Node.js will not have access to those values.因此,预计您在 Node.js 中的服务器端代码将无法访问这些值。

What you need to do instead is make a request to your server that will contain all the line items they have in their cart.相反,您需要做的是向您的服务器发出请求,该请求将包含他们在购物车中的所有订单项。 In Javascript, this is usually done in two ways在 Javascript 中,这通常通过两种方式完成

  1. You have your own form in HTML and you pass all the information in the form and submit it which redirects the whole page.您在 HTML 中有自己的表单,您将表单中的所有信息传递并提交,从而重定向整个页面。
  2. You write code using the Fetch API so that you can send your line items (usually as JSON) to your server and have it reply asynchronously.您使用Fetch API编写代码,以便您可以将您的订单项(通常以 JSON 格式)发送到您的服务器并让它异步回复。

In your case, the second option is usually best.在您的情况下,第二种选择通常是最好的。 You'll have a button that someone can click on when they are ready to pay.您将有一个按钮,当他们准备好付款时可以点击该按钮。 At that point, the code will take all your line_items and send them as a JSON array/hash to your server-side code.此时,代码将获取您的所有 line_items 并将它们作为 JSON 数组/哈希发送到您的服务器端代码。

It's important to note that the format of your line items right now doesn't really work well because you have an array alternating a Price id price_123 and then the quantity, and then another Price id, etc. making editing quite hard.重要的是要注意,您的订单项的格式现在并不能很好地工作,因为您有一个数组交替使用价格 ID price_123 ,然后是数量,然后是另一个价格 ID,等等。这使得编辑非常困难。 Usually, you'd want to use an array of objects instead, which would be quite similar to what you would send to the Create Checkout Session API 's line_items parameter .通常,您希望使用对象数组,这与您发送到 Create Checkout Session APIline_items 参数的内容非常相似。 What you want to send should look more like this:您要发送的内容应如下所示:

var cart = {
  line_items: [
    {
      price: "price_ABC",
      quantity: 1
    },
    {
      price: "price_123",
      quantity: 7
    },
    {
      price: "price_XYZ",
      quantity: 2
    }
  ]
};

This format makes it a lot easier because you have a hash/object that is your cart.这种格式使它更容易,因为您有一个哈希/对象是您的购物车。 It has a line_items property that is an array array of line items.它有一个line_items属性,它是一个行项目数组。 It contains 3 elements, and the first element's price is price_ABC and its quantity is 1 which will be a lot easier to parse and modify in Javascript both client-side and server-side.它包含3个元素,第一个元素的价格是price_ABC ,数量是1 ,在Javascript客户端和服务器端都更容易解析和修改。

Here's a basic example of how to send your cart variable/content to your server with fetch() :这是如何使用fetch()将购物车变量/内容发送到服务器的基本示例:

fetch(
  "/checkout_session_creation",
  {
    method: "POST",
    body: JSON.stringify(cart)
  })
.then(function(response) {
  return response.json();
})
.then(function(data) {
  console.log("Got the response: ", data);
});

This will post to /checkout_session_creation though you have to put the route you are expecting there.这将发布到/checkout_session_creation虽然您必须将您期望的路线放在那里。 It will sent the cart variable/content in the POST body as JSON.它会将 POST 正文中的cart变量/内容作为 JSON 发送。 Then it expects a response back in JSON that you can parse.然后它期望在 JSON 中返回您可以解析的响应。

The idea here is that after creating the Checkout Session in Node server-side, you will get the url property that you then need to return to your client.这里的想法是,在节点服务器端创建 Checkout Session 后,您将获得url 属性,然后您需要将其返回给客户端。 You then need to redirect client-side to that URL.然后,您需要将客户端重定向到该 URL。

With all of that said, I'd highly recommend going through Stripe's official Checkout Quickstart end to end as they walk you through most of this.综上所述,我强烈建议您端到端地阅读 Stripe 的官方Checkout Quickstart ,因为他们会引导您完成大部分内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM