Woocommerce 自定义订单状态更改日期

Woocommerce custom order status change date

在 Woocommerce 中,我想在更新自定义订单状态的订单状态时显示日期和时间。我正在使用 $order->get_date_modified(); 但它不适合:

$order_transporte =  wc_get_order_status_name( $order->get_status() );
if($order_transporte == "Transporte"){
    $date_modified = $order->get_date_modified();
    echo sprintf( '<tr><td>%s</td><td>Transporte</td></tr>', $date_modified->date("d/M/Y g:i:s"));
}

woocommerce 中是否有任何功能可以从特定状态中获取更新日期?

对于自定义订单状态,您可以使用 woocommerce_order_status_{$status_transition[to]} 复合操作挂钩,您可以在其中将 {$status_transition[to]} 替换为自定义状态 slug 以设置此自定义状态的更改日期,例如:

add_action( 'woocommerce_order_status_transporte', 'action_on_order_status_transporte', 10, 2 );
function action_on_order_status_transporte( $order_id, $order ) {
    // Set your default time zone (http://php.net/manual/en/timezones.php)
    date_default_timezone_set('Europe/Paris');
    $order->update_meta_data('_date_transporte', strtotime("now") );
    $order->save();
}

代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。

然后您将使用以下内容来显示您的自定义状态 date/time 更改:

if( $order->has_status('transporte') ){
    // Set your default time zone (http://php.net/manual/en/timezones.php)
    date_default_timezone_set('Europe/Paris');
    $date_transporte = date( "d/M/Y g:i:s", $order->get_meta('_date_transporte') );
    echo '<tr><td>'.$date_transporte.'</td><td>'.__("Transporte").'</td></tr>';
}