首页 > 解决方案 > 将运输方式和时间段添加到 woocommerce 电子邮件

问题描述

我想将所选的运输方式添加到我的 woocommerce“新订单”电子邮件的顶部。

我还想在此旁边添加选定的时间段(自定义字段:jckwds_timeslot)。

我知道我需要将 PHP 代码添加到 functions.php 文件中,但我不确定如何。

这是我已经用来在电子邮件顶部添加订购时间的代码:

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 at <time>%s</time>)</p>',
        $order->get_order_number( ),
        $date->date("g:i A")
    );
}

标签: phpwoocommerce

解决方案


您可以使用$order->get_shipping_method()检索订单的运输方式。您可以使用它$order->get_meta()来检索自定义字段值。

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
    $date =  empty( $date_paid ) ? $order->get_date_modified() : $order->get_date_paid();
    printf( '<p>Order NO. %s (placed at <time>%s</time>)</p>', $order->get_order_number(), $date->date("g:i A") );

    //Shipping method
    printf( '<p>Shipping method: %s</p>', $order->get_shipping_method() );

    //Time slot
    printf( '<p>Time slot: %s</p>', $order->get_meta( 'jckwds_timeslot', true ) );
}

推荐阅读