0

How to show the total quantity number(Like 10) instead of "In Stock" status in Magento 2.x?

I need help for Magento 2.x. Recently I have developed an eCommerce site. On products details page has "Stock Availability" using "In Stock" status.

So, I want to show the total number of quantity "like Stock: 20" instead of "In Stock" Status and also quantity number will be increased/decrease after update/place order.

Could anyone please help to do that in my Magento 2.x eCommerce website.

Please check my attached images below then you can get the clear idea about my words what I have mentioned in above?

Please tell me where I have to write the code to get total quantity number Like "Stock: 20" enter image description here

0

If you have product object then you will get Qty

<?php 
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$StockState = $objectManager->get('\Magento\CatalogInventory\Api\StockStateInterface');
$qty =$StockState->getStockQty($_product->getId(), $_product->getStore()->getWebsiteId());
?>  

you need add below code inside <?php if ($_product->getIsSalable()): ?> .. else:1.

<?php if($_product->getTypeId() == 'simple'){ ?>
    <?php 
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $StockState = $objectManager->get('\Magento\CatalogInventory\Api\StockStateInterface');
    $qty =$StockState->getStockQty($_product->getId(), $_product->getStore()->getWebsiteId());
    ?>  
<p class="available stock" title="<?php /* @escapeNotVerified */ echo __('Stock') ?>">
    <span><?php /* @escapeNotVerified */ echo $qty ?></span>
</p>
<?php }else{ ?>
<p class="available stock" title="<?php /* @escapeNotVerified */ echo __('Stock') ?>">
    <span><?php /* @escapeNotVerified */ echo __('In stock') ?></span>
</p>
<?php  }?>

note that direct use of object manager is not good

  • How to do it in configurable product? – Dhaduk Mitesh Nov 20 '18 at 9:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.