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. 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.
Also Read:
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
<?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!
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.
Thank You.
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 businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
Prev
How to Add Custom Field in Invoice Totals in Magento 2 Invoice Email
How to Get Current Product ID in Magento 2
Next