$_product->getQty() return 0 - Magento_Catalog default.phtml

I'm trying to override Magento/Catalog/templates/product/view/type/default.phtml

With this code :

<?php
/**
 * Copyright © 2013-2017 Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

?>
<?php /* @var $block \Magento\Catalog\Block\Product\View\AbstractView */?>
<?php $_product = $block->getProduct() ?>

<?php if ($block->displayProductStockStatus()): ?>
    <?php if ($_product->isAvailable() && $_product->getQty() >= 1): ?>
        <div class="stock available" title="<?php /* @escapeNotVerified */ echo __('Availability') ?>">
            <span class="status-instock"><?php /* @escapeNotVerified */ echo __('In stock') ?></span>
        </div>
    <?php elseif($_product->isAvailable() && $_product->getQty() <= 0): ?>
        <div class="stock available" title="<?php /* @escapeNotVerified */ echo __('Availability') ?>">
            <span class="status-preorder"><?php /* @escapeNotVerified */ echo __('Preorder') ?></span>
        </div>
    <?php else: ?>
        <div class="stock unavailable" title="<?php /* @escapeNotVerified */ echo __('Availability') ?>">
            <span class="status-outofstock"><?php /* @escapeNotVerified */ echo __('Out of stock') ?></span>
        </div>
    <?php endif; ?>
<?php endif; ?>

It seem that $_product->getQty() always return 0

How can I get the true stock available for my products ?

Thank you in advance !

Regards

Solutions

I finally found a solution :

replace :

$_product->getQty() 

by

$_product->getExtensionAttributes()->getStockItem()->getQty()

Please tell me if it's a good solution :)

Tags: Magento 2.x

Similar questions

Magento2 Integration Tests fails when installing data for Magento_Catalog test db throwing Asymmetric transaction rollback
I am trying to run the full set of standard integration tests, which I want to run before deployments on an continuous integration server. (Whilst developing I will run just for my own module using the methodology from here.) However I can't get the test database to install. It is throwing: What are my steps for debugging? Namely: Here is the full ...
Access product sku within app/design/frontend/theme/Magento_Catalog/templates/product/price/amount
Is there a way to get the product sku within the app/design/frontend/theme/Magento_Catalog/templates/product/price/amount.phtml file? I need to retrieve the product sku value there, because i want to insert it to });
Magento 2 : what is use of _replica table in Magento_Catalog module?
As Magento releases 2.X version, I found _replica tables, particularly in the Magento_Catalog module may be for indexing the catalog data. I also notice that _replica is nothing but the duplicate structure of the respective table in database nature but it means a lot for Magento 2 framework for read and write operational activities I guess. I want ...
override Magento_Catalog in 2.2
I want to override prices in catalog module. What I did was to test on actual phtml It works as intended. Now, to do it properly, I left file as it was originally and copied files and directory into my theme, made same modifications. Path where I copied files: But changes are not being applied. is that the path I have to use?
Localhost install stuck at 57% Module 'Magento_Catalog':
Everything on the installation is fine until this part. It has been 1 hour it has been sitting at 53% and no errors in the console. I am using Xampp.
Adding Magento_Catalog to Custom Module
I wanted to override the content of addtocart.phtml which is located in vendor/magento/module-catalog/view/frontend/templates/product/view/addtocart.phtml Now I want to modify the content of this and add this to my custom module located in app/code/CustomVendor/CustomModule/view/frontend/templates/product/view/addtocart.phtml But my changes does no...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.