I need to create a fee which is only applied to certain items (OrderItem fee) but also has a minimum and maximum that must/can be applied of this fee type to each order (Order fee).
Commerce has base classes for both of these fee types. The benefit of the OrderItemFeeBase is that it provides UI to select which product types this fee is applied to. But using this, I am not able to control min/max fee amount added for entire order.
For maximum amount, it would be possible to carry a static running order total to evaluate on each order item and then asses or not based on if total has been exceeded. But for minimum, this is not possible without knowing which item is last (hmm, do I know this?).
I can use an Order fee and analyze each item to see if it is the type the fee should be applied to; but then I lose the nice UI to select types.
Possibly there is an event or alter that can post-process the fee amount after the fees have been totaled? This would allow me to use the OrderItem type but then post-process the total fee for min/max.
Not sure how to add the OrderItemFee UI into the OrderFee UI but OrderFee seems to be the best approach as It can see all line items at once to be able to handle min/max.
The approach at the moment is to hard code the list of product types i don't want included in the fee and (with a cloned version of the Order) remove the order items which aren't of the allowed types.