Stock Quantity on Magento Product Page

back to tech articles
Magento Community Edition 1.7.0.2

Warning: This is a quick-n-dirty hack and will not work on all product types.

Instead of just showing the “in stock” and “out of stock” messages, we want to have an in-between message to show that stock levels are running low. This is easily done, by editing the following file:

app/design/frontend/base/default/template/catalog/product/view/type/default.phtml

We change the contents of the file to the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php
$_product = $this->getProduct();
$_quantity = intval(Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty());
?>

<?php if ($_product->isAvailable()): ?>
  <?php if ( intval($_quantity) < 10 ): ?>
    <p class="availability last-stock"><?php echo $this->__('Availability:') ?>
      <span><?php echo $this->__('Only '); echo $_quantity; echo $this->__(" left"); ?></span>
    </p>
  <?php else : ?>
    <p class="availability in-stock"><?php echo $this->__('Availability:') ?>
      <span><?php echo $this->__('In stock') ?></span>
    </p>
  <?php endif; ?>
<?php else: ?>
  <p class="availability out-of-stock"><?php echo $this->__('Availability:') ?>
    <span><?php echo $this->__('Out of stock') ?></span></p>
<?php endif; ?>

<?php echo $this->getChildHtml('product_type_data_extra') ?>
<?php echo $this->getPriceHtml($_product) ?>

That’s it!

We just created a warning-type level once stock drops to less than 10 items. Meanwhile, both the “in-stock” and “out-of-stock” messages are preserved. This can be easily styled using CSS. Note we added the class “last-stock” to our new ptag

😉

WordPress › Error

There has been a critical error on your website.

Learn more about debugging in WordPress.