Php / Magento根据库存数量显示图像

Nobody that can help me? (update 17-02)

Basicly what I need it the following.

  • Get the qty of stock of a product
  • Show qty number X
  • When negative number show 0
  • When it's > 0 show image X
  • When it's < 0 show image Y

Old info

In magento I am showing the stock qty with the actual numbers. When the number is lower then 0 he always shows 0. What I now want to do is add an image to both conditions.

For example when the qty is > 0 show a green image and when the qty = 0 then show a red image. Anybody knows how to do this?

 <div class="qty-amount2">
        <?php //echo (int) Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty(); ?>
        <?php  $_op_voorraad = (int) Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty();
            if($_op_voorraad < 0){
                $_op_voorraad = 0;
            }
            echo $_op_voorraad; ?><span><?php echo $this->__(' op voorraad') ?></span>

Ok got this fixed by hiring a developer but will share it

 ?>
<?php /* @var $this Mage_Catalog_Block_Product_View_Abstract */?>
<?php $_product = $this->getProduct() ?>
<?php if($_product->isSaleable()): ?>
<?php  $_op_voorraad = (int) Mage::getModel('cataloginventory/stock_item')-       >loadByProduct($_product)->getQty();?>
<?php if($_op_voorraad > 0): ?>
    <div class="qty-amount2"><img style="float: left;"   src="image_path" alt="">
        <?php echo $_op_voorraad; ?><span><?php echo $this->__(' op voorraad') ?></span>
    </div>
<?php else: ?>
    <?php $_op_voorraad = 0;?>
    <div class=" qty-amount2 geen"><img src="image_path" alt="">
        <?php echo $_op_voorraad; ?><span><?php echo $this->__(' op voorraad') ?></span>
    </div>
<?php endif;?>
<?php else: ?>
<div class="geen"><img src="image_path"   alt="">
<p><?php echo $this->__('Availability: Out of stock.') ?></p></div>
<?php endif; ?><br>

<?php echo $this->getPriceHtml($_product) ?>
<?php 
$stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty();

    if ($_product->isAvailable()): 
        if(intval($stock)>0){
            //green image code
        }else{
            //red image code
        }
    endif; 
?>      

You don't need to know the stock quantity.

If the item is in Out of Stock the isSaleable() method return 'false'. You can use this method.

For Example:

        <?php if($_product->isSaleable()): ?>
            <img src="[linkimage1]" width="135" height="135" alt="" />
        <?php else: ?>
            <img src="[linkimage2]" width="135" height="135" alt="" />
        <?php endif; ?>

Remember to enable the Out of Stock visibility on frontend:

from admin:System->Catalog\Inventory->Stock Options->Display Out of Stock Products ('Yes')

Reindex all.