I\'m trying to get the Order Increment Id in Magento, on the success.phtml page so that I can use this for affiliate tracking.
I\'m using the following code, but it
I had to use...
$_order = Mage::getModel('sales/order')->loadByIncrementId($this->getOrderId());
While in the success.phtml template. Instead of load() I used loadByIncrementId - then my order object was no longer empty.
getRealOrderId()
appears to return the order number as presented in data grids. getId()
will return the internal id of row in the database, which you probably don't want.
$shipmentID = $shipment->increment_id;
$order = $shipment->getOrder();
$orderID = $order->increment_id;
$lastOrderIncrementId = Mage::getModel("sales/order")->getCollection()->getLastItem()->getIncrementId();
Your call to
Mage::getSingleton('sales/order')
isn't returning an object. Try
var_dump(Mage::getSingleton('sales/order'));
to confirm.
I haven't dived into the checkout code recently, but I'm pretty sure that's because sales/order
will get you the order in progress. Once the order's been placed it's no longer in progress.
The "right" way to do this would be to create an observer for one of the events that Magento fires during checkout. The
checkout_onepage_controller_success_action
event should be sufficient, assuming you haven't done too much customization of the checkout process.
There's a terse explaination of how to do this on the Wiki (for a different event)
Once you get your event setup and responding, do a
$event = $observer->getEvent();
var_dump($event->getData());
to see what kind of information you have available. Chances are there's an order object in there which will let you get the ID you're after.
If you are in admin mode - try this:
$orderModel = Mage::getModel('sales/order');
$orders = $orderModel->getCollection()->setOrder('increment_id', 'DESC')->setPageSize(1)->setCurPage(1);
$orderId = $orders->getFirstItem()->getIncrementId();