如何在 Woocommerce 中获取最新的订单 ID
How can I get the latest order id in Woocommerce
在 Woocommerce 中,我正在尝试获取最新的订单 ID。我试过这个:
global $post;
$order_id = $post->ID;
$order = new WC_Order($order_id);
$order_details = $order->get_data();
但是没有用。
如何在woocommerce中获取最新的订单ID?
这是一个自定义函数,它将 return 最后一个订单 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);
}
代码进入活动子主题的 function.php 文件(活动主题或任何插件文件)。
用法 (示例):
$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>';
已测试并可以使用。
在 Woocommerce 中,我正在尝试获取最新的订单 ID。我试过这个:
global $post;
$order_id = $post->ID;
$order = new WC_Order($order_id);
$order_details = $order->get_data();
但是没有用。
如何在woocommerce中获取最新的订单ID?
这是一个自定义函数,它将 return 最后一个订单 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);
}
代码进入活动子主题的 function.php 文件(活动主题或任何插件文件)。
用法 (示例):
$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>';
已测试并可以使用。