php 在 Magento 中获取订单增量 ID
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2195743/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get Order Increment Id in Magento
提问by doubleplusgood
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.
我正在尝试在 Magento 中的 success.phtml 页面上获取订单增量 ID,以便我可以将其用于会员跟踪。
I'm using the following code, but it is giving an error on the second line;
我正在使用以下代码,但在第二行出现错误;
$order = Mage::getSingleton('sales/order')->getLastOrderId();
$lastOrderId = $order->getIncrementId();
The error reads:
错误如下:
Fatal error: Call to a member function getIncrementId() on a non-object on line 34: $LastOrderId = $order->getIncrementId();
致命错误:在第 34 行调用非对象上的成员函数 getIncrementId():$LastOrderId = $order->getIncrementId();
I was wondering if anyone has any ideas on how to get the Order Increment Id? This is the reference number seen in the admin, usually something like: #1000123
我想知道是否有人对如何获取订单增量 ID 有任何想法?这是在管理员中看到的参考编号,通常类似于:#1000123
采纳答案by Chris Norton
If you're specifically doing this on the checkout success page - in success.phtml - then the code to get the order increment ID is already available in the template, since it is displayed to the customer.
如果您专门在结帐成功页面(在 success.phtml 中)执行此操作,则模板中已提供用于获取订单增量 ID 的代码,因为它已向客户显示。
You just need the following:
您只需要以下内容:
$orderId = $this->getOrderId();
Note that this won't work on other pages so, for those, you'd need to use:
请注意,这不适用于其他页面,因此,对于这些页面,您需要使用:
$orderId = Mage::getSingleton('checkout/session')->getLastRealOrderId();
回答by Greg
$order in your code is the last order ID...as the function name implies. If this isn't the value you want, then use it to load an order, and then use the getter on that:
代码中的 $order 是最后一个订单 ID...正如函数名称所暗示的那样。如果这不是您想要的值,则使用它来加载订单,然后在其上使用 getter:
$order = Mage::getModel('sales/order');
$order->load(Mage::getSingleton('sales/order')->getLastOrderId());
$lastOrderId = $order->getIncrementId();
回答by Chiragit007
This will work perfectly, I m running this one in my module now.
这将完美运行,我现在正在我的模块中运行这个。
$last_order_increment_id = Mage::getModel("sales/order")->getCollection()->getLastItem()->getIncrementId();
Hope it helps thanks. :)
希望有帮助谢谢。:)
回答by Alan Storm
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/orderwill get you the order in progress. Once the order's been placed it's no longer in progress.
我最近没有深入研究结帐代码,但我很确定那是因为sales/order会为您提供正在进行的订单。一旦下订单,它就不再在进行中。
The "right" way to do this would be to create an observer for one of the events that Magento fires during checkout. The
执行此操作的“正确”方法是为 Magento 在结账期间触发的事件之一创建一个观察者。这
checkout_onepage_controller_success_action
event should be sufficient, assuming you haven't done too much customization of the checkout process.
event 应该就足够了,假设您还没有对结帐流程进行太多自定义。
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.
看看你有什么样的信息可用。那里可能有一个订单对象,它可以让您获得所需的 ID。
回答by Bryce
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.
而在success.phtml 模板中。我使用 loadByIncrementId 而不是 load() - 然后我的订单对象不再是空的。
回答by Matthias Kleine
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();
$orderModel = Mage::getModel('sales/order');
$orders = $orderModel->getCollection()->setOrder('increment_id', 'DESC')->setPageSize(1)->setCurPage(1);
$orderId = $orders->getFirstItem()->getIncrementId();
回答by lucasvm1980
You can get the increment id using this code snippet:
您可以使用以下代码片段获取增量 ID:
$orderId = 12;
$order = Mage::getModel('sales/order')->load($orderId);
$Incrementid = $order->getIncrementId();
Now you can do an echo to the $Incrementid variable and see the increment id.
现在您可以对 $Incrementid 变量进行回显并查看增量 ID。
I hope this helps.
我希望这有帮助。
回答by Dominic
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.
getRealOrderId()似乎返回数据网格中显示的订单号。getId()将返回数据库中行的内部 id,您可能不想要。
回答by Naveenbos
$lastOrderIncrementId = Mage::getModel("sales/order")->getCollection()->getLastItem()->getIncrementId();
回答by devopsmagento
$shipmentID = $shipment->increment_id;
$order = $shipment->getOrder();
$orderID = $order->increment_id;

