Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

203
Views
¿Cómo puedo guardar la variación de múltiples productos?

Tengo estos productos con variaciones de producto de colores. El problema es que no graba el color del producto que he añadido. Por ejemplo, la camiseta tiene 2 colores, rojo y verde, y elegiré el rojo, solo se guardará el rojo y se actualizará la cantidad para ese producto en su conjunto, pero no en el color.

Esto es lo que pasa:

ingrese la descripción de la imagen aquí

Códigos y caja: https://codesandbox.io/s/add-to-cart-jsd9fd?file=/src/App.js:0-2599

Códigos:

 export default function App() { const [cartItems, setCartItems] = useState([]); const handleAdd = (id, name, size, cat, color) => { console.log("add", id, name, size, cat, color); const productExist = cartItems.find((item) => item.id === id); if (productExist) { setCartItems( cartItems.map((item) => item.id === id ? { ...productExist, quantity: productExist.quantity + 1 } : item ) ); } else { setCartItems([...cartItems, { id, name, size, cat, color, quantity: 1 }]); } }; // const handleAdd = (item) => { // console.log("add", item); // }; console.log(cartItems); return ( <div className="App"> {products.map((index) => ( <ul key={index.id}> <li> <b>{index.prodName}</b> </li> <li>Category: {index.cat}</li> <li>Size: {index.size}</li> {Object.entries(index.colorMap).map((color) => ( <> {color[1] !== 0 ? ( <> {color[0]} <button onClick={(e) => handleAdd( index.id, index.prodName, index.size, index.cat, color[0] ) } > Add to Cart </button> {/* <button onClick={(e) => handleAdd(index)}>Add to Cart</button> */} </> ) : ( <>2</> )} <br /> </> ))} Php {index.price}.00 </ul> ))} <Cart cartItems={cartItems} /> </div> ); }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Cuando verifica si el producto ya está en el carrito, solo verifica por su id :

 const productExist = cartItems.find((item) => item.id === id);

Pero si desea separar los productos por id y color , también debe verificarlo:

 const productExist = cartItems.find((item) => item.id === id && item.color === color);

Y, por supuesto, actualice la misma lógica de comparación al actualizar el carrito:

 setCartItems( cartItems.map((item) => (item.id === id && item.color === color) // <--- here ? { ...productExist, quantity: productExist.quantity + 1 } : item ) );
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!