Override inventory checks when creating an order in Magento

Override inventory checks when creating an order in Magento

October 19, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Override inventory checks when creating an order in Magento

Overriding Inventory Checks for Programmatic Order Creation in Magento

As an experienced e-commerce developer, I often receive queries from merchants who want to override Magento’s default inventory management rules when creating orders programmatically. The question is: how can we force Magento to add out-of-stock items to an order, even if the backorder configuration is set to not allow it?

Let me walk you through a practical solution that bypasses these checks while maintaining the integrity of your inventory system.

Understanding the Default Behavior

By default, when you try to create an order with an out-of-stock item in Magento, one of two things happens:

  1. If backorders are allowed for that product type (configurable products), the order is created successfully.
  2. If backorders aren’t allowed or if the product has no stock, Magento throws a validation error during order creation.

Bypassing Inventory Checks During Programmatic Order Creation

To override this behavior, we’ll temporarily disable the inventory checks and create an event observer that handles the order creation process differently.

Here’s a step-by-step guide:

  1. Disable Inventory Management for Specific Products

    You can use Magento 2’s API to update your products’ backorders field to allow. However, this method is permanent and might not be suitable if you need to allow backorders only during certain periods or conditions.

    For a more granular control, consider creating a separate custom attribute like “Allow Backorder Temporarily” and use it in your order creation process. This approach allows you to enable backorders for specific products as needed.

  2. Create an Event Observer

    In your custom Magento 2 module, create an event observer that listens for the sales_order_save_before event. This event triggers right before a sales order is saved in the database.

    Here’s a basic example of how to set up this observer:

    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/config.xsd">
        <event name="sales_order_save_before">
            <observer name="your_company_allow_backorders_observer" instance="YourCompany\AllowBackorders\Model\Observer"/>
        </event>
    </config>
    
  3. Implement the Observer

    In your observer class, you can check if certain conditions are met (like a specific date range or an admin flag) and then set the allowed_backorder attribute to true for those products.

    Here’s some sample code:

    <?php
    
    namespace YourCompany\AllowBackorders\Model;
    
    use Magento\Framework\Event\Observer;
    use Magento\Framework\Event\ObserverInterface;
    use Magento\CatalogInventory\Model\StockItemRepository;
    
    class Observer implements ObserverInterface
    {
        private $stockItemRepository;
    
        public function __construct(StockItemRepository $stockItemRepository)
        {
            $this->stockItemRepository = $stockItemRepository;
        }
    
        public function execute(Observer $observer)
        {
            // Get the order object from the observer data
            $order = $observer->getEvent()->getOrder();
    
            if ($this->shouldAllowBackorders()) {
                foreach ($order->getItems() as $item) {
                    $productId = $item->getProductId();
                    $stockItem = $this->stockItemRepository->getById($productId);
    
                    // Set the allowed backorder flag to true
                    $stockItem->setAllowedBackorders(true);
                    $this->stockItemRepository->save($stockItem);
                }
            }
        }
    
        private function shouldAllowBackorders()
        {
            // Implement your own logic here, e.g., check a custom admin flag or date range
            return true;
        }
    }
    

    Remember to replace YourCompany with your actual vendor name.

Conclusion

Overriding inventory checks in Magento programmatically isn’t a straightforward task. It requires careful planning and a deep understanding of the system’s architecture. The solution I’ve outlined here provides an effective way to bypass these checks during specific order creation processes while maintaining control over your inventory management system.

It’s essential to ensure that any changes you make are documented properly and tested thoroughly before deployment, especially if they involve critical business logic like inventory management.

At Laravel Company, we specialize in providing expert Magento development services, including custom module development and event observer implementation. If you find yourself needing more advanced solutions or require professional assistance with your e-commerce platform, don’t hesitate to reach out to our team for a consultation.

Magento 2 Catalog Database API

Our network