How to Programmatically Check Whether Stock is Managed for Particular Product in Magento 2

Inventory management is one of the most challenging tasks for any eCommerce store owner. However, Magento 2 store owner can take the inventory management at ease because of its new and expanded features include an inventory management system that keeps tracking the stock flow across all sales channels and locations!

Magento 2 provides an option that prevents decreasing the product stock when an order is placed. For example, downloadable and virtual products don’t have any quantity limitations.

For that, Magento 2 inventory system offers two ways to manage your product stock inventory:

  • Global level
  • Product level

In the product level inventory system, if the “Manage Stock” option is enabled from the ‘Advanced Inventory’ of a product edit page, then Magento inventory management automatically tracks product quantities without additional configuration.

How to Programmatically Check Whether Stock is Managed for Particular Product in Magento 2

When the ‘Use Config Settings’ is checked in the “Manage Stock” option, the Magento inventory system prefers the system’s default value passed in the global inventory system.

However, what if you want to get the applied value of the “Manage Stock” option for the particular product programmatically? For such a need, you have to follow the method to programmatically check whether stock is managed for particular product in Magento 2. And even for such situations you can also Programmatically Check if Stock is Decreased When Order is Placed in Magento 2 or not so that you can maintain the stock and not let it decrease.

For instance, you want to add products set to “Yes” in the “Manage Stock” option in your custom inventory grid. If it is set to “No,” then the product should not be displayed in the grid.

Integrate Zoho Inventory Today!

An easy way to sync products with Zoho Inventory without coding.

Get Extension
Magento 2 Zoho Inventory Integration

Use the below code in such a scenario and manage your product stock inventory.

Method to Programmatically Check Whether Stock is Managed for Particular Product in Magento 2

Use the below code in your helper file.

<?php


namespace Vendor\Module\Helper;


use Magento\Catalog\Api\Data\ProductExtension;
use Magento\Catalog\Model\Product;
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;

/**
 * Class IsStockManagedForProduct
 */
class IsStockManagedForProduct extends AbstractHelper
{
    /**
     * IsStockManagedForProduct constructor.
     * @param Context $context
     */
    public function __construct(Context $context)
    {
        parent::__construct($context);
    }

    /**
     * @param Product $product
     * @return bool
     */
    public function isStockManagedForProduct(Product $product)
    {
        /** @var  ProductExtension $productExtension */
        $productExtension = $product->getExtensionAttributes();
        return $productExtension->getStockItem()->getManageStock();
    }
}

That’s it! Likewise you can also manage your inventory using Magento 2 Zoho Inventory Extension.

Feel free to share the solution with Magento Community via social media.

Thank You.

Sanjay Jethva

Article by

Sanjay Jethva

Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe. His passion for Magento 2 and Shopify solutions has made him a trusted source for...