I'm gonna make my own "reduce stock" functionality in WooCommerce. But first I'm trying to find out which hooks to use and how WooCommerce currently is reducing stock on the "creating order procedure".

I read this Stack Overflow post where it's recommended to add the custom stock decreasing functionality in the woocommerce_payment_complete hook.

And that hook is called after the payment procedure. Which makes me think;

What if someone else buys the last one and is quicker with the payment? Then my payment will complete, and then not reduce the stock. But two products needs to be sent. That's a problem.

Wouldn't it be better is the stock was reduced just before payment and then if payment fails decrease stock again? Why isn't it like that?

Or have I missed something? How does it work? I know there is a "hold stock" functionality too. But I don't really get how that works. I have looked in the check_cart_item_stock() function in class-wc-cart.php and I don't really understand what's happening there. My guess also is that WC's "hold stock" only is for when an order is already created but not paid. Is that right? So it doesn't hold stock when you have a product in cart?

Can someone tell me how WooCommerce ensures that a product doesn't run out of stock during payment?


Browse other questions tagged or ask your own question.