When an order that has previously reduced stock is “cancelled” or goes to “pending payment” because the payment failed or the store admin decided to change the order status, the wc_maybe_increase_stock_levels() function triggers and restores the order items’ stock quantity.
As usual, some WooCommerce entrepreneurs asked for a way to disable this automatic restocking given their custom setup. It often goes like that – you can’t really please everyone.
This is unless you’re a smart developer and can account for both options, thanks to a WordPress “filter“. Here’s a PHP one-liner that can immediately disable this default behavior, so that you can avoid the automatic restocking (and maybe doing it manually based on your business rules). Enjoy!
PHP Snippet: Disable Automatic Restocking When A WooCommerce Order Is Set to Pending / Cancelled
This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101
WooCommerce: Show “Sold Out” @ Shop Page Here’s another simple snippet that can easily help user experience and make sure a “sold out” badge shows on each out of stock product in the category & shop pages. Not all themes allow this so you can use the snippet below to make it happen!
WooCommerce: Display Stock Availability @ Shop Page In this tutorial, my goal is to show the “stock availability” under each product in the shop, category and archive pages. This follows exactly the same settings as the stock display of the single product page. Go to /wp-admin/admin.php?page=wc-settings&tab=products§ion=inventory to manage “Stock display format”. Enjoy!
WooCommerce: Display Out of Stock Products (Shortcode) A client of mine wanted to show out of stock products on a separate page – so I coded a simple shortcode for you all! You can use this shortcode for different goals. For example, you might want to display what products you’ve sold to enhance customer trust / social proof. So let’s see (1) […]
WooCommerce: Display Variations’ Stock @ Shop Page Thanks to the various requests I get from Business Bloomer fans, this week I’m going to show you a simple PHP snippet to echo the variations’ name and stock quantity on the shop, categories and loop pages. Of course, if “Manage stock” is not enabled at variation level, the quantity will be null, and therefore […]
WooCommerce: Create a Custom Order Status All WooCommerce orders go to either “processing”, “completed”, “on-hold” and other default order statuses based on the payment method and product type. Sometimes these statuses are not enough. For example, you might need to mark certain orders in a different way for tracking, filtering, exporting purposes. Or you might want to disable default emails by […]
Business Bloomer Founder
Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli