如何在 Woocommerce 中获取最新的订单 ID [英] How can I get the latest order id in Woocommerce
本文介绍了如何在 Woocommerce 中获取最新的订单 ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 Woocommerce 中,我试图获取最新的订单 ID.我试过这个:
全局$post;$order_id = $post->ID;$order = new WC_Order($order_id);$order_details = $order->get_data();
但是没有用.
woocommerce 如何获取最新的订单号?
解决方案
这是一个返回最后一个订单 ID 的自定义函数:
function get_last_order_id(){全球 $wpdb;$statuses = array_keys(wc_get_order_statuses());$statuses = implode( "','", $statuses );//获取最后一个订单 ID(最大值)$results = $wpdb->get_col("SELECT MAX(ID) FROM {$wpdb->prefix}postsWHERE post_type LIKE 'shop_order'AND post_status IN ('$statuses')");返回重置($结果);}
代码位于活动子主题(活动主题或任何插件文件)的 function.php 文件中.
<小时>用法 (示例):
$latest_order_id = get_last_order_id();//最后一个订单ID$order = wc_get_order( $latest_order_id );//获取 WC_Order 对象的实例$order_details = $order->get_data();//获取数组中的订单数据//原始输出测试echo '';print_r( $order_details );echo '</pre>';
经过测试和工作.
In Woocommerce I am trying to get the latest order ID. I have tried this:
global $post;
$order_id = $post->ID;
$order = new WC_Order($order_id);
$order_details = $order->get_data();
But it didn't work.
How to get the latest order ID in woocommerce?
解决方案
Here is a custom function that will return the last order ID:
function get_last_order_id(){
global $wpdb;
$statuses = array_keys(wc_get_order_statuses());
$statuses = implode( "','", $statuses );
// Getting last Order ID (max value)
$results = $wpdb->get_col( "
SELECT MAX(ID) FROM {$wpdb->prefix}posts
WHERE post_type LIKE 'shop_order'
AND post_status IN ('$statuses')
" );
return reset($results);
}
Code goes in function.php file of your active child theme (active theme or in any plugin file).
USAGE (Example):
$latest_order_id = get_last_order_id(); // Last order ID
$order = wc_get_order( $latest_order_id ); // Get an instance of the WC_Order oject
$order_details = $order->get_data(); // Get the order data in an array
// Raw output test
echo '<pre>'; print_r( $order_details ); echo '</pre>';
Tested and work.
这篇关于如何在 Woocommerce 中获取最新的订单 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文