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:29:19 | Current Revision | ||
---|---|---|---|
Content | |||
Deleted: < | Added: <p>There are many ways to get the last order ID in Magento, here is the detail:</p> | ||
Deleted: < | Added: <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> | ||
Deleted: < | Added: <p>This solution will not work in case you want to get the last order ID in the backend. | ||
Deleted: </ | Added: </p> | ||
Deleted: < | Added: <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> | ||
Deleted: < | 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') ->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> | ||
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 :)</ | 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.