客户处理订单电子邮件通知中的 Woocommerce 订单日期时间

Woocommerce order date-time in customer processing order email notification

有没有办法让准确的订单日期和时间显示在 woocommerce 电子邮件中?到目前为止,我正在使用它来获取订单日期:

<?php printf( '<time datetime="%s">%s</time>', $order->get_date_created()->format( 'c' ), wc_format_datetime( $order->get_date_created() ) ); ?>

我得到了正确的日期,但是下订单时没有时间戳。如何添加准确的时间戳?

像这样:

订单号XXXX(放置于 2018 年 2 月 25 日 10:06PM EST)

下订单时有 4 个不同的日期案例 (其中 $orderWC_order 对象的一个​​实例) 取决于订单状态、使用的支付方式和 Woocommerce 的行为:

  • 创建日期:$order->get_date_created()
  • 修改日期:$order->get_date_modified()
  • 支付日期:$order->get_date_paid()
  • 完成日期:$order->get_date_completed()

所有这 4 个不同日期的订单都是 WC_DateTime objects (instances) where you can use available WC_DateTime 方法。

要获得正确的格式,例如:
订单号XXXX(放置于 2018 年 2 月 25 日 10:06PM EST)
…您将使用以下示例:

$date_modified = $order->get_date_modified();
echo sprintf( '<p>Order NO. %s (placed on <time>%s</time>)</p>', 
    $order->get_order_number( ), 
    $date_modified->date("F j, Y, g:i:s A T")
);

If you want to use get_date_paid() or get_date_completed() methods you should need to do it carefully, testing that the WC_DateTime object exist before trying to display it…

$date_paid = $order->get_date_paid();
if( ! empty( $date_paid) ){
    echo sprintf( '<p>Order NO. %s (placed on <time>%s</time>)</p>', 
        $order->get_order_number( ), 
        $date_paid->date("F j, Y, g:i:s A T")
    );
}

由于您没有具体指定要将其显示在客户处理订单电子邮件通知的具体位置,我将为您提供一个您可以使用的挂钩函数的示例:

add_action( 'woocommerce_email_order_details', 'custom_processing_order_notification', 1, 4 );
function custom_processing_order_notification( $order, $sent_to_admin, $plain_text, $email ) {
    // Only for processing email notifications to customer
    if( ! 'customer_processing_order' == $email->id ) return;

    $date_modified = $order->get_date_modified();
    $date_paid = $order->get_date_paid();

    $date =  empty( $date_paid ) ? $date_modified : $date_paid;

    echo sprintf( '<p>Order NO. %s (placed on <time>%s</time>)</p>',
        $order->get_order_number( ),
        $date->date("F j, Y, g:i:s A T")
    );
}

代码进入您活跃的子主题(或主题)的 function.php 文件。

已测试并有效。