James James - 13 days ago 5
Javascript Question

jQuery iff less than 10 do this, else if 0 do this not working

rather new to JS so having a little problem and tried a few things... anyone have any ideas?

if (variant.inventory_management == "shopify" && variant.inventory_policy != "continue") {
if (variant.inventory_quantity < 10) {
$lowStockAmount.html('Last few remaining! only ' + variant.inventory_quantity + ' left');
} else if (variant.inventory_quantity < 1) {
$lowStockAmount.html("We're sold out, Don't worry! <a href='#'>click here</a> to be notified when it's back in stock.");
}
}


I tried == "0" as well but same thing, just shows last few remaining! only 0 left but if it is 0 want it to show that sold out message,

Thanks in advance for any help

Answer

Here is a simplified answer to your problem. If the quantity is 0, show the "We're sold out" info. Else, if the quantity is not 0 but less than 10, show the "Last few remaining" info. Otherwise don't show anything.

var quantity = 10;

if (quantity === 0) {
    console.log("We're sold out, Don't worry! <a href='#'>click here</a> to be notified when it's back in stock.");
} else if (quantity < 10) {
    console.log('Last few remaining! only ' + quantity + ' left');
}

The quantity == "0" will not work, because you're comparing a number with a string.

Comments