The commerce checkout flow appears to be resolved only the first time a cart enters checkout. I believe the resolved checkout should be reset once the products in the cart/order change.

I have created an entity trait that requires an extra step during checkout. This trait is attached to a product variation and a custom CheckoutFlowResolver scans through the purchased entities, looking for this field; returning a different checkout flow if the field is detected. (I think this is a valid use case unless there are better options?)

Reproduce:
- Add a regular product to the cart
- Enter checkout
- See the default checkout flow
- Leave checkout (navigate to catalog)
- Add the special product to the cart
- Enter checkout again
- Still see the default checkout flow, where the custom checkout is expected.

The other way around; if the product is on the initial cart and removed later, the checkout is stuck in the custom checkout flow instead.

I'd like to provided a patch, but I do not know where this reset should be done. Since the checkout is stored in the order, I suppose an order event subscriber should clear the checkout flow if order items, quantities, prices, more(?) change?
I don't think a cart event subscriber would suffice as orders can exist without a cart.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Neograph734 created an issue. See original summary.

Neograph734’s picture

Neograph734’s picture

Hmm, I suppose I should have subscribed to the OrderEvents::ORDER_ITEM_DELETE event as well. But let's see for now.

Status: Needs review » Needs work

The last submitted patch, 2: 3083384-resolve_checkout_flow_on_order_change.patch, failed testing. View results

Neograph734’s picture

Status: Needs work » Needs review
FileSize
8.05 KB
1.84 KB

Adding the event subscriber in services, subscribing to order item deletion and actually saving the order after the event fired.

Status: Needs review » Needs work

The last submitted patch, 6: 3083384-resolve_checkout_flow_on_order_change.patch, failed testing. View results

Neograph734’s picture

Seems like is misinterpreted the OrderEvents::ORDER_ITEM_INSERT and other events. Those trigger when the order item entity is created/updated/deleted. Instead, this patch should respond to changes on order items attached to orders; similar to CartEvents::CART_ENTITY_ADD, CART_ORDER_ITEM_UPDATE and CART_ORDER_ITEM_REMOVE.

However I feel this should not apply only to cart orders. There could be situations where an item is sent straight to checkout. So I suppose the Cart events should have a similar event within the order, and then this EventSubscriber should use those events instead.

Neograph734’s picture

I suppose we need some way to know when the order contents have changed. Created a new issue for that.

Neograph734’s picture

Thinking of it, we could also use the Order::recalculateTotalPrice() method to trigger the CheckoutFlowResolver? That would also include changes that happened as the result of an order adjustment. Theoretically there are so many things where a developer might expect the checkout flow to be reset that I am not sure where to draw the line...

bojanz’s picture

#3086876: Allow handling order changes on order level instead of in the cart is a huge refactoring, we can't block a bug fix on it.

An order presave subscriber could detect that an order item has disappeared or was added at least. For updates we'd still need an order item presave subscriber.

Neograph734’s picture

Thanks for the reply, but then what should be the criteria for clearing the checkout flow? For instance would changing the profile also justify a checkout reset?

UPDATE
I guess changing alterations and profiles should not reset the checkout flow for as they can be set during a checkout flow.
Using pre save would be undesirable as well because we would need all kinds of exceptions for changed/completed dates etc.

I think I'll proceed with the events introduced in #3086876: Allow handling order changes on order level instead of in the cart.

Neograph734’s picture

Status: Needs work » Needs review
FileSize
1.82 KB

Let's bring this in line with resetCheckoutStep for now by adding it to the cart manager and then revisit after #3086876: Allow handling order changes on order level instead of in the cart. This should (for now) be good for most of the use cases.

Renrhaf’s picture

Renrhaf’s picture

Status: Needs review » Reviewed & tested by the community

In my use case, I have products that can be either bought or rented. If rented items are in the cart, the checkout flow contains an additional step : signing contracts. I needed to refresh the checkout flow whenever an item is added/removed from the cart. The patch works great!

jsacksick’s picture

Status: Reviewed & tested by the community » Needs review

You can't RTBC your own patches :).

jsacksick’s picture

Status: Needs review » Reviewed & tested by the community

Sorry, missed that this was a reroll.

jungle’s picture

Re-rolled, keep RTBC

jsacksick’s picture

Status: Reviewed & tested by the community » Fixed

Considering that we're doing the exact same thing for the checkout step, I'm fine with this approach, perhaps we could have moved the logic in the same method and rename it, but this is also fine. Committed, thanks!

Status: Fixed » Closed (fixed)

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