2

I would like to get the last order Number saved in the database of the store and increment it to get what the current order number would be before it is saved as I would like to pass the order number in my API as a reference when posting.

I currently try to get the order number this way

$quote = Mage::getModel('checkout/session')->getQuote();
$quoteData= $quote->getData();
$orders = Mage::getSingleton('checkout/session')->getLastRealOrderId();
Mage::log($orders,true);
$test =  substr($orders,0,7);
$test2 =  substr($orders,7);
$number = $test2++;

But the minute I clear the cache or restart my pc and attempt to create a new order there is no order number - I am currently testing on my local machine and using the Magento demo store to build my module.

How would I get the order number stored in the database?

Thank you.

5

You can get the latest order with following code.

$lastOrder = Mage::getModel('sales/order')->getCollection()
       ->setOrder('entity_id','DESC')
       ->setPageSize(1)
       ->getFirstItem();
echo $lastOrder->getEntityId();
echo $lastOrder->getIncrementId();
| improve this answer | |
  • can this be done from a class in the model? – Radhiyah Williams Aug 30 '18 at 7:51
  • @RadhiyahWilliams yes you can use this code anywhere you want – Addify Aug 30 '18 at 7:51
  • Thank you - i will try that now . this will give me the order number that gets displayed on the success page? correct? example :145000284. – Radhiyah Williams Aug 30 '18 at 7:53
  • @RadhiyahWilliams use this code echo $lastOrder->getIncrementId(); – Addify Aug 30 '18 at 7:56
  • @RadhiyahWilliams kindly mark it as accepted answer so it ca also help fellow community. – Addify Aug 31 '18 at 15:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.