【问题标题】:Woocommerce custom order status change dateWoocommerce 自定义订单状态更改日期
【发布时间】:2018-12-09 01:54:47
【问题描述】:

在 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 中是否有任何功能可以从特定状态中获取更新日期?

【问题讨论】:

    标签: php wordpress date woocommerce orders


    【解决方案1】:

    对于自定义订单状态,您可以使用 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 文件中。经过测试并且可以工作。

    然后您将使用以下内容显示您的自定义状态日期/时间更改:

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

    【讨论】:

      猜你喜欢
      • 2018-08-30
      • 2019-08-27
      • 2017-05-24
      • 1970-01-01
      • 2022-12-15
      • 2019-04-24
      • 1970-01-01
      • 1970-01-01
      • 2019-05-18
      相关资源
      最近更新 更多