Followup to #2506163: Complete the initial product architecture.
Also needed for the add to cart form.
Commerce 1.x has no API for determining if a product is available. This means that modules such as commerce_stock need to register various different validation handlers (on the different add to cart forms) to be able to prevent a product from being added to the cart.
There's also the "Set the product price to NULL from a pricing rule" way of doing it, but it's bad DX.
Solution:
- AvailabilityCheckerInterface (with a check($variation, $quantity) method) -> implemented by services tagged with commerce_product.availability_checker.
The check returns TRUE (available), FALSE (not available), or NULL (no opinion).
We should replace these with value objects later, allowing the return values to be expanded in the future without breaking BC.
- AvailabilityManager service (with the same check method) which gets all of the registered checkers, runs them until it gets a FALSE.
One FALSE means that the variation is not available. This service can maintain a static cache of results.
These classes need to live in commerce_order. We'll make it product entity type agnostic in one of the iterations.
The add to cart form will check the availability of the selected variation, disable the add to cart button if the variation is not available.
It will repeat the same check when the form is submitted.
Use cases:
- Deny availability if the variation "available on" date is before the current date
- Deny availability if the variation is out of stock (commerce_stock or custom stock check)
Comments
Comment #1
bojanz commentedComment #2
bojanz commentedComment #3
bojanz commentedMerged in https://github.com/commerceguys/commerce/pull/224
Comment #4
bojanz commentedWrong issue.
Comment #6
bojanz commentedMerged in https://github.com/commerceguys/commerce/pull/225