0

A product in Drupal Commerce 2 has limited stock. Users can add-to-cart so long as there is a current stock value > 0, and stock is decremented at the order-placed stage. If a second concurrent user passes the order-placed stage first and has used all available stock, when user 2 gets to the order-placed stage I want to take action (remove item from cart, show a message, do something if no other items in cart..).

I am catching 'commerce_order.place.pre_transition' event, and can manipulate order items, but I'm not sure if I'll mess up other DC processes. Would it be better to have a CheckoutPane plugin to do something pre-placement at/after the Review stage, or something else? The action needs to be taken just pre payment attempt.

0

I created a CheckoutPane plugin which worked well in order-information step as it can can include form fields so can use form-validate function to stop progress until cart manually corrected. In theory the stock could change between order-information and review steps. The panel is automatically processed in review step but it is only a display string not a form so cannot enforce anything here.

It is possible to use HOOK_form_FORM_ID_alter (mymodule_form_commerce_checkout_flow_multistep_default_alter) which is called on submit of review step - so can add a validate function and stop things

The Pane approach gives a better UI space for explaining things to user as HOOK.. can mostly only generate a validation error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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