How To

How to Add Available Quantity Per Product in The Catalog On Magento 2.3

Magento Folks?,

What’s going on? Welcome back to the How-To series where today the illustration will be on How can I add the available quantity per product in the catalog on Magento 2.3? Lastly, I have illustrated to you Magento 2: How To Import Products with Custom Product Types with appropriate codes.

Let’s Begin??

Introduction:

Follow the given below steps for implementing how you can add the available quantity per product in the catalog on Magento 2.3? 

Step 1: Create a block file in the custom extension

app/code/[VENDOR]/[EXTENSION]/Block/Product.php
<?php

namespace [VENDOR]\[EXTENSION]\Block;

class Product extends \Magento\Framework\View\Element\Template
{
    protected $_scopeConfig;
    protected $_stockInterface;
    protected $_productRepository;

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        \Magento\CatalogInventory\Api\StockStateInterface $stockInterface,
        \Magento\Catalog\Model\ProductRepository $productRepository
    ){
        $this->_scopeConfig = $scopeConfig;
        $this->_stockInterface = $stockInterface;
        $this->_productRepository = $productRepository;
        parent::__construct($context);
    }

    public function getStockMessage($productId){
        $_product = $this->getProductById($productId);
        $_stock = $this->getStock($_product);
        if($_stock <= $this->getThresoldQty()){
            return __('Only %1 left', $_stock);
        }
        return '';
    }

    public function getProductById($id)
    {
        return $this->_productRepository->getById($id);
    }

    public function getStock($_product)
    {
        return $this->_stockInterface->getStockQty($_product->getId(), $_product->getStore()->getWebsiteId());
    }

    public function getThresoldQty(){
        return $this->_scopeConfig->getValue('cataloginventory/options/stock_threshold_qty', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
    }
}

Step 2: Now add below code in list.phtml

$blockObj = $block->getLayout()->createBlock('[VENDOR]\[EXTENSION]\Block\Product');

Step 3: Now, you can call in the block inside your foreach loop.

<?php echo $blockObj->getStockMessage($_product->getId()); ?>

By applying the above-given steps you will be successfully able to add the available quantity per product on the catalog on Magento 2.3

Final Words:

I hope the above-given solution for adding the available quantity per product in the catalog in Magento 2.3 is helpful for you and if you find any difficulties then you can contact MageComp Support for more guidance. And if you like the article then let us know in the comment section below. Don’t forget to share it with your Magento friends and make them more knowledgeable.

Happy Coding?

Click to rate this post!
[Total: 3 Average: 3.7]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

Recent Posts

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

2 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

2 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

3 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

4 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

6 days ago

How Upcoming Cookie Changes Will Affect Your E-commerce Website?

The e-commerce world is constantly in flux. New tech and strategies emerge daily to help…

6 days ago