0

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

1 Answer 1

0

I finally found a solution :

replace :

$_product->getQty() 

by

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

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

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.