使用WooCommerce关闭订单注释中的订单状态更新

时间:2019-06-16 15:14:35

标签: php wordpress woocommerce hook-woocommerce wordpress-hook

我希望关闭所有在Woocommerce订单上默认使用的所有订单状态更新 ,但仍然可以使用手动注释,即我是否添加了自己的注释公开或私人注释。

是否可以通过挂钩或类似的方式实现?

这是订购单目前的样子:

enter image description here

1 个答案:

答案 0 :(得分:0)

实际上,没有任何消息特定的订单注释挂钩。相反,您可以使用woocommerce_order_status_changed操作挂钩,该挂钩在更改订单状态后立即触发,以获取订单注释的转换值。在下一步中,您应该使用函数中的woocommerce_new_order_note_data过滤器挂钩(该挂钩挂钩到woocommerce_order_status_changed操作),以在状态更改时比较和取消设置订单注释。

最终代码如下:

add_action('woocommerce_order_status_changed', 'remove_order_status_change_notes', 10, 3);
function remove_order_status_change_notes($order_id, $status_from, $status_to)
{
    $transition_note = sprintf( __( 'Order status changed from %1$s to %2$s.', 'woocommerce' ), wc_get_order_status_name($status_from), wc_get_order_status_name($status_to) );
    add_filter('woocommerce_new_order_note_data', function ($args) use ($transition_note)
    {
        if ($args['comment_content'] ===  $transition_note) {
            return [];
        } else {
            return $args;
        }
    });
}

经过测试,可以正常运行