Git Product home page Git Product logo

Comments (8)

pranjal-barnwal avatar pranjal-barnwal commented on May 17, 2024 1

For those who wanted the cart order to remain the same after increasing or decreasing the quantity,

setCartItems([...newCartItems.slice(0, index), {...foundProduct, quantity: foundProduct.quantity + 1}, ...newCartItems.slice(index)])

setCartItems([...newCartItems.slice(0, index), {...foundProduct, quantity: foundProduct.quantity - 1}, ...newCartItems.slice(index)])

https://stackoverflow.com/questions/586182/how-to-insert-an-item-into-an-array-at-a-specific-index-javascript --refer to the link for more info

Great answer 🤗🤗

I tried in a bit crude way:
`
const toggleCartItemQuantity = (id, value) => {
foundProduct = cartItems.find((item) => item._id === id);
index = cartItems.findIndex((product) => product._id === id);

const newCartItems = cartItems.filter((item) => item._id !== id);

if (value === 'inc') {
    let finalCartItems = [...newCartItems, { ...foundProduct, quantity: foundProduct.quantity + 1 }];
    let length = finalCartItems.length;
    [finalCartItems[index], finalCartItems[length - 1]] = [finalCartItems[length - 1], finalCartItems[index]];
    setCartItems(finalCartItems);
    setTotalPrice((prevTotalPrice) => prevTotalPrice + foundProduct.price);
    setTotalQuantities((prevTotalQuantities) => prevTotalQuantities + 1);
}
else if (value === 'dec') {
    if (foundProduct.quantity > 1) {
        let finalCartItems = [...newCartItems, { ...foundProduct, quantity: foundProduct.quantity - 1 }];
        let length = finalCartItems.length;
        [finalCartItems[index], finalCartItems[length - 1]] = [finalCartItems[length - 1], finalCartItems[index]];
        setCartItems(finalCartItems);
        setTotalPrice((prevTotalPrice) => prevTotalPrice - foundProduct.price);
        setTotalQuantities((prevTotalQuantities) => prevTotalQuantities - 1);
    }
}

}
`

from ecommerce_sanity_stripe.

Az1m04 avatar Az1m04 commented on May 17, 2024 1

image
this works for me

from ecommerce_sanity_stripe.

saleh1992 avatar saleh1992 commented on May 17, 2024

Thanks worked for me

from ecommerce_sanity_stripe.

Brenda-FED avatar Brenda-FED commented on May 17, 2024

Thank you! This also worked well for me!

from ecommerce_sanity_stripe.

yoho4613 avatar yoho4613 commented on May 17, 2024

I have figured out with this way using splice
use Splice with index to update newCartItems then simply setCartitems as newCartItems

const toggleCartItemQuantity = (id, value) => {

   foundProduct = cartItems.find((item) => item._id === id);
   index = cartItems.findIndex((product) => product._id === id);
   const newCartItems = cartItems.filter((item) => item._id !== id);

   if (value === "inc") {
     newCartItems.splice(index, 0, {...foundProduct, quantity: foundProduct.quantity + 1})

     setCartItems([ ...newCartItems ]);
     setTotalPrice((prevTotalPrice) => prevTotalPrice + foundProduct.price);
     setTotalQuantities((prevTotalQuantities) => prevTotalQuantities + 1);
   } else if (value === "dec") {
     if (foundProduct.quantity > 1) {
       newCartItems.splice(index, 0, {...foundProduct, quantity: foundProduct.quantity - 1})

       setCartItems([ ...newCartItems ]);
       setTotalPrice((prevTotalPrice) => prevTotalPrice - foundProduct.price);
       setTotalQuantities((prevTotalQuantities) => prevTotalQuantities - 1);
     }
   }
 };

from ecommerce_sanity_stripe.

cCoKeXx avatar cCoKeXx commented on May 17, 2024

For those who wanted the cart order to remain the same after increasing or decreasing the quantity,

setCartItems([...newCartItems.slice(0, index), {...foundProduct, quantity: foundProduct.quantity + 1}, ...newCartItems.slice(index)])

setCartItems([...newCartItems.slice(0, index), {...foundProduct, quantity: foundProduct.quantity - 1}, ...newCartItems.slice(index)])

https://stackoverflow.com/questions/586182/how-to-insert-an-item-into-an-array-at-a-specific-index-javascript --refer to the link for more info

Thanks for the fix, worked for me ! 😊

from ecommerce_sanity_stripe.

NabintouSFofana avatar NabintouSFofana commented on May 17, 2024

For those who wanted the cart order to remain the same after increasing or decreasing the quantity,

setCartItems([...newCartItems.slice(0, index), {...foundProduct, quantity: foundProduct.quantity + 1}, ...newCartItems.slice(index)])

setCartItems([...newCartItems.slice(0, index), {...foundProduct, quantity: foundProduct.quantity - 1}, ...newCartItems.slice(index)])

https://stackoverflow.com/questions/586182/how-to-insert-an-item-into-an-array-at-a-specific-index-javascript --refer to the link for more info

thanks !!!

from ecommerce_sanity_stripe.

NabintouSFofana avatar NabintouSFofana commented on May 17, 2024

For those who wanted the cart order to remain the same after increasing or decreasing the quantity,
setCartItems([...newCartItems.slice(0, index), {...foundProduct, quantity: foundProduct.quantity + 1}, ...newCartItems.slice(index)])
setCartItems([...newCartItems.slice(0, index), {...foundProduct, quantity: foundProduct.quantity - 1}, ...newCartItems.slice(index)])
https://stackoverflow.com/questions/586182/how-to-insert-an-item-into-an-array-at-a-specific-index-javascript --refer to the link for more info

Great answer 🤗🤗

I tried in a bit crude way: ` const toggleCartItemQuantity = (id, value) => { foundProduct = cartItems.find((item) => item._id === id); index = cartItems.findIndex((product) => product._id === id);

const newCartItems = cartItems.filter((item) => item._id !== id);

if (value === 'inc') {
    let finalCartItems = [...newCartItems, { ...foundProduct, quantity: foundProduct.quantity + 1 }];
    let length = finalCartItems.length;
    [finalCartItems[index], finalCartItems[length - 1]] = [finalCartItems[length - 1], finalCartItems[index]];
    setCartItems(finalCartItems);
    setTotalPrice((prevTotalPrice) => prevTotalPrice + foundProduct.price);
    setTotalQuantities((prevTotalQuantities) => prevTotalQuantities + 1);
}
else if (value === 'dec') {
    if (foundProduct.quantity > 1) {
        let finalCartItems = [...newCartItems, { ...foundProduct, quantity: foundProduct.quantity - 1 }];
        let length = finalCartItems.length;
        [finalCartItems[index], finalCartItems[length - 1]] = [finalCartItems[length - 1], finalCartItems[index]];
        setCartItems(finalCartItems);
        setTotalPrice((prevTotalPrice) => prevTotalPrice - foundProduct.price);
        setTotalQuantities((prevTotalQuantities) => prevTotalQuantities - 1);
    }
}

} `

THANKS

from ecommerce_sanity_stripe.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.