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.
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.
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.
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.
* Class IsStockManagedForProduct
class IsStockManagedForProduct extends AbstractHelper
* IsStockManagedForProduct constructor.
* @param Context $context
public function __construct(Context $context)
* @param Product $product
* @return bool
public function isStockManagedForProduct(Product $product)
/** @var ProductExtension $productExtension */
$productExtension = $product->getExtensionAttributes();
If you have any doubts regarding this post, just mention them in the Comments section below.
I would be happy to help.
Feel free to share the solution with Magento Community via social media.