简体   繁体   中英

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. I'm using $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?

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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