获取以前的订单号 WooCommerce
Get a previous order number WooCommerce
我正在尝试从指定的订单号中获取以前的订单号,例如我们有 5 个订单。
1780
1781
1782
1784
1786
现在,如果我指定订单号
wc_custom_get_previous_order_number(1784)
我应该得到1782,我已经尝试了多种解决方案。例如:
add_filter( 'woocommerce_order_number', 'custom_woocommerce_order_number', 1, 2 );
function custom_woocommerce_order_number( $oldnumber, $order ) {
$lastorderid = $order->id-1; // here is i am trying to get previous order number.
// other code...
}
您可以使用带有 2 个参数的 SQL 查询构建您自己的函数:
$order_id
为起始订单ID。
$limit
是要获取的订单 ID 的数量。
函数代码:
function get_orders_from( $order_id, $limit = 1 ){
global $wpdb;
// The SQL query
$results = $wpdb->get_col( "
SELECT ID
FROM {$wpdb->prefix}posts
WHERE post_type LIKE 'shop_order'
AND ID < $order_id
ORDER BY ID DESC
LIMIT $limit
" );
return $limit == 1 ? reset( $results ) : $results;
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
用法 (示例):
1) 要从当前 $order_id
中获取之前的订单 ID,您将在代码中使用:
$previous_order_id = get_orders_from( $order_id );
您将直接获得之前的订单ID…
2) 从订单 ID 1784 获取之前的 3 个订单 ID:
// Get the array of orders_ids
$orders_ids = get_orders_from( 1784, 3 );
// Converting and displaying a string of IDs (coma separated)
echo implode( ', ', $orders_ids );
您将获得:1780, 1781, 1782
我正在尝试从指定的订单号中获取以前的订单号,例如我们有 5 个订单。 1780 1781 1782 1784 1786 现在,如果我指定订单号
wc_custom_get_previous_order_number(1784)
我应该得到1782,我已经尝试了多种解决方案。例如:
add_filter( 'woocommerce_order_number', 'custom_woocommerce_order_number', 1, 2 );
function custom_woocommerce_order_number( $oldnumber, $order ) {
$lastorderid = $order->id-1; // here is i am trying to get previous order number.
// other code...
}
您可以使用带有 2 个参数的 SQL 查询构建您自己的函数:
$order_id
为起始订单ID。$limit
是要获取的订单 ID 的数量。
函数代码:
function get_orders_from( $order_id, $limit = 1 ){
global $wpdb;
// The SQL query
$results = $wpdb->get_col( "
SELECT ID
FROM {$wpdb->prefix}posts
WHERE post_type LIKE 'shop_order'
AND ID < $order_id
ORDER BY ID DESC
LIMIT $limit
" );
return $limit == 1 ? reset( $results ) : $results;
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
用法 (示例):
1) 要从当前 $order_id
中获取之前的订单 ID,您将在代码中使用:
$previous_order_id = get_orders_from( $order_id );
您将直接获得之前的订单ID…
2) 从订单 ID 1784 获取之前的 3 个订单 ID:
// Get the array of orders_ids
$orders_ids = get_orders_from( 1784, 3 );
// Converting and displaying a string of IDs (coma separated)
echo implode( ', ', $orders_ids );
您将获得:1780, 1781, 1782