In the Commerce project I'm currently running, I need to provide tax amounts per line item to the payment service. So, I modified and ran the
CartPrepareWorkflow by calling the oh so nice
OrderGroupWorkflowManager.RunWorkflow(ch.Cart, OrderGroupWorkflowManager.CartPrepareWorkflowName). But with no luck. At least for some hours.
Deep within that nice, nested piece of code, I calculated the tax for each single item, like shown below. This seemed to work out nicely, and as I was running
order.AcceptChanges(); at the bottom of the method, everything should be fine. Shouldn't it? After all, the total taxes for the order form were updated and persisted as they should.
My line items, however, remained unchanged. After digging for hours, I did find the reason, though. In the default code, you find the following:
...so lets take a look at the default
Do notice the
Select(x => new LineItem... part. This means the line items being changed earlier were in fact just copies of the ones actually on the form. So that kindof made sense out of why no changes were persisted. A simple solution is to go directly on the
form.LineItems collections instead (they point at the same object) - like shown below. However, you'd need to handle split shipments logic yourself. Inspiration can probably be found in the old
method we just removed.
Also, further changes to this workflow on line item level should now be possible - making the entire thing quite a bit more intuitive to work with.