Automatic stock restoration
WooCommerce reduces stock when an order becomes "Processing", "On hold" or "Completed". It puts the stock back itself when the order is cancelled, but not when the status changes to "Refunded": goods you refunded still count as sold. This module puts them back automatically and makes sure nothing is restocked twice.
Enabling
Section titled "Enabling"Go to Polski > Modules and enable Auto Restore Stock in the "Stock & Cart" section. The module is off by default and has no settings of its own.
Supported status transitions
Section titled "Supported status transitions"| From status | To status | Module checks the order |
|---|---|---|
| Processing | Cancelled | Yes |
| Completed | Cancelled | Yes |
| On hold | Cancelled | Yes |
| Processing | Refunded | Yes |
| Completed | Refunded | Yes |
| On hold | Refunded | Yes |
| Processing | Failed | Yes |
| On hold | Failed | Yes |
Other changes, for example from "Pending payment" to "Cancelled", are skipped. The stock of such an order has usually not been reduced yet.
How it works
Section titled "How it works"- The order goes through one of the transitions in the table.
- The module checks the WooCommerce option Enable stock management. When it is off, nothing happens.
- For every line item whose product manages stock, it reads the
_reduced_stockmarker. WooCommerce stores in it how much it reduced that line, and deletes it when it restocks the line itself. - A line with the marker goes back into stock by the stored quantity and the marker is removed. A line without the marker is skipped.
- Every restored line adds an order note, for example "Stock restored: T-shirt (5 -> 8)".
- When anything was restored, the order gets the
_polski_stock_restoredmeta and is marked as an order whose stock is no longer reduced.
On cancellation WooCommerce usually restocks first and clears the markers, so the module has nothing left to restore. That is why stock never comes back twice.
Example
Section titled "Example"A customer bought 3 items, the order is "Completed" and the product stock is 5. The customer sends the goods back, you refund the money and set the status to "Refunded". The module raises the stock to 8 and adds the note "Stock restored: T-shirt (5 -> 8)". Any later status change of that order does not change stock again.
Requirements
Section titled "Requirements"- The WooCommerce option Enable stock management (WooCommerce > Settings > Products > Inventory) must be on.
- Products must have stock management enabled.
Hook for developers
Section titled "Hook for developers"| Hook | Type | Arguments | When |
|---|---|---|---|
polski/stock/restored |
action | WC_Product $product, WC_Order_Item_Product $item, WC_Order $order |
After one line item is restocked |
add_action('polski/stock/restored', function ($product, $item, $order) { // For example: push the stock change to an external warehouse.}, 10, 3);Can stock come back twice?
Section titled "Can stock come back twice?"No. The module restores only lines that still carry the _reduced_stock marker and removes it afterwards. An order with the _polski_stock_restored meta is skipped entirely.
What about refunding part of an order?
Section titled "What about refunding part of an order?"The module reacts to a status change of the whole order. A partial refund without changing the status to "Refunded" is handled by WooCommerce: in the refund dialog you choose to put the refunded items back into stock.
Does the module act on "Pending payment" orders?
Section titled "Does the module act on "Pending payment" orders?"No. The stock of such orders has usually not been reduced yet, so there is nothing to restore.