英文:
Woocommerce, get order status from order note
问题
在这段代码中,我正在返回订单备注历史中倒数第二个备注。我需要提取此订单状态的元数据键,该函数仅返回备注的文本。
英文:
function get_last_but_one_order_status($order_id) {
$order = wc_get_order($order_id);
$status_history = array();
// Get all order notes
$order_notes = wc_get_order_notes(array(
'order_id' => $order_id,
'type' => 'system_status_change',
'orderby' => 'date_created',
'order' => 'ASC',
));
// Extract status history from notes
foreach ($order_notes as $note) {
$status_history[] = $note->content;
}
$status_count = count($status_history);
if ($status_count < 2) {
return ''; // No previous status available
}
// Get the last but one status
$last_but_one_status = $status_history[$status_count - 2];
return $last_but_one_status;
}
In this code I am returning the last but one note in order notes history. I need to extract the meta key of this order status, the function returns only the text of the note
答案1
得分: 1
在评论中,您提到您需要订单状态中顶部的第二个状态。因此,我修改了您的代码,请尝试并根据需要进行修改:
function get_last_but_one_order_status($order_id) {
$order = wc_get_order($order_id);
$status_history = array();
// 获取所有类型为 'system_status_change' 的订单备注
$order_notes = wc_get_order_notes(array(
'order_id' => $order_id,
'type' => 'system_status_change',
'orderby' => 'date_created',
'order' => 'ASC',
));
// 从备注中提取状态历史记录
foreach ($order_notes as $note) {
$status_change_notes[] = $note->content;
}
$status_change_notes_count = count($status_change_notes);
if ($status_change_notes_count < 2) {
return ''; // 没有可用的上一个状态更改
}
// 获取倒数第二个状态更改备注
$last_but_one = $status_change_notes[$status_change_notes_count - 2];
// 使用正则表达式从备注中提取最终状态
if (preg_match('/to ([a-z\-]+)/i', $last_but_one, $matches)) {
$last_but_one_status = $matches[1];
return $last_but_one_status;
}
return '';
}
使用示例:
$order_id = 13;
$new_order_status = get_last_but_one_order_status($order_id);
if (!empty($new_order_status)) {
$order->update_status($new_order_status, '程序更改订单状态。');
}
如果您的订单状态具有与其对应名称不同的键(slug),那么您需要在单独的函数中获取slug,像这样:
function get_order_status_slug_from_name($status_name) {
$statuses = get_terms('shop_order_status', array('hide_empty' => 0));
foreach ($statuses as $status) {
if ($status->name == $status_name) {
return $status->slug;
}
}
return false;
}
function get_last_but_one_order_status($order_id) {
// ...
// ...
// 使用正则表达式从备注中提取状态名称
if (preg_match('/to ([a-z\-]+)/i', $last_but_one, $matches)) {
$status_name = $matches[1];
$status_slug = get_order_status_slug_from_name($status_name);
if ($status_slug) {
return $status_slug;
}
}
return '';
}
这是您提供的代码的翻译部分。
英文:
In the comment, you mentioned you need the second one from the top of order statuses. So, I modified your code, try it and modify it as u see fit:
function get_last_but_one_order_status($order_id) {
$order = wc_get_order($order_id);
$status_history = array();
// Get all order notes of type 'system_status_change'
$order_notes = wc_get_order_notes(array(
'order_id' => $order_id,
'type' => 'system_status_change',
'orderby' => 'date_created',
'order' => 'ASC',
));
// Extract status history from notes
foreach ($order_notes as $note) {
$status_change_notes[] = $note->content;
}
$status_change_notes_count = count($status_change_notes);
if ($status_change_notes_count < 2) {
return ''; // No previous status change available
}
// Get the last but one status-change note
$last_but_one = $status_change_notes[$status_change_notes_count - 2];
// Use regular expressions to extract the final status from the note
if (preg_match('/to ([a-z\-]+)/i', $last_but_one, $matches)) {
$last_but_one_status = $matches[1];
return $last_but_one_status;
}
return '';
}
Usage example:
$order_id = 13;
$new_order_status = get_last_but_one_order_status($order_id);
if (!empty($new_order_status)) {
$order->update_status($new_order_status, 'Order status changed programmatically.');
}
If your order statuses have keys (slugs) which are different from their corresponding names, then you would have to get the slug in a separate function, like so:
function get_order_status_slug_from_name($status_name) {
$statuses = get_terms('shop_order_status', array('hide_empty' => 0));
foreach ($statuses as $status) {
if ($status->name == $status_name) {
return $status->slug;
}
}
return false;
}
function get_last_but_one_order_status($order_id) {
// ...
// ...
// Use regular expressions to extract the status name from the note
if (preg_match('/to ([a-z\-]+)/i', $last_but_one, $matches)) {
$status_name = $matches[1];
$status_slug = get_order_status_slug_from_name($status_name);
if ($status_slug) {
return $status_slug;
}
}
return '';
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论