Comments (8)
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.
from ecommerce_sanity_stripe.
Thanks worked for me
from ecommerce_sanity_stripe.
Thank you! This also worked well for me!
from ecommerce_sanity_stripe.
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.
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.
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.
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 infoGreat 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)
- Unable to resolve image URL from source (undefined) at 1:28 min HOT 4
- Schema error
- Sup guys if you have any issues with adding products to local storage here is the code or you wanna Margaret to next.js 13 here is the repo
- TypeError: Cannot read properties of undefined (reading 'length') bannerData.length is not fetching from sanity (sanity has 2 products in it's db) HOT 6
- components and indexjs erros HOT 14
- Ordering products in cart based on add order HOT 2
- TypeError: Cannot destructure property 'image' of 'product' as it is null. This error happened while generating the page. Any console logs will be displayed in the terminal window. Source pages\product\[slug].js (9:10) @ ProductDetails 7 | 8 | const ProductDetails = ({ product, products }) => { > 9 | const { image, name, details, price } = product ; | ^ 10 | const [index, setIndex] = useState(0); 11 | const { decQty, incQty, qty, onAdd, setShowCart } = useStateContext(); 12 |
- How to link search bar I added the code but when I click on search it is redirecting me and showing 404 Not Found. can you help me plzzz..!
- Help me create menu i'm newbie PLSSSSSS HOT 1
- if i try to add same item after adding another item it says undefiend to all the other items beside newly added one HOT 1
- ClientError: Unauthorized - Session does not match project host HOT 1
- Unhandled Runtime Error TypeError: Cannot read properties of undefined (reading '0')
- Module parse failed: Unexpected token (1:7) HOT 2
- Server Error Error: Unable to resolve image URL from source (undefined) This error happened while generating the page. Any console logs will be displayed in the terminal window.
- SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data HOT 2
- sanity schema problem HOT 3
- Stripe redirect to Checkout failed HOT 3
- const { decQty, incQty, qty, onAdd, setShowCart } = useStateContext(); these variables are undefined in the slug.js file. On printing useStateContext() on console it shows the default value() HOT 3
- > _No description provided._ HOT 1
- "Error: Unable to resolve image URL from source (undefined)" -> [slug].js: image[index] == undefined HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ecommerce_sanity_stripe.