Change Color of ‘Availability: In Stock’ Based on Quantity in Magento 1.7

magento-1.7productstock

I changed the color of "Availability: In stock" in green but i want to change the color "Availability: In stock" programmly.
For example

if the quantity of product < 5 i want to color "Availability: In
stock" in red

if the quantity of product > 5 and the quantity of
product < 10, i want to color "Availability: In stock" in orange

if the quantity of product > 5 and the quantity of product <
10, i want to color "Availability: In stock" in green

Best Answer

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.

Related Topic