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

hnk*_*nkk 3 php datetime orders email-notifications woocommerce

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

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

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

像这样的东西:

订单号.XXXX(2018年2月25日美国东部时间晚上10:06)

Loi*_*tec 8

根据订单状态,使用的付款方式以及与Woocommerce有关的行为,订单时,有4种不同的日期情况(对象$order的实例在哪里WC_order):

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

所有这4个订单的不同日期都是可以使用可用方法的WC_DateTime对象(实例)WC_DateTime.

要获得正确的格式,请执行以下操作:
订购号.XXXX(放置于2018年2月25日美国东部时间晚上10:06)
...您将使用以下示例:

$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")
);
Run Code Online (Sandbox Code Playgroud)

如果你想使用get_date_paid()或者get_date_completed()方法,你应该仔细地做,WC_DateTime在试图显示它之前测试对象是否存在...

$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")
    );
}
Run Code Online (Sandbox Code Playgroud)

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

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")
    );
}
Run Code Online (Sandbox Code Playgroud)

代码位于活动子主题(或主题)的function.php文件中.

经过测试和工作.