在Woocommerce中,我尝试使用以下代码获取最后的订单ID:
<?php $order = new WC_Order($post->ID);echo $order->id;//to escape # from order id $order_id = trim(str_replace('#', '', $order->get_order_number())); ?>
但是它不起作用,因为我得到一个零值。
目的是在此最后一个订单ID上加1,以获取新的可用订单ID。
感谢您的帮助。
答案 0 :(得分:0)
似乎您想获取下一个可用的 POST ID (订单ID),例如,将其保存为数据库中的新订单,其中包含一些相关的信息数据。
这绝对不是方法,您需要将其与众不同 ...现在,您可以使用以下三种方式之一:
使用WordPress 专用功能wp_insert_post()
,该功能返回帖子ID(订单ID)。
使用Woocommerce 专用函数wc_create_order()
,该函数返回WC_Order
对象。
然后从订单对象中,您可以使用$order->get_id()
获取订单ID。
使用Woocommerce 空的WC_Order
对象实例和save()
方法:
// Get an empty instance of the `WC_Order` Object
$order = new WC_Order();
// Save the order to the database
$order->save();
// Get the Order ID
$order_id = $order->get_id();
添加-获取Woocommerce中的最后一个订单ID:
要获取并显示woocommerce中的最后一个订单ID,请在这两个简单的行中使用WC_Order_Query
:
<?php
$last_order_id = wc_get_orders(array('limit' => 1, 'return' => 'ids')); // Get last Order ID (array)
echo (string) reset($last_order_id); // Displaying last order ID
?>
经测试可正常工作