Can I modify a commercetools Order price?
Short answer: no.
commercetools offers some update actions on the Order itself, but none of them helps you in case you want to change the Order price.
And that’s the reason why we will use Order Edits.
What are Order Edits, then?
As per the commercetools official documentation, here’s the Order Edits definition:
OrderEdit are containers for financial changes after an Order has been placed.
If no financial aspect of an Order should be changed, use Update Order, which does not perform a recalculation. If no Deliveries or Payments occurred, you can alternatively replicate the Order as a new Cart.
commercetools Order Edits documentation
A simple strategy to modify the order price
Imagine the following scenario:
- Invoice Issued: Your company issues an invoice for 2016.90€.
- Payment Made: The client pays the invoice via bank transfer after several days.
- Price Adjustment: Due to increased product prices, the order is placed with a total price of 2997.90€.
- Misalignment: There is a misalignment of 81€ between the invoice amount and the current order price.
The simplest strategy is to apply a Direct Discount.
A Direct Discount represents a CartDiscount that is only associated with a single Cart or Order.
Create an OrderEdit
In this code we specify:
- a key (try to give consistent names, guaranteeing sequential keys, because duplicate keys are not tolerated)
- a resource: that’s our order
- stagedActions array
- setDirectDiscounts action to set a direct discount related to the totalPrice with an absolute amount of 81€
- comment: it’s always a good choice to document your action, because Orders are sensitive entities and they should be rarely modified
We obtain a JSON representing the newly created OrderEdit.
The JSON is easy to read.
What’s important is to keep the OrderEdit ID and the OrderEdit version for the next call.
The OrderEdit type should be set to PreviewSuccess.
Apply the OrderEdit
We can now apply the OrderEdit:
We will just pass the OrderEdit ID in the URL, and the OrderEdit version plus the Order version in the body.
That’s it!
In the result you should see an important result section like this:
"result": {
"type": "Applied",
"appliedAt": "2024-05-03T11:09:08.051Z"
[...]
sto confirm that it’s applied.
The result section also reports the previous and current state of the OrderEdit resource, respectively in the excerptBeforeEdit and excerptAfterEdit section:
"excerptBeforeEdit": {
"totalPrice": {
"type": "centPrecision",
"currencyCode": "EUR",
"centAmount": 209790,
"fractionDigits": 2
},
[...]
},
"excerptAfterEdit": {
"totalPrice": {
"type": "centPrecision",
"currencyCode": "EUR",
"centAmount": 201690,
"fractionDigits": 2
},
[...]
}
And that’s it.
The post Unlocking commercetools: Modifying Order Prices Made Simple appeared first on L.S..