简体   繁体   English

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

[英]Woocommerce custom order status change date

In Woocommerce, I would like to show the date and time when updating an order status for a custom order status. 在Woocommerce中,我想在将订单状态更新为自定义订单状态时显示日期和时间。 I'm using $order->get_date_modified(); 我正在使用$order->get_date_modified(); but it is not suitable: 但这不适合:

$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"));
}

Is there any function in woocommerce to pick up the update date from a specific status? woocommerce中是否有任何功能可以从特定状态获取更新日期?

For a custom order status you can use woocommerce_order_status_{$status_transition[to]} composite action hook, where you will replace {$status_transition[to]} by the custom status slug to set the date changed for this custom status like: 对于自定义订单状态,您可以使用woocommerce_order_status_{$status_transition[to]}复合操作钩,在其中您将用自定义状态段替换{$status_transition[to]}以设置此自定义状态的更改日期,例如:

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();
}

Code goes in function.php file of your active child theme (or active theme). 代码进入您的活动子主题(或活动主题)的function.php文件中。 Tested and works. 经过测试和工作。

Then you will use the following for the display of your custom status date/time change: 然后,将使用以下内容显示自定义状态日期/时间更改:

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>';
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM