0

I'm working on woocommerce project and I need to check inventory status via external API before I create order. It is a cash register system in physical store that I can't change. I'm using

woocommerce_checkout_order_processed

hook to add my logic here but I'm not sure how can I prevent order from saving if invenotry over API changed in meantime. So the flow I need here is:

  • User wants to buy 10 items, adds them to cart
  • User clicks 'Place Order'
  • hook is activated
  • api is triggered and it returns that only 5 are available at the moment
  • show message and don't save order

1 Answer 1

1

The woocommerce_checkout_order_processed hook is too late. The order has already been created. You'd probably want to hook into woocommerce_after_checkout_validation so that you can check for the inventory before the order is even triggered and set an error that will be displayed at checkout.

By hooking in here, you will be able to short circuit the order creation with the error notice.

You can find more details about this in the WC_Checkout class.

See: https://github.com/woocommerce/woocommerce/blob/trunk/plugins/woocommerce/includes/class-wc-checkout.php#L849-L890

Not the answer you're looking for? Browse other questions tagged or ask your own question.