Problem/Motivation
Commerce Cart Links currently skips the full order item validation pipeline—including availability checkers, required-field checks, and any custom validation handlers—when adding products via URL-based links. It should enforce the same validation logic as the standard add-to-cart form to prevent invalid items from being added to the cart.
Original issue summary:
The availability checker API constrains adding an item to the cart via the add-to-cart form. An example implementation can be found in the commerce_order_test test module, where items with SKUs starting with TEST_ cannot be added to the cart. In such a case user will see the following (or similar) message:
Side Table is not available with a quantity of 1.
However, this module allows adding all product variations, regardless of their availability status.
Proposed resolution
Check availability before adding the item to the cart. Return 403 if the item cannot be added to the cart.
Issue fork commerce_cart_links-3520311
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
rszrama commentedI'm not sure what the correct course of action should be here, to be honest. On the one hand, availability checkers should be respected, but on the other hand, how they are respected can be context dependent.
I believe this is why, for example, the cart manager itself does not check availability before creating an order item. Additionally, I'm pretty sure Commerce only checks availability after the add to cart has occurred via the order processor, but I could be wrong.
In this case, I'd almost suggest we leave the default behavior as is but optionally execute availability checking first, rendering the message upon failure. (The problem is, that message is pretty non-specific ... but it's the best we have atm.)
Generally speaking, the availability system needs work. Will link to this from our internal Commerce channel for discussion in a future breaking release.
Comment #4
zaporylievalidateon the order item, that would trigger the same code/behavior as the add to cart form.I added MR with a proposal.
Comment #5
rszrama commentedIf we go this direction, I think we may need a couple of different approaches to messaging.
For on-site usage (i.e., a cart link replacing an Add to Cart form in a catalog page), the default messaging is probably fine, though I think taling about "quantity" where there is no quantity input is awkward at best.
For off-site usage (i.e., a link template used in a Google Ad to "buy it now"), I don't think it makes as much sense to talk about a product generically not being available. I envision something more related to a link, promotion, etc. no longer being active, though even there, I'm not sure we can assume. Other ideas?
Comment #6
zaporylieThe MR adopts the same messaging approach as the standard Add to Cart form. It delegates responsibility to the checker to override the generic message with a more specific one that clearly explains why the item couldn’t be added to the cart.
Comment #7
jsacksick commentedhm... I guess this approach is ok though we could probably copy what the EditQuantity views field handler is doing:
This way we flag only errors for the purchased entity field and the quantity... Thoughts?
We could also implement the availability checker directly though the current approach implemented allows developers to opt out for the availability checks if the constraint is removed from the field.
Comment #8
zaporylieI gave some more thought to the proposal in #7. While it makes sense within the scope of the current issue, I believe we should keep the implementation as it is and instead adjust the scope.
In my view, we're aiming to replicate the default behavior of the Add to Cart form—which, like with any other entity type in Drupal, runs validation before creating an order item.
A good example of why this matters: imagine a Commerce site with multiple product types, each tied to its own order item type. Some of those order item types have additional required fields. Full validation ensures we don’t end up creating invalid order items when the Add to Cart form would normally prompt for more data before proceeding.
Comment #9
jsacksick commentedI'm fine with validating all the fields just like the patch is doing atm. So in EditQuantity we only validate the purchased entity and the quantity since these are the only 2 fields the user is editing. In some cases you might have a constraint that could fail with no way course of action to actually fix the validation. But let's go with this :).
Comment #10
zaporylieComment #12
vmarchukCommitted!