简体   繁体   English

我应该如何为电子商务网站的结帐页面创建逻辑?

[英]How should i create logic for checkout page for an e-commerce website?

I am using React and building a simple e-commerce project.我正在使用 React 并构建一个简单的电子商务项目。 On the shop page, when I click a product's "+" button, that product will be added to the checkout items.在商店页面上,当我单击产品的“+”按钮时,该产品将添加到结帐项目中。 The issue is that if you add more than one of the same product, it will add it as another item.问题是,如果您添加多个相同的产品,它会将其添加为另一项。 What I want to do is if a product's quantity is greater than 1, then just show 1 item and show the quantity.我想做的是,如果产品的数量大于 1,则只显示 1 件商品并显示数量。

I use useContext for share all data to shop and checkout pages.我使用 useContext 将所有数据共享到购物和结帐页面。 I created the addItem function and all states in that context.(ShopContext)我在该上下文中创建了 addItem function 和所有状态。(ShopContext)

ShopContext商店环境

 const [products, setProducts] = useState([]);
 const [checkoutItems, setCheckoutItems] = useState([]);

const addItem = (id) => {
    const newProduct = id;
    setCheckoutItems([...checkoutItems, products[newProduct]]);
  };

 const values = {
    products,
    addItem,
    checkoutItems,
  };
  return (
    <ShopContext.Provider value={values}>{props.children}</ShopContext.Provider>
  );

The button:按钮:

<button
              id={idx}
              className="bg-purple-400 px-4"
              onClick={(e) => addItem(e.target.id)}>+</button>

Checkout Page结帐页面

const Checkout = () => {
  const { checkoutItems } = useContext(ShopContext);
  return (
    <div className="my-7 lg:max-w-7xl mx-auto font-Rubik items-center">
      {JSON.stringify(checkoutItems)}
    </div>
  );
};

Your checkoutItems should be an array of objects with properties like您的 checkoutItems 应该是一个对象数组,其属性如下

[ { id: 'abc', quantity: 2 }, { id: 'cde', quantity: 1 ] 

On backend you should retrieve the price of the product base on ID and display it to the user.在后端,您应该根据 ID 检索产品的价格并将其显示给用户。 You don't need more information (such as price etc.) in your frontend because all of this should be handled by backend.您的前端不需要更多信息(例如价格等),因为所有这些都应该由后端处理。 You also don't want to have any endpoint on your server which will allow information such as price to be sent by frontend.您也不希望在您的服务器上有任何端点,这将允许前端发送价格等信息。

One way to solve this issue is to keep track of the quantity of each item in the checkoutItems state. You can do this by creating an object for each item that includes the product information as well as a quantity property.解决此问题的一种方法是在 checkoutItems state 中跟踪每个项目的数量。您可以通过为每个项目创建一个 object 来实现这一点,其中包括产品信息和数量属性。

In the addItem function, you can check if the item already exists in the checkoutItems state. If it does, you can increment the quantity property.在 addItem function 中,您可以检查该项目是否已存在于 checkoutItems state 中。如果存在,您可以增加 quantity 属性。 If it doesn't, you can add a new object to the state with the product information and a quantity of 1.如果没有,您可以在 state 中添加一个新的 object,其中包含产品信息和数量 1。

Here's an example of how you can update your code:以下是如何更新代码的示例:

ShopContext商店环境

const addItem = (id) => {
const newProduct = products[id];
const existingItem = 
checkoutItems.find(item => item.id === id);
if(existingItem){
setCheckoutItems(checkoutItems.map(item => item.id === id ? {...item, quantity: item.quantity+1} : item));
} else {
setCheckoutItems([...checkoutItems, {...newProduct, quantity: 1}]);
}
};

CheckoutPage结账页面

const Checkout = () => {
const { checkoutItems } = 
useContext(ShopContext);
return (
<div className="my-7 lg:max-w-7xl mx-auto font-Rubik items-center">
{checkoutItems.map(item => <div> 
{item.name} - Quantity: {item.quantity} 
</div>)}
</div>
);
};

You can then use the quantity property to display the correct quantity on the checkout page.然后,您可以使用数量属性在结帐页面上显示正确的数量。

Apologies for the code, I'm on my mobile为代码道歉,我在我的手机上

The issue with your current code is that you are adding a new object to the checkoutItems state each time the "+" button is clicked, instead of updating the quantity of an existing item.您当前代码的问题是,每次单击“+”按钮时,您都会向 checkoutItems state 添加一个新的 object,而不是更新现有项目的数量。 To fix this, you need to check if the item already exists in the checkoutItems state, and if it does, you should update the quantity property of that item.要解决此问题,您需要检查该项目是否已存在于 checkoutItems state 中,如果存在,则应更新该项目的数量属性。

To accomplish this, you can use the Array.prototype.find() method to find the existing item in the checkoutItems state, and then use the Array.prototype.map() method to update the quantity property of that item.为此,您可以使用 Array.prototype.find() 方法在 checkoutItems state 中查找现有项目,然后使用 Array.prototype.map() 方法更新该项目的数量属性。

Regarding the newProduct = products[id], it is used to get the product object from the products state by its id, so that you can add it to the checkoutItems state.关于newProduct = products[id],它是用来通过id从products state中获取product object,这样就可以把它添加到checkoutItems state中了。

Also, you are using JSON.stringify on checkoutItems in Checkout component, and it will not display the item's name and quantity.此外,您在 Checkout 组件的 checkoutItems 上使用 JSON.stringify,它不会显示项目的名称和数量。 You should use map to iterate and display the item's name and quantity.您应该使用 map 来迭代并显示项目的名称和数量。

The reason find() method is returning undefined is because it's not able to find the matching item in the checkoutItems state. This could be due to the fact that the id's of the products in the products state and the checkoutItems state are not the same. find() 方法返回未定义的原因是因为它无法在 checkoutItems state 中找到匹配项。这可能是因为产品 state 和 checkoutItems state 中的产品 ID 不相同。

Using the array indexer like checkoutItems[0].id === id would only check the first item in the checkoutItems state and will not check other items.使用像 checkoutItems[0].id === id 这样的数组索引器只会检查 checkoutItems state 中的第一项,而不会检查其他项目。

You can use the Array.prototype.filter() method to iterate through the checkoutItems state, and check for the existence of the item in the state by its id.您可以使用 Array.prototype.filter() 方法遍历 checkoutItems state,并通过其 id 检查 state 中的项目是否存在。

Here's an example of how you can update your code:以下是如何更新代码的示例:

const addItem = (id) => {
const newProduct = products.find(product => product.id === id);
const existingItem = checkoutItems.filter(item => item.id === id);
if (existingItem.length > 0) {
setCheckoutItems(
  checkoutItems.map(item =>
    item.id === id ? { ...item, quantity: item.quantity + 1 } : item
  )
);
 } else {
setCheckoutItems([...checkoutItems, { ...newProduct, quantity: 1 }]);
}
};

Here I am using the Array.prototype.filter() method to filter the checkoutItems state and check if the item with the given id already exists in the state. If it does, then I am updating the quantity property using the Array.prototype.map() method.在这里,我使用 Array.prototype.filter() 方法来过滤 checkoutItems state 并检查具有给定 id 的项目是否已经存在于 state 中。如果存在,那么我将使用 Array.prototype 更新数量属性。地图()方法。 If it doesn't, then I am adding the new product to the state with a quantity of 1.如果没有,那么我将新产品添加到 state,数量为 1。

Also, make sure that the id's of the products in the products state and the checkoutItems state are the same, so that you can find and match the items correctly.另外,请确保产品 state 和 checkoutItems state 中的产品 id 相同,以便您可以正确查找和匹配项目。

暂无
暂无

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

相关问题 电商网站购物车页面计算及HTMl更新 - E-commerce website cart page calculation and HTMl updation 如何计算电子商务网站上产品的排名/位置? - How to count rank/position of products on e-commerce website? 如何在php中创建电子商务网站相关产品滑块 - How to create E-Commerce websites related products slider in php 储存Cookie电子商务网站 - store a cookies e-commerce website React MongoDB 电子商务网站。 如果用户已经登录,如何从“帐户”按钮重定向到用户信息而不是登录页面? - React MongoDB E-Commerce website. If user is already logged in, how to redirect from "Account" button to User info instead of Login page? 如何在电子商务表格上要求最低数量? - How Can I require a Minimum Quantity on an e-commerce Form? 如何为我的电子商务项目乘以 reactjs? - How do I multiply for my e-commerce project with reactjs? 我正在建立一个电子商务网站,在这个过程中,我面临一个问题,如何将用户输入数据从 javascript 发送到后端 - I'm building an E-commerce website, in this process, I'm facing a problem that how to send users input data from javascript to the backend 单击电子商务网站中的搜索按钮后,如何阻止页面重新加载? - How can I stop the page from reloading after I click on search button in my E-commerce site? 我需要向下滚动,直到使用硒Webdriver在电子商务网站中完成搜索结果为止 - I need to scroll down until the search result complete in e-commerce website using selenium webdriver
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM