How to Programmatically Check if Stock is Decreased When Order is Placed in Magento 2
Managing well-structured inventory is one of the most challenging and troublesome tasks for any E-commerce store owner. However, Magento 2 inventory management can’t be overlooked as it is one of the crucial factors that define the success of any business.
Magento 2 store owner applies various tactics to manage an accurate inventory that guarantees better control and planning of production and sales. Maintaining an inventory grid by generating an inventory report is one such tactic to handle inventory effectively.
While working with such inventory-related tasks, a developer often needs to check the selected value of the default Magento 2 inventory management system.
Magento 2 offers various options to manage stock and inventory. One such option is a “Decrease Stock When Order is Placed” from Stores > Settings > Configuration > Catalog > Inventory > Stock Options that decreases the quantity in stock when order is placed if set to “Yes” and vice versa, for “No”.
Now, what if you want to add a column in your inventory records grid that displays the particular product’s salable quantity changes?
You first have to programmatically check if stock is decreased when order is placed in Magento 2 and then set a necessary condition to display a record of stock set to “Yes” in the “Decrease Stock When Order is Placed” option. And even for such situations you can also Programmatically Check Whether Stock is Managed for Particular Product in Magento 2 or not so that you can maintain the stock and not let it decrease.
Check out the below code to do so.
You may need to do the same with the “Manage Stock” option too, and for that, you can refer to Programmatically Check Whether Stock is Managed for Particular Product in Magento 2.
Method to Programmatically Check if Stock is Decreased When Order is Placed 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 37 38 39 40 41 42 43 44 45 46 |
<?php namespace Vendor\Module\Helper; use Magento\CatalogInventory\Model\Configuration; use Magento\Framework\App\Helper\AbstractHelper; use Magento\Framework\App\Helper\Context; use Magento\Framework\Exception\NoSuchEntityException; use Magento\Store\Model\StoreManagerInterface; /** * Class IsStockDecreaseWhenOrderIsPlaced */ class IsStockDecreaseWhenOrderIsPlaced extends AbstractHelper { /** * @var Configuration */ private $inventoryConfiguration; /** * @var StoreManagerInterface */ private $storeManager; /** * IsStockDecreaseWhenOrderIsPlaced constructor. * @param Configuration $inventoryConfiguration * @param StoreManagerInterface $storeManager * @param Context $context */ public function __construct(Configuration $inventoryConfiguration, StoreManagerInterface $storeManager, Context $context) { parent::__construct($context); $this->inventoryConfiguration = $inventoryConfiguration; $this->storeManager = $storeManager; } /** * @return bool * @throws NoSuchEntityException */ public function isStockDecreaseWhenOrderIsPlaced() { return $this->inventoryConfiguration->canSubtractQty($this->storeManager->getStore()->getId()); } } |
That’s it.
If you have queries, feel free to ask in the Comments section below.
I would like to solve your problem.
Don’t forget to share this post 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
Announcing the Exciting Partnership of Meetanshi With TemplateMonster
How to Use Grid Renderer in Magento 2
Next