Magento 1.8 – How to Get Last OrderId Inside an Observer

event-observerinvoicemagento-1.8orders

I am trying to get the last order id for an order so that I can create an invoice and shipment automatically but I can't seems to load the correct orderId to perform the action…

Here is how I am trying to load the orderId Observer.php

$order = Mage::getModel('sales/order')->load($orderid);
$orderIncrementId = $order->getIncrementId();

If the orderId is not present throw exception

$order = Mage::getModel('sales/order')
            ->loadByIncrementId($orderIncrementId);    
if (!$order->getId()) {
        Mage::throwException("Order does not exist, for the Shipment process to complete");
    }

The problem is if I pass in a static orderId no exception is thrown

$orderIncrementId = '1100000023-4';

But when I try to load it through the Model I can't

Can anyone show me how to get the orderId for an order please?

Best Answer

The accepted answer didn't work for me. Digging into it turned out, following solution works for me:

Mage::getModel("sales/order")->getCollection()->getLastItem()->getIncrementId()
Related Topic