获取订单备注中的订单状态。

huangapple go评论82阅读模式
英文:

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 &#39;system_status_change&#39;
    $order_notes = wc_get_order_notes(array(
        &#39;order_id&#39; =&gt; $order_id,
        &#39;type&#39; =&gt; &#39;system_status_change&#39;,
        &#39;orderby&#39; =&gt; &#39;date_created&#39;,
        &#39;order&#39; =&gt; &#39;ASC&#39;,
    ));

    // Extract status history from notes
    foreach ($order_notes as $note) {
        $status_change_notes[] = $note-&gt;content;
    }

    $status_change_notes_count = count($status_change_notes);

    if ($status_change_notes_count &lt; 2) {
        return &#39;&#39;; // 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(&#39;/to ([a-z\-]+)/i&#39;, $last_but_one, $matches)) {
        $last_but_one_status = $matches[1];
        return $last_but_one_status;
    }

    return &#39;&#39;;
}

Usage example:

$order_id = 13;
$new_order_status = get_last_but_one_order_status($order_id);
if (!empty($new_order_status)) {
    $order-&gt;update_status($new_order_status, &#39;Order status changed programmatically.&#39;);
}

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(&#39;shop_order_status&#39;, array(&#39;hide_empty&#39; =&gt; 0));
    foreach ($statuses as $status) {
        if ($status-&gt;name == $status_name) {
            return $status-&gt;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(&#39;/to ([a-z\-]+)/i&#39;, $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 &#39;&#39;;
}

huangapple
  • 本文由 发表于 2023年6月29日 23:57:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76582741.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定