Magento Order Number Issue

The first time I got an out of sequence number, we had surprise and some dismay until I figured out what was happening. It has to do with how Magento allocates Sales Order numbers.

It's entirely normal to have one out of sequence like that, be previous to the current allocated numbers and a month or more old. The secret to it is that it was a logged in customer who didn't complete the order after a certain critical stage, came back, logged in and decided to finally buy.

The quote with the allocated Sales Order number uses that number for the Sales Order number.

Now for the explanation.

The Magento order process creates a quote the first time something is added to the cart.

  • For guest customers, this quote lasts as long as their session has not timed out, at which point it exists in the database, but is not recoverable by the guest customer.
  • When a registered customer logs in, the cart quote gets assigned their customer id so that the cart lasts as long as the customer does not empty it and is retrievable by the registered customer by logging into their account.

At this point, the quote is a potential Sales Order only. It has no assigned number because the customer has not committed to paying for it.

As the customer clicks the Proceed Button to checkout, they will:

  • either be logged in previous to starting the cart
  • or if not logged in, asked if they want to register or check out as a guest.

What follows is an important bit: The customers who choose to register in the cart are treated as guest customers till the order is completed and they get to the success page, at which time their account is created and they're logged in. The quote remains a guest customer quote with the session timeout loss of cart if the order is not completed and a success page displayed.

With a credit card order, the following happens when the Place Order button is clicked.

  • The credit card information, billing address information, cart totals and order information are assembled
  • A Sales Order number is assigned for this quote (sales_flat_quote table in the reserved_order_id column)
  • The data package is submitted to the credit card gateway for authorizing/caputuring the funds to pay for the order.
  • The credit cart processor passes back:
    • either an authorization/capture of funds with the appropriate transaction information to be recorded
    • or rejection of payment with appropriate information as to why authorization/capture was denied.
  • With a successful authorization/capture, the quote is converted to a Sales Order and if this is a cart register, the customer account is created.

If the credit card transaction is declined for any customer by the credit card payment gateway, and the next customer places a successful order there will be a skip in the Sales Order number sequence due to the declined payment Sales Order being assigned a reserved Sales Order number and the following successful Sales Order being assigned the next available number.

For guest carts (guest orders and unsuccessful register in cart customers) that exceed the session timeout, this reserved Sales Order number will be lost when the session expires, leaving gaps in the Sales Order sequence.

For customers who logged in before clicking the Proceed Button, the quote is assigned a customer id, so if they attempt to place an order and find that it's declined, they can come back, log in, find the cart still has contents and place the order, sometimes much later (longest to date was four months). The quote will use the assigned reserved Sales Order number, leading to an out of sequence Sales Order number showing in your Sales Order management display.


I was facing the same issue but it was only when the server was hit with a huge amount of load. This issue occurs because the db goes into the lock state while converting quote into order. On further inspection, I found out that the issue was that it tried to write into sales_flat_order_grid table within transaction right after insert into sales_flat_order table. With concurrent queries it caused locking collisions. The real solution is to move stuff of sales_flat_order_grid out of the transaction.

The link helped me understand the issue

The patch resolved the issue for me.

You have to remove function _afterSave from the Mage_Sales_Model_Abstract and add

public function afterCommitCallback(){
    if (!$this->getForceUpdateGridRecords()) {
         $this->_getResource()->updateGridRecords($this->getId());
     }
    parent::afterCommitCallback();
}

Let me know if it solves the problem for you.