php - 使用 php 从数据库中获取 Woocommerce 的最后一个订单 ID
问题描述
在 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。
任何帮助表示赞赏。
解决方案
似乎您想获取下一个可用的POST ID (Order Id),以将其保存为数据库中的新订单以及一些相关数据。
这绝对不是这样做的方法,您需要考虑不同...现在您可以使用以下 3 种方法之一:
使用返回帖子 ID(订单 ID)的 WordPress专用函数
wp_insert_post()
。wc_create_order()
使用返回WC_Order
Object的 Woocommerce专用函数。然后从订单对象中,您可以使用
$order->get_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
在这两个简单的行中使用 a:
<?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
?>
测试和工作
推荐阅读
- jpa - Get MAX of COUNT in JPQL Query
- r - 有人可以帮助加快我在 r 中的 Woodbury 矩阵求逆代码吗?
- linux - 如何在 linux 中查找所有 .zip 文件和文件大小
- java - 在为嵌套对象创建自定义 Spring Data JPA 查询时获取 IllegalArgumentException
- php - 如何在更改选择选项时更改 php 内容
- c# - ArgumentException:JSON 解析错误:缺少对象成员的名称,unity 2019.2.4f1
- javascript - 是否有一种更快、更简单的方法来编写具有多个 AND 条件的多个 if 语句?
- c# - 使用元数据上传单个 Sharepoint 文档
- grpc - 从未调用过基于进程混合器适配器 Validate/CreateSession/CloseSession 的 istio 会话
- wordpress - wordpress 编辑器中缺少主题标题