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

bojanz created an issue. See original summary.

mglaman’s picture

OrderRefresh++

Manager doesn't make sense because it isn't doing anything but applying tagged services - doesn't to collection or instance creation.

mglaman’s picture

Will this live in Order or Cart?

Currently the refresh settings are owned by the cart.

commerce_order.commerce_order_type.*.third_party.commerce_cart:
  type: mapping
  label: 'Shopping cart settings'
  mapping:
    cart_form_view:
        type: string
        label: 'Shopping cart form view'
    refresh_mode:
        type: string
        label: 'Shopping cart refresh mode'
    refresh_frequency:
        type: integer
        label: 'Shopping cart refresh frequency'

EDIT:

Maybe default always returns TRUE and cart overrides service to have needsRefresh check this?

EDIT EDIT

I can read.

Can always be TRUE for the initial implementation.

mglaman’s picture

mglaman’s picture

Assigned: Unassigned » mglaman
Status: Active » Needs work
mglaman’s picture

Writing test, found a nice recursion issue.

When the entity validates then we call loadUnchanged and load, then post load and refresh again.

7.2944   94667688   8. Drupal\Tests\commerce_order\Kernel\OrderRefreshTest->testAvailabilityOrderRefreshProcessor() /Users/mglaman/Drupal/sites/commerce2x/vendor/phpunit/phpunit/src/Framework/TestCase.php:909
    7.5828   93529840   9. Drupal\commerce_order\OrderRefresh->refresh() /Users/mglaman/Drupal/sites/commerce2x/web/modules/contrib/commerce/modules/order/tests/src/Kernel/OrderRefreshTest.php:144
    7.6492   93708024  10. Drupal\Core\Entity\ContentEntityBase->validate() /Users/mglaman/Drupal/sites/commerce2x/web/modules/contrib/commerce/modules/order/src/OrderRefresh.php:68
    7.6492   93708088  11. Drupal\Core\TypedData\TypedData->validate() /Users/mglaman/Drupal/sites/commerce2x/web/core/lib/Drupal/Core/Entity/ContentEntityBase.php:373
    7.6565   93808320  12. Drupal\Core\TypedData\Validation\RecursiveValidator->validate() /Users/mglaman/Drupal/sites/commerce2x/web/core/lib/Drupal/Core/TypedData/TypedData.php:135
    7.6639   94011424  13. Drupal\Core\TypedData\Validation\RecursiveContextualValidator->validate() /Users/mglaman/Drupal/sites/commerce2x/web/core/lib/Drupal/Core/TypedData/Validation/RecursiveValidator.php:90
    7.6640   94011976  14. Drupal\Core\TypedData\Validation\RecursiveContextualValidator->validateNode() /Users/mglaman/Drupal/sites/commerce2x/web/core/lib/Drupal/Core/TypedData/Validation/RecursiveContextualValidator.php:99
    7.7302   94391304  15. Drupal\Core\TypedData\Validation\RecursiveContextualValidator->validateConstraints() /Users/mglaman/Drupal/sites/commerce2x/web/core/lib/Drupal/Core/TypedData/Validation/RecursiveContextualValidator.php:140
    7.7339   94460136  16. Drupal\Core\Entity\Plugin\Validation\Constraint\EntityChangedConstraintValidator->validate() /Users/mglaman/Drupal/sites/commerce2x/web/core/lib/Drupal/Core/TypedData/Validation/RecursiveContextualValidator.php:185
    7.7339   94460184  17. Drupal\Core\Entity\EntityStorageBase->loadUnchanged() /Users/mglaman/Drupal/sites/commerce2x/web/core/lib/Drupal/Core/Entity/Plugin/Validation/Constraint/EntityChangedConstraintValidator.php:20
    7.7340   94460200  18. Drupal\Core\Entity\EntityStorageBase->load() /Users/mglaman/Drupal/sites/commerce2x/web/core/lib/Drupal/Core/Entity/EntityStorageBase.php:109
    7.7340   94460712  19. Drupal\Core\Entity\EntityStorageBase->loadMultiple() /Users/mglaman/Drupal/sites/commerce2x/web/core/lib/Drupal/Core/Entity/EntityStorageBase.php:212
    7.7464   94487144  20. Drupal\commerce_order\OrderStorage->postLoad() /Users/mglaman/Drupal/sites/commerce2x/web/core/lib/Drupal/Core/Entity/EntityStorageBase.php:249
    7.7465   94487176  21. Drupal\commerce_order\OrderRefresh->refresh() /Users/mglaman/Drupal/sites/commerce2x/web/modules/contrib/commerce/modules/order/src/OrderStorage.php:70
    7.8764   94653416  22. Drupal\Core\Entity\ContentEntityBase->validate() /Users/mglaman/Drupal/sites/commerce2x/web/modules/contrib/commerce/modules/order/src/OrderRefresh.php:68
    7.8764   94653464  23. Drupal\Core\TypedData\TypedData->validate() /Users/mglaman/Drupal/sites/commerce2x/web/core/lib/Drupal/Core/Entity/ContentEntityBase.php:373
    7.8764   94653464  24. Drupal\Core\TypedData\Validation\RecursiveValidator->validate() /Users/mglaman/Drupal/sites/commerce2x/web/core/lib/Drupal/Core/TypedData/TypedData.php:135
    7.8765   94654656  25. Drupal\Core\TypedData\Validation\RecursiveContextualValidator->validate() /Users/mglaman/Drupal/sites/commerce2x/web/core/lib/Drupal/Core/TypedData/Validation/RecursiveValidator.php:90
    7.8765   94654704  26. Drupal\Core\TypedData\Validation\RecursiveContextualValidator->validateNode() /Users/mglaman/Drupal/sites/commerce2x/web/core/lib/Drupal/Core/TypedData/Validation/RecursiveContextualValidator.php:99
    7.8772   94658176  27. Drupal\Core\TypedData\Validation\RecursiveContextualValidator->validateConstraints() /Users/mglaman/Drupal/sites/commerce2x/web/core/lib/Drupal/Core/TypedData/Validation/RecursiveContextualValidator.php:140
    7.8773   94659664  28. Drupal\Core\Entity\Plugin\Validation\Constraint\EntityChangedConstraintValidator->validate() /Users/mglaman/Drupal/sites/commerce2x/web/core/lib/Drupal/Core/TypedData/Validation/RecursiveContextualValidator.php:185
    7.8774   94659664  29. Drupal\Core\Entity\EntityStorageBase->loadUnchanged() /Users/mglaman/Drupal/sites/commerce2x/web/core/lib/Drupal/Core/Entity/Plugin/Validation/Constraint/EntityChangedConstraintValidator.php:20
    7.8774   94655944  30. Drupal\Core\Entity\EntityStorageBase->load() /Users/mglaman/Drupal/sites/commerce2x/web/core/lib/Drupal/Core/Entity/EntityStorageBase.php:109
    7.8774   94656248  31. Drupal\Core\Entity\EntityStorageBase->loadMultiple() /Users/mglaman/Drupal/sites/commerce2x/web/core/lib/Drupal/Core/Entity/EntityStorageBase.php:212
    7.8812   94676144  32. Drupal\commerce_order\OrderStorage->postLoad() /Users/mglaman/Drupal/sites/commerce2x/web/core/lib/Drupal/Core/Entity/EntityStorageBase.php:249
    7.8812   94676144  33. Drupal\commerce_order\OrderRefresh->refresh() /Users/mglaman/Drupal/sites/commerce2x/web/modules/contrib/commerce/modules/order/src/OrderStorage.php:70
    7.9417   94843104  34. Drupal\Core\Entity\ContentEntityBase->validate() /Users/mglaman/Drupal/sites/commerce2x/web/modules/contrib/commerce/modules/order/src/OrderRefresh.php:68
    7.9417   94843152  35. Drupal\Core\TypedData\TypedData->validate() 
mglaman’s picture

We have a test! Tests the availability manager and that a line item gets removed: https://github.com/drupalcommerce/commerce/pull/491/files#diff-6902b6156...

agoradesign’s picture

As 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?

  • mglaman authored 58df25b on 8.x-2.x
    Issue #2800223: Create an order refresh process
    
    
mglaman’s picture

Status: Needs work » Fixed

WE HAVE MERGE! Order processors for the refresh are in. Example with Availability Manager exists. These are swapped at compile time like any other service.

mglaman’s picture

Status: Needs work » Fixed

WE HAVE MERGE! Order processors for the refresh are in. Example with Availability Manager exists. These are swapped at compile time like any other service.

agoradesign’s picture

After 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...

mglaman’s picture

Issue summary: View changes

agoradesign, 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.

agoradesign’s picture

ok, I've opened up an issue :)

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.