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
- January 24, 2018 @ 17:34:53 [Current Revision] by Sharing Solution
- January 24, 2018 @ 17:34:53 by Sharing Solution
- January 24, 2018 @ 17:34:29 by Sharing Solution
- January 24, 2018 @ 17:34:04 [Autosave] by Sharing Solution
- January 24, 2018 @ 17:29:19 by Sharing Solution
Revision Differences
January 24, 2018 @ 17:34:29 | Current Revision | ||
---|---|---|---|
Content | |||
Unchanged: <p>There are many ways to get the last order ID in Magento, here is the detail:</p> | Unchanged: <p>There are many ways to get the last order ID in Magento, here is the detail:</p> | ||
Unchanged: <p> 1. From the checkout session.</p> | Unchanged: <p> 1. From the checkout session.</p> | ||
Unchanged: <pre> $lastOrderId = Mage::getSingleton( 'checkout/session') | Unchanged: <pre> $lastOrderId = Mage::getSingleton( 'checkout/session') | ||
Unchanged: ->getLastRealOrderId(); | Unchanged: ->getLastRealOrderId(); | ||
Unchanged: $orderId = Mage::getModel( 'sales/order') | Unchanged: $orderId = Mage::getModel( 'sales/order') | ||
Unchanged: ->loadByIncrementId( $lastOrderId) | Unchanged: ->loadByIncrementId( $lastOrderId) | ||
Unchanged: ->getEntityId(); | Unchanged: ->getEntityId(); | ||
Unchanged: </pre> | Unchanged: </pre> | ||
Unchanged: <p>This solution will not work in case you want to get the last order ID in the backend. | Unchanged: <p>This solution will not work in case you want to get the last order ID in the backend. | ||
Unchanged: </p> | Unchanged: </p> | ||
Unchanged: <p>2. From the model.</p> | Unchanged: <p>2. From the model.</p> | ||
Unchanged: <pre>$orders = Mage::getModel( 'sales/order') ->getCollection() | Unchanged: <pre>$orders = Mage::getModel( 'sales/order') ->getCollection() | ||
Unchanged: ->setOrder( 'increment_id','DESC') | Unchanged: ->setOrder( 'increment_id','DESC') | ||
Unchanged: ->setPageSize(1) | Unchanged: ->setPageSize(1) | ||
Unchanged: ->setCurPage(1);</pre> | Unchanged: ->setCurPage(1);</pre> | ||
Unchanged: <pre>$orderId = $orders->getFirstItem( )->getEntityId();</pre> | Unchanged: <pre>$orderId = $orders->getFirstItem( )->getEntityId();</pre> | ||
Unchanged: <p>This method will not work if there are multiple stores in a single Magento setup. | Unchanged: <p>This method will not work if there are multiple stores in a single Magento setup. | ||
Unchanged: </p> | Unchanged: </p> | ||
Unchanged: <p> | Unchanged: <p> | ||
Unchanged: There is a better solution here:</p> | Unchanged: There is a better solution here:</p> | ||
Unchanged: <pre> | Unchanged: <pre> | ||
Unchanged: $orders = Mage::getModel( 'sales/order') ->getCollection() | Unchanged: $orders = Mage::getModel( 'sales/order') ->getCollection() | ||
Unchanged: ->setOrder( 'created_at','DESC') | Unchanged: ->setOrder( 'created_at','DESC') | ||
Unchanged: ->setPageSize(1) | Unchanged: ->setPageSize(1) | ||
Deleted: ->setCurPage(1); | Added: ->setCurPage(1); | ||
Deleted: | Added: $orderId = $orders->getFirstItem( )->getEntityId();</pre> | ||
Unchanged: <p> | Unchanged: <p> | ||
Unchanged: This method will work in all cases. | Unchanged: This method will work in all cases. | ||
Unchanged: </p> | Unchanged: </p> | ||
Unchanged: <p> Hope it is useful for you 🙂 </p> | Unchanged: <p> Hope it is useful for you 🙂 </p> |
Note: Spaces may be added to comparison text to allow better line wrapping.
No comments yet.