If you load a product display node in a tab with an Add to Cart form displayed but disable that product in another tab, you'll end up being able to submit an Add to Cart form that is now displayed in your first tab in an inconsistent state. On reload, that form would be rebuilt to just show a "Product not available" button, because disabled products cannot be added to the cart. This results in fun errors like those uploaded in the comment below.
The solution I believe is for us to add a validate handler to check and ensure the rebuilt form still has the selected product available for purchase. If it does not, we should display a "This product is no longer available." message and trigger a rebuild with $form_state['rebuild'] = TRUE.
Comments
Comment #1
rfayNice. Here's what the current situation looks like when you add an item to the cart that's no longer available:
Comment #2
rszrama commentedTagging. Should be a quick fix.
Comment #3
megensel commentedPatch that loads the product to check if it is valid.
Comment #4
rfayMighty slick and nice; works perfectly with and without javascript. However, in testing, I find that the form_set_error() doesn't work anyway, and there's no need to rebuild the form. Here's a simpler patch; see what you think.
Comment #5
megensel commentedThanks, not sure why the form_set_error is not working but in all honesty I don't think there is a need to use it. I made one minor change to your code and set the message type to warning.
Comment #6
rfayWorks for me. This is such an edge case anyway, and @megensel's code works so nicely, that I think this is plenty good enough.
Comment #7
damien tournoud commentedPlease remember that setting #disabled to something doesn't prevent submission of that something. The Form API will protect against this and ignore values matching elements in #disabled state, but the form can still be submitted. The Form API doesn't prevent form submission even if there is no valid button submitted (which explains why it is way better to use button-level #validate and #submit rather then their form-level counterparts).
Here is a proper patch: we need allow the "Product not available" button to be pressed, and prevent the validation of the form in that case.
Comment #8
rszrama commentedThanks for the patches all, and for the explanation, Damien. I always get confused by what happens in FAPI w/ #disabled = TRUE and #access = FALSE.
Committed Damien's patch as is.
Comment #9.0
(not verified) commentedImage future proof.