0

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

2 Answers 2

2

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.

5
  • I must use css to change the color
    – Amirov
    Commented Sep 25, 2013 at 13:03
  • I've edited my answer.
    – Marius
    Commented Sep 25, 2013 at 13:11
  • I will added this code in this file app\code\core\Mage\Catalog\Block\Product\View\Type\simple.php in a function ?
    – Amirov
    Commented Sep 25, 2013 at 13:35
  • @Amirov You can add it directly in the template. It's easier and you don't have to override classes.
    – Marius
    Commented Sep 25, 2013 at 13:39
  • I confused with other things thank you that's work
    – Amirov
    Commented Sep 25, 2013 at 14:14
0

Would you know how to implement this in magento 2.4?

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.