Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
In D7 Commerce there was an automatic rounding for CHF currency (in Switzerland) where prices are rounded to the next 0.05 CHF. I think that this feature has been introduced with the applied patch of https://www.drupal.org/project/commerce/issues/958944 for D7 Commerce 1.
Is there an equivalent functionality for D8 Commerce 2?
Comments
Comment #2
bojanz CreditAttribution: bojanz commentedWe evaluated this feature for 2.x back in 2015 with a number of our Swiss contributors, and the consensus was that the Commerce 1.x feature was not implemented correctly and shouldn't be ported over.
Rounding to the nearest 0.05 CHF is called cash rounding, or Swedish rounding.
By definition it should only be applied to the order total (19.93 -> 19.95), and it should only be done if the order will be paid in cash.
The Commerce 1.x approach is to do this via currency rounding, which means that each individual (per-order-item) VAT amount and discount is rounded to the nearest 0.05, and done regardless of the payment method used.
Assuming that our understanding is correct, the right approach would be to provide a custom adjustment type (called "Rounding" or something similar), and an order processor, which would calculate the required amount, and add it as an order-level adjustment (using the newly defined adjustment type). The order processor would also need to look at $order->payment_gateway to decide whether to kick-in. That would also allow sites to decide whether to always round, or to round for a specific gateway only (such as "cash on delivery").
I think that it would be best for this functionality to live in a contrib module (commerce_cash_rounding?), and not in Commerce core.
Comment #3
bojanz CreditAttribution: bojanz commentedImproving the title.
Comment #4
M_Z CreditAttribution: M_Z commentedThank you bojanz for pointing the direction for a solution.
Some more detailed Questions:
1. Could you tell me some Commerce core sub-modules or Commerce contrib modules that implement an order processor which is doing something similar as you told ("calculate the required amount")? I think that tax or discount calculation could be used as starting point for a rounding contrib module, but maybe you can suggest a less complex module?
2. Must the adjustment type be declared as plugin or something similar? (Maybe the sample module from question 1 shows such a declaration?)
The part of your answer with "$order->payment_gateway" seems to be clear and I know what you mean.
Many thanks in advance.
Comment #5
czigor CreditAttribution: czigor at Centarro commentedHi M_Z,
1. I'd suggest to look at commerce_shipping's order processor, ShipmentOrderProcessor. The ones in tax and promotion are a bit more complicated.
2.
No, you don't need to define the adjustment type. You can just use any string you want for the type.See Bojan's answer below.Comment #6
bojanz CreditAttribution: bojanz commentedI would still define a custom adjustment type, to make it cleaner.
In yourmodule.commerce_adjustment_types.yml:
And an example order processor in src/RoundingOrderProcessor.php:
Don't forget the entry in yourmodule.services.yml:
Comment #7
bojanz CreditAttribution: bojanz at Centarro commentedClosing since an answer was given in #6.