0

I have this code:

<?php if($_product->isSaleable()): ?>
    <p><?php echo $this->__('In stock:') ?>
    <?=(int)Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty() ?>
    <?php echo $this->__(' stk.') ?></p>
<?php endif; ?> 

It works great for simple products, shows how many left.

But i also use configurable products. How can I get the code to get the stock of the simple product when the parent is a configurable? Now it shows 0.

1
  • you should to use ajax for that because we get child product only when we select any product. Please let me know if you want more detail.
    – Nits
    Commented Jan 9, 2018 at 4:47

4 Answers 4

2

Use the following for all types of products in order to check the stock:

if ($_product->getTypeInstance(true)->isSalable($_product)) {

}

Haven't checked but you can try this as well:

if ($_product->isAvailable()) {

}
1

As each option of configurable product has its own stock it is not possible to tell how much is left until visitor selects one (or more if more then one is configured).

1

In case anyone is still looking for the answer, I use this code in CE 1.9 to determine if there are 2 or fewer items left in stock and display a message. It sounds like you would be interested in displaying the value of $stock for each iteration of the loop along with the option title...

if($_product->getTypeId() == "configurable"):
    foreach ($_product->getTypeInstance(true)->getUsedProducts(null, $_product) as $simple) {
        $stock = (int)Mage::getModel('cataloginventory/stock_item')->loadByProduct($simple)->getQty();
            $ttlStock = $ttlStock + $stock;
    }
    if ($ttlStock <= 2 && $ttlStock > 0) {
        echo '<div class="onlyLeft">Only ' . $ttlStock . ' left!</div>';
    }
endif;
1
  • Hi! Where I can put this code you posted? I'm working on a store that will sell bodies for babies and I will have to use configurable products but using only the attribute "size" to create them. I'm looking exactly for that solution, I mean, to display that message "Only X in stock" depending on size qty. Commented Mar 12, 2017 at 7:15
0

Go to Inventory Tab, and select "No" for Manage stock in the configurable listing.

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.