I’m looking for the best (i.e. official) way to add a custom order
field in the billing step of the checkout page.
At the end, I want the field’s value to be stored in the sales_order
table.
I understand that extension attributes is the way to go to extend entities like order
.
No place in payload of place-order
action
I’ve gone through the checkout customization tutorials in the official docs. However, I’ve not found an example on how to save additional data in the order entity in particular.
First, I though attaching the input field’s value to the JSON payload of POST /carts/mine/payment-information
would be the way to go. For that, I would have to implement a mixin for place-order.js
. However, the JSON object of that call has the following structure:
{
"billingAddress": { ... },
"cartId": "12345",
"paymentMethod": { ... }
}
Well, there’s no obvious way where one would put data concerning the order
(or quote
) entity. Of course, I could hack my way around this and “abuse” paymentMethod.extension_attributes
somehow. But that doesn’t feel right.
Additional Ajax requests
When looking for other solutions, I stumbled upon an article of an Magento extension developer that offers a module just doing what I try to do.
The article suggests to implement a mixin for place-order.js
as well. However, in the mixin, a custom Ajax request is issued that simply saves the value of the custom field. On the server-side, the value is stored in the quote
table. When the order is submitted, an observer
makes sure the value is copied to the order
entity.
Best way?
While the above solution works – and obviously is used by a Magento extension developer for this very purpose – isn’t there a more idiomatic and less hacky way?