I really need help, I’m stuck on this matter for over 2 days and so far nobody was able to help me.
I’m trying to do one thing only: I have an e-commerce shop where there’s multiple items. Each item can have multiple options like quantity: 300g, 500g and 1000g. I want to display the price of that item based on the quantity that’s selected. I have all the data in my own API. Here’s a photo so you can clearly see what I’m trying to do:
and here’s some relevant code.
1) How each item looks like in my json file:
{
id: '233fdcc3',
nume: 'Adimel',
slug: 'adimel',
categorie: 'ingrasaminte',
imagine: 'https://images.unsplash.com/photo-1609361528183-1acc0d399845?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=687&q=80',
pret: [
{'300g': 25.00},
{'500g': 32.00},
{'1000g': 44.00},
],
descriere: 'lorem ipsum blaboajsojas saf sfa fas fasfgaiasfagagjgoajg afaoj foasf jafo afo afdgagagdg a a',
inStoc: 5,
reducere: '10%',
},
^ As you can see above, “pret” is an array that contains key value pairs.
Then, here’s my product page code:
I have set useState because I’m assuming price always changes based on the selection like this:
const [currentProductPrice, setCurrentProductPrice] = useState('');
and then part of my component code is here(I highlighted the part that I had no idea how to do)
<QuantityInput name='cantitate' onChange={handleChange}>
{product.pret.map(valoare => (
<Optiune key={Math.random(10)} value={Object.keys(valoare)}>{Object.keys(valoare)}</Optiune>
))}
</QuantityInput>
</QuantitySelector>
<TotalPriceAndQuantity>
<TotalPrice>
Total:
{???????}
</TotalPrice>
I’ve also made a function like this
const handleChange = (e) => {
console.log(e.target.value);
}
and it works perfectly, i only get the keys, but how do I set the price based on the key selected?
HUGE thanks in advance!
