在 WooCommerce 订单和电子邮件通知上显示自定义送货通知

Display a custom delivery notice on WooCommerce orders and email notifications

使用以下代码,我在 WooCommerce 结帐页面上显示自定义消息。当客户在周六和周日下订单时,订单将在下周一交付。如果客户在 22:00h 后的周一至周五之间下订单,则将在后天发货。如果客户在 22:00h 之前的周一至周五之间下订单,则会在第二天发货。

function bbloomer_notice_shipping() {

 date_default_timezone_set( 'Europe/Amsterdam' );  
    
   // if SAT delivery will be MON
   if ( date( 'N' ) >= 6 ) {
      $del_day = date( "l jS F", strtotime( "next monday" ) );
      $order_by = "maandag";
   } 
    
   // if bestelling vindt plaats op MON/FRI na 22 uur delivery will be day after tomorrow
   elseif ( date( 'H' ) >= 22 ) {
      $del_day = date( "l jS F", strtotime( "tomorrow + 1day" ) );
      $order_by = "overmorgen";
   } 
    
   // if bestelling vindt plaats op MON/FRI voor 22 uur delivery will be tomorrow
   else {
      $del_day = date( "l jS F", strtotime( "tomorrow" ) );
      $order_by = "vandaag";
   }
    
    
    $chosen_shipping_method_id = WC()->session->get( 'chosen_shipping_methods' )[0];
    
    if ($chosen_shipping_method_id == 'flat_rate:1') {
        $verzend_melding = 'wordt bezorgt op '.$del_day.'';
    }
    elseif ($chosen_shipping_method_id == 'local_pickup:4') {
        $verzend_melding = 'kan worden opgehaald op '.$del_day.'';
    }
        elseif ($chosen_shipping_method_id == 'local_pickup:3') {
        $verzend_melding = 'kan worden opgehaald op '.$del_day.'';
    }
 
       echo "<tr><td colspan='2'><i>Uw bestelling $verzend_melding</i></td></tr>";  
    
}

现在,我要做的是将此 message/notice 添加到电子邮件通知和 WooCommerce 中的后端订单视图中。有人能给我指出正确的方向吗?

我已经在一个函数中调整了您的 WooCommerce 订单代码,用于在以下位置显示自定义送货消息:

  • 管理员单笔订单
  • 客户订单(收到订单和查看订单)
  • 电子邮件通知

代码如下:

// Custom function that returns a delivery notice based on order date an shipping method
function get_delivery_message_for_order( $order ){
    date_default_timezone_set( 'Europe/Amsterdam' );

    $order_date = $order->get_date_created();

    // If order is placed Saturday or Sunday delivery will be Monday
    if ( $order_date->date('N') >= 6 ) {
        $delivery_date = date_i18n( "l jS F", strtotime( "next monday" ) );
    }
    // If order is placed from monday to friday after 22h delivery will be day after tomorrow
    elseif ( $order_date->date('H') >= 22 ) {
        $delivery_date = date_i18n( "l jS F", strtotime( "tomorrow + 1day" ) );
    }
    // If order is placed from monday to friday before 22h delivery will be tomorrow
    else {
        $delivery_date = date_i18n( "l jS F", strtotime( "tomorrow" ) );
    }

    $shipping_rates_ids = array();

    foreach ( $order->get_shipping_methods() as $shipping_method ) {
        $shipping_rates_ids[] = $shipping_method->get_method_id() . ':' . $shipping_method->get_instance_id();
    }

    if ( in_array( 'flat_rate:1', $shipping_rates_ids ) ) {
        $verzend_melding = sprintf( __("wordt bezorgt op %s"), $delivery_date );
    }
    elseif ( in_array( 'local_pickup:4', $shipping_rates_ids ) ) {
        $verzend_melding = sprintf( __("kan worden opgehaald op %s"), $delivery_date );
    }
    elseif ( in_array( 'local_pickup:3', $shipping_rates_ids ) ) {
        $verzend_melding = sprintf( __("kan worden opgehaald op %s"), $delivery_date );
    }

    if ( isset($verzend_melding) ) {
        return sprintf( __("Uw bestelling %s"), $verzend_melding ) . '</em></td></tr>';
    }
}

// On Admin single order pages
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'admin_order_delivery_notice' );
function admin_order_delivery_notice( $order ) {
    $delivery_message = get_delivery_message_for_order( $order );

    if( $delivery_message ) {
        echo '<div class="delivery">
            <p>' . $delivery_message . '</p>
        </div>';
    }
}

// On email notifications
add_action( 'woocommerce_email_order_details', 'email_notification_delivery_notice', 4, 4 );
function email_notification_delivery_notice( $order, $sent_to_admin, $plain_text, $email ) {
    $delivery_message = get_delivery_message_for_order( $order );

    if( $delivery_message ) {
        echo '<table><tr><td>' . $delivery_message . '</td></tr></table>';
    }
}

// On customer order view and order received pages
add_action( 'woocommerce_order_details_before_order_table', 'customer_order_delivery_notice' );
function customer_order_delivery_notice( $order ) {
    $delivery_message = get_delivery_message_for_order( $order );

    if( $delivery_message ) {
        wc_print_notice( $delivery_message );
    }
}

代码进入活动子主题(或活动主题)的 functions.php 文件。应该可以。