Question

I have got a strange issue with Order Number in Magento.

Recently when one order was placed on my website the Order number came was 100000350,Ideally it should have been 100000370 as my previous order numbers were 100000369 and 100000367. I have attached the screenshot below for it

Order Number Screenshot

Moreover, I have checked for the error logs but haven't found any entry of it. We are using SagePay and PayPal as the payment gateway for it.

Can anyone guide me on this?

Was it helpful?

Solution

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.

OTHER TIPS

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.

I am not sure, but this might solve your problem:

As far as I think, something might have disturbed your eav_entity_store table. It contains information about the next increment_id i.e. order_id to use. May be some module or code in your magento system might have changed it.

Open this table, and update the increment_last_id coloumn with your last order id. Be careful, it contains increment id's of others as well such as invoice, shipment etc. To be sure, just go to eav_entity_types table and see what is the entity_type_id for sales/order (column entity_model). In my magento, its 5. So now go to eav_entity_store table and just update the increment_id for row whose entity_type_id is 5. You can directly update it via phpmyadmin or you can run query like,

update eav_entity_store set increment_last_id = 'your_last_order_id' where entity_type_id = 5; 

Please note 5 is entity_type_id in my magento for order

There can be many reasons for your issue, but I think this might solve your issue.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top