Reputation: 25
So I'm trying to make a stock type of system where when a user clicks a button it checks which Items are in the shopping cart and what their names are and the subtotal price of the single product.
So if the subtotal is 1$ I know that the quantity of that product is 1 and then subtract that from the stock in the MySQL database, and if the subtotal is 2$ I know that the quantity is 2 and subtract 2 of the stock of that specific product.
What I've done so far is get the first h4
text and check if that is equal to a product name and then check the price to get the quantity and all of that would be in a if else statement which is a pretty bad way of doing it since I would have to make about 360 if else checks.
This is an example of how a shopping cart could look like and there can be more products in the shopping cart or the price could be more since the quantity per product can be higher
<div class="list-group sc-cart-item-list">
<div class="sc-cart-item list-group-item" data-unique-key="1586696968143">
<button type="button" class="sc-cart-remove">×</button>
<img class="img-responsive pull-left" src="">
<h4 class="list-group-item-heading">Iphone 11 6.1</h4>
<p class="list-group-item-text">Yellow</p>
<div class="sc-cart-item-summary">
<span class="sc-cart-item-price">19,99 €</span> × <input type="number" min="1" max="1000" class="sc-cart-item-qty" value="1"> = <span class="sc-cart-item-amount">19,99 €</span>
</div>
</div>
<div class="sc-cart-item list-group-item" data-unique-key="1586697501762">
<button type="button" class="sc-cart-remove">×</button>
<img class="img-responsive pull-left" src="">
<h4 class="list-group-item-heading">Iphone XS Max</h4>
<p class="list-group-item-text">Orange</p>
<div class="sc-cart-item-summary">
<span class="sc-cart-item-price">19,99 €</span> × <input type="number" min="1" max="1000" class="sc-cart-item-qty" value="1"> = <span class="sc-cart-item-amount">19,99 €</span>
</div>
</div>
</div>
This is the script I have so far but its generally very bad since first of all the part where I check the price in a if statement doesn't work and I would have to do this 360 times over for every possible quantiy someone can buy and every possible variation of the product
<script type="text/javascript">
$(document).ready(function() {
$("#vbutton").click(function() {
alert("clicked");
var w = $('h4', '').first().text();
//console.log(v);
var a = $('.sc-cart-item-amount', '').first().text();
//console.log(a);
if (w !== '') {
console.log("Hi");
if (w == "Iphone 11 6.1") {
console.log(a);
if (a == '19,99 €') {
console.log("removed 1 from database");
}
}
}
});
});
</script>
Then I would need to do this for the second, third, fourth and so on text that could be in the shopping Cart. I would really appreciate any help possible, thank you.
Upvotes: 0
Views: 588
Reputation: 308
Do you want to say you want to parse the ''19,99 €'' ?
parseInt('19,99 €'.replace(/,/g, ''));
// 1999
Then you can do some calculate, and get the quantity.
Upvotes: 0