You are viewing an old revision of this post, from January 24, 2018 @ 17:29:19. See below for differences between this version and the current revision.

Magento – Get the last order ID

There are many ways to get the last order ID in Magento, here is the detail:

 1. From the checkout session.

    $lastOrderId = Mage::getSingleton('checkout/session')
                   ->getLastRealOrderId();

    $orderId = Mage::getModel('sales/order')
               ->loadByIncrementId($lastOrderId)
               ->getEntityId();

This solution will not work in case you want to get the last order ID in the backend.

2. From the model.

$orders = Mage::getModel('sales/order')->getCollection()
     ->setOrder('increment_id','DESC')
     ->setPageSize(1)
     ->setCurPage(1);
$orderId = $orders->getFirstItem()->getEntityId();

This method will not work if there are multiple stores in a single Magento setup.

There is a better solution here:

$orders = Mage::getModel('sales/order')->getCollection()
     ->setOrder('created_at','DESC')
     ->setPageSize(1)
     ->setCurPage(1);

$orderId = $orders->getFirstItem()->getEntityId();

This method will work in all cases.

Hope it is useful for you 🙂

Revisions

Revision Differences

January 24, 2018 @ 17:29:19Current Revision
Content
Deleted: <pre>There are many ways to get the last order ID in Magento, here is the detail:</pre> Added: <p>There are many ways to get the last order ID in Magento, here is the detail:</p>
Deleted: <pre> 1. From the checkout session.</pre> Added: <p> 1. From the checkout session.</p>
Unchanged: <pre>  $lastOrderId = Mage::getSingleton( 'checkout/session')Unchanged: <pre>  $lastOrderId = Mage::getSingleton( 'checkout/session')
Unchanged: -&gt;getLastRealOrderId();Unchanged: -&gt;getLastRealOrderId();
Unchanged: $orderId = Mage::getModel( 'sales/order')Unchanged: $orderId = Mage::getModel( 'sales/order')
Unchanged: -&gt;loadByIncrementId( $lastOrderId)Unchanged: -&gt;loadByIncrementId( $lastOrderId)
Unchanged: -&gt;getEntityId();Unchanged: -&gt;getEntityId();
Unchanged: </pre>Unchanged: </pre>
Deleted: <pre>This solution will not work in case you want to get the last order ID in the backend. Added: <p>This solution will not work in case you want to get the last order ID in the backend.
Deleted: </pre> Added: </p>
Deleted: <pre>2. From the model.</pre> Added: <p>2. From the model.</p>
Unchanged: <pre>$orders = Mage::getModel( 'sales/order') -&gt;getCollection()Unchanged: <pre>$orders = Mage::getModel( 'sales/order') -&gt;getCollection()
Unchanged: -&gt;setOrder( 'increment_id','DESC')Unchanged: -&gt;setOrder( 'increment_id','DESC')
Unchanged: -&gt;setPageSize(1)Unchanged: -&gt;setPageSize(1)
Unchanged: -&gt;setCurPage(1);</pre>Unchanged: -&gt;setCurPage(1);</pre>
Unchanged: <pre>$orderId = $orders-&gt;getFirstItem( )-&gt;getEntityId();</pre>Unchanged: <pre>$orderId = $orders-&gt;getFirstItem( )-&gt;getEntityId();</pre>
Deleted: <pre>This method will not work if there are multiple stores in a single Magento setup. Added: <p>This method will not work if there are multiple stores in a single Magento setup.
 Added: </p>
 Added: <p>
Deleted: There is a better solution here: Added: There is a better solution here:</p>
 Added: <pre>
Unchanged: $orders = Mage::getModel( 'sales/order') -&gt;getCollection()Unchanged: $orders = Mage::getModel( 'sales/order') -&gt;getCollection()
Unchanged: -&gt;setOrder( 'created_at','DESC')Unchanged: -&gt;setOrder( 'created_at','DESC')
Unchanged: -&gt;setPageSize(1)Unchanged: -&gt;setPageSize(1)
Deleted: -&gt;setCurPage(1);</pre> Added: -&gt;setCurPage(1);
Deleted: <pre>$orderId = $orders-&gt;getFirstItem( )-&gt;getEntityId(); Added: $orderId = $orders-&gt;getFirstItem( )-&gt;getEntityId();</pre>
 Added: <p>
Unchanged: This method will work in all cases.Unchanged: This method will work in all cases.
 Added: </p>
Deleted: Hope it is useful for you :)</pre> Added: <p> Hope it is useful for you 🙂 </p>

Note: Spaces may be added to comparison text to allow better line wrapping.

No comments yet.

Leave a Reply