You can get the available qty like this
$qty = $_product->getStockItem()->getQty();
But you have to be careful. You can have products where the line above returns 0 but the product is in stock because Manage stock
is set to No
.
To check this use:
$manageStock = $_product->getStockItem()->getManageStock();
Now combining these 2 you can get the color like this:
$manageStock = $_product->getStockItem()->getManageStock();
if (!$manageStock) {
$color = 'green'; //forever in stock
}
else {
$qty = $_product->getStockItem()->getQty();
if ($qty < 5){
$color = 'red';
}
elseif ($qty < 10) {
$color = 'orange';
}
else {
$color = 'green';
}
}
[EDIT]
Now change the element that wraps the availability to this:
<p class="availability in-stock <?php echo $color?>">
and add this to your css file
p.green{color:green}
p.orange{color:orange}
p.red{color:red}
note: This works for simple products.
If you want to add out of stock products to cart without changing any configuration from the backend then you have added enable super mode of the product during add to card
$productInfo->setIsSuperMode(true) ;
...->addProduct($productInfo, $requestInfo);
Best Answer
Try with this
And after