在 Magento 中获取订单增量 ID

2021-12-19 00:00:00 php magento

我正在尝试在 Magento 中的 success.phtml 页面上获取订单增量 ID,以便我可以将其用于会员跟踪.

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 is giving an error on the second line;

$order = Mage::getSingleton('sales/order')->getLastOrderId();
$lastOrderId = $order->getIncrementId();

错误如下:

致命错误:在第 34 行调用非对象上的成员函数 getIncrementId():$LastOrderId = $order->getIncrementId();

Fatal error: Call to a member function getIncrementId() on a non-object on line 34: $LastOrderId = $order->getIncrementId();

我想知道是否有人对如何获取订单增量 ID 有任何想法?这是在管理员中看到的参考编号,通常类似于:#1000123

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

推荐答案

如果您专门在结帐成功页面上执行此操作 - 在 success.phtml 中 - 那么获取订单增量 ID 的代码已在模板中可用, 因为它是显示给客户的.

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.

您只需要以下内容:

$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();

相关文章