By default order incement ids are specific to store views, but sometimes you might want to share the order number no matter on which store orders have been made. You just need to have an incremental number with no distinctions. So let’s see how Magento works out of the box and then how we can achieve our need.
Magento order increment id generation
The generation takes place when a customer clicks on the place order button:
To reserve the order id:
We can see in getReservedOrderId() that Magento first selects the corresponding sequence table, based on the entity type and the store view id (order_sequence_[store_id] in our case), in order to get the next sequence value.
Actually sequence tables just have 1 column ‘sequence_value‘ with an auto increment.
Magento will insert a null value in the sequence table to get the last inserted id.
Based on that id, the full increment id is generated according to configurations stored in the sequence_meta_profile table.
For more details about these configurations consult this great article from Classy Llama
So now that we have the increment id, it’s reserved for this quote and only this one.
If payment succeed, increment id is used for the new created order.
Else, increment id is lost and a new one will be generated when the customer will click the place order button again.
Knowing that, how to use the same order sequence value?
You got it, we just need to change the default behavior to select a specific table instead of using the order sequence table based on the current store view.
You even can decide to share sequence value between websites.
There are many ways to achieve this goal.
Personnally I decided to use sequence_order_0 as common table, so customization is pretty straight forward. Create a before plugin on getSequence() method, force store id to 0 and you’re done.
Starting from a defined order increment id
You have 2 choices:
- use the ‘start’ field from the sequence_meta_profile table
- or just insert a value with the number you want to start with, in your shared order sequence table.
How to use the same sequence value for others sales entities?
Obviously we can apply this process to others sales entities. We just need to define the table in which we will pickup the desired sequence value.
It could be:
- Invoice: invoice_sequence_0
- Shipment: shipment_sequence_0
- Credit Memo: creditmemo_sequence_0
- RMA: rma_sequence_0
Magento Cloud (Pro kit)
Notice that on Cloud environments you could observe a gap up to 3 about order sequence value. This is due to Magento Cloud configuration.
More details about this setting here.