Skip to content

Magento 2 show current stock for salable quantity on productpage

I have this code for showing if my product is in stock or not.

 <?php /* @var $block MagentoCatalogBlockProductViewAbstractView */?>
 <?php $_product = $block->getProduct() ?>
 <?php
 $stockstate = MagentoFrameworkAppObjectManager::getInstance()- 
 >get('MagentoInventorySalesApiApiGetProductSalableQtyInterface');
 $sale_qty = $stockstate->execute($_product->getSku(), 1);
 ?>

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

What I want to add is the number of products in stock like 1,5,7 pcs etc.
So it must be that avilability in stock that needs to be changed.

Could anyone help me with that so I get the number of products in stock if more than 1 pcs in stock and if salable quantity is 0 or below it displays out of stock.