'Show available stock quantity on product page

Firstly I would like to apologise if this question has been answered somewhere else but I am unable to find what I am looking for as I am new to PHP and assume I need this to solve my problem.

I have built a website and am using Mals-e shopping cart. I have everything up and running but I would like to show how many products are still in stock under the product description and if there are no items in stock. For example:

Available stock: 2

or

Sold Out

I have read that I need a text file with product name, price and quantity, a PHP file to read and rewrite the quantity available and out put the results on the product page and a mypage.php page but I really don't know where to start. I've spent days trying to sort this out.

I have Mysql database with some items in table called (items) with available quantity but don't know how to go about sorting it out. Any help would be most appreciated.

Thank you.



Solution 1:[1]

Without seeing the actual code you're using to display the product, it's hard to say buy all you should need is something like:

<?php
// get the product and stock level
if($product->numberInStock > 0) {
    echo 'Available: ' . $product->numberInStock;
} else {
    echo 'Out of stock';
}

If you're editing a phtml type template (HTML with embedded PHP), you might display it like:

<? if($product->numberInStock > 0): ?>
    <p>Available: <?= $product->numberInStock; ?></p>
<? else ?>
    <p>Out of stock</p>
<? endif; ?>

Solution 2:[2]

Had same issue, found out following.

Inside your catalog/product type template you can use this:

<?php 
$_product = $this->getProduct();
$_qty = $_product->getStockItem()->getQty();
?>
<p>
  <?php if($_qty > 0): ?>
    Available: <?php echo $_qty; ?>
  <?php else: ?>
    Out of stock
  <?php endif; ?>
</p>

Solution 3:[3]

session_start($_POST['quantity']);
if(isset($_POST['quantity']))
{
$postedquantity=$_POST['quantity'];
$productQuantity="20";
if($postedquantity<$productQuantity){
    echo "In stock";
    echo "<br/>";


}
$productQuantity=$productQuantity-$postedquantity;
    echo $productQuantity."Remaining";
}

You can even do like this,storing quantity value in session and everytime it's posted it will check whether it is in stock or not and it will show how much quantity is remaining.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Joran Den Houting
Solution 2 Olexandr Tylnyj
Solution 3