Once we have price resolvers (https://github.com/drupalcommerce/commerce/pull/366) we need to implement an order refresh process. That's done by a service that's called after an order has been loaded (by the storage?)
The class could be called OrderRefresh, and have methods needsRefresh($order) and refresh($order).
I thought about OrderRefreshManager too but I'm not convinced the word Manager adds any clarity.
needsRefresh($order) would check the last refresh date ($order->changed?) and use the refresh settings to make a decision. Can always be TRUE for the initial implementation.
refresh($order) does the following:
- Update line item titles from the purchased entity titles
- Resolve the unit prices (using the price resolver from PR #366)
- Clear all adjustments
- Run the order processors.
One order processor is for availability (check, remove line item if not available), one for taxes, one for promotions. In that order.
- Save the order.
Order processors are the same architecture as resolvers, except we run all of them. They are passed to OrderRefresh via addProcessor().
Additional notes: the price resolving and availability checking also work outside of an order, so it would be best to add a Context value object that is passed in both cases. The context has a date, store, user for now, and the OrderRefresh populates those from the order.
Comments
Comment #2
mglamanOrderRefresh++
Manager doesn't make sense because it isn't doing anything but applying tagged services - doesn't to collection or instance creation.
Comment #3
mglamanWill this live in Order or Cart?
Currently the refresh settings are owned by the cart.
EDIT:
Maybe default always returns TRUE and cart overrides service to have needsRefresh check this?
EDIT EDIT
I can read.
Comment #4
mglamanBranch WIP https://github.com/mglaman/commerce/tree/2800223-order-refresh-process
Comment #5
mglamanComment #6
mglamanWriting test, found a nice recursion issue.
When the entity validates then we call loadUnchanged and load, then post load and refresh again.
Comment #7
mglamanWe have a test! Tests the availability manager and that a line item gets removed: https://github.com/drupalcommerce/commerce/pull/491/files#diff-6902b6156...
Comment #8
agoradesign commentedAs we run all of them - which is clearly necessary - how can we easily swap/disable an order processor? Do we want/need some configuration that allows disabling a processor, do we move every implementation, that is not necessarily needed in 90% of the use cases, into submodules, or should we swap services per services.yml or settings.php for divergent needs?
One concrete example came immediately into my mind, when I read mglaman's PR this morning: the availability processor, which removes unavailable line items, may not be wanted in every project. What if, you want to sell products that are currently out of stock - if further the availability is checked based upon stock (e.g. if using commerce_stock or a custom implementation). And there can be different kinds of being unavailable: out of stock is one, but disabled status/removed product variation is another, etc
If you want a more complex availability checking, you'd have to implement your own processor. If you just define it, the default one would also run... so what's the best way here?
I wouldn't go that far to add some extra configuration layer, because in D8 swapping services is easy. But maybe have some implementations, that may often be not needed, reside in optional sub modules? What do you think?
Comment #10
mglamanWE HAVE MERGE! Order processors for the refresh are in. Example with Availability Manager exists. These are swapped at compile time like any other service.
Comment #11
mglamanWE HAVE MERGE! Order processors for the refresh are in. Example with Availability Manager exists. These are swapped at compile time like any other service.
Comment #12
agoradesign commentedAfter a very quick code review on my mobile, there's one possible issue I saw: it seems that needsRefresh() does not check, whether the order is already completed
But I could have overseen something...
Comment #13
mglamanagoradesign, there's probably a handful of wrap ups that slipped through. Feel free to open some follow up issues as you see them. It'll help us push the test coverage.
Comment #14
agoradesign commentedok, I've opened up an issue :)