简体   繁体   English

将不同尺寸的相同产品添加到购物车

[英]Add same product with different sizes to the cart

I can add different products as individual arrays and also can increment the quantities if same product is added.我可以添加不同的产品作为单独的数组,如果添加相同的产品,也可以增加数量。 But now I need to add same product but with Different size and it should create another new array.但是现在我需要添加相同的产品但大小不同,它应该创建另一个新数组。 Below is the code I am using.. Please help out..!!下面是我使用的代码..请帮忙..!!

switch (action.type) {
case ADD_TO_CART:
    const productId = action.product.id;

    if (findIndex(state.cart, product => product.id === productId) !== -1) {
        const cart = state.cart.reduce((cartAcc, product) => {
            if (product.id === productId) {
                // if (product.size === action.size) {
                    cartAcc.push({ ...product, size: parseInt(product.size), qty: parseInt(product.qty) + parseInt(action.qty), sum: (product.discount ? product.salePrice : product.price) * (parseInt(product.qty) + parseInt(action.qty)) }) // Increment qty
                // } else {
                //     cartAcc.push(product)
                // }

            } else {
                cartAcc.push(product)
            }
            return cartAcc
        }, [])

        return { ...state, cart }
    }
    return {
        ...state,
        cart: [
            ...state.cart,
            {
                ...action.product,
                qty: action.qty,
                size: action.size,
                sum: (action.product.discount ? action.product.salePrice : action.product.price) * action.qty
            }
        ]
    }

You are almost done, just check for size equality (in findIndex and in the subsequent if):你快完成了,只需检查大小相等(在 findIndex 和随后的 if 中):

const productId = action.product.id;
const size = action.size;

if (findIndex(state.cart, product => (product.id === productId && product.size === size)) !== -1) {
        const cart = state.cart.reduce((cartAcc, product) => {
            if (product.id === productId  && product.size === size) {
                    cartAcc.push({ ...product, size: size, qty: parseInt(product.qty) + parseInt(action.qty), sum: (product.discount ? product.salePrice : product.price) * (parseInt(product.qty) + parseInt(action.qty)) }) // Increment qty
            } else {
                cartAcc.push(product)
            }
            return cartAcc
        }, [])

        return { ...state, cart }
    }

Your code is correct however you need to give a unique product ID for each product with specific attributes.您的代码是正确的,但是您需要为每个具有特定属性的产品提供唯一的产品 ID。 for Example例如

Product: { id: 'Nike' size: 's' color: 'blue'}

your Cart is having multi items where this product example having ID: 'Nike,s,blue'.您的购物车有多个商品,其中此产品示例的 ID 为:“Nike,s,blue”。 each time you try to find this product with that specific ID you will be able to add it as new or Increase it if it already exist.每次您尝试使用该特定 ID 查找此产品时,您都可以将其添加为新产品或增加它(如果它已经存在)。

You may take the same logic with T-shirt 'L' where its ID: 'Nike,L,blue'.您可以对 T 恤“L”采用相同的逻辑,其 ID 为:“Nike,L,blue”。 it make it a new product and separated.它使它成为一个新产品并分离。

Briefly: You need to make a unique ID for each product along with its Attributes (you may combine the attributes with the ID based on the Object.values or array where you have the Data).简而言之:您需要为每个产品及其属性创建一个唯一的 ID(您可以根据您拥有数据的 Object.values 或数组将属性与 ID 结合起来)。

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

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