Display a custom delivery notice on WooCommerce orders and email notifications

Tags: , , , ,



With the following code I display a custom message on WooCommerce checkout page. When a customer places an order on a Saturday and Sunday, the order will be delivered on the next Monday. If a customer places an order between Monday and Friday after 22:00h, the delivery will take place the day after tomorrow. When a customer places an order between Monday and Friday before 22:00h, delivery will take place the next day.

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

Now, what I am trying to do is to add this message/notice to Email notifications and in backend order-view within WooCommerce. Could someone point me in the proper direction?

Answer

I have adapted your code for WooCommerce orders in a function to be used to display a custom delivery message in:

  • Admin single orders
  • Customer orders (order received and order view)
  • Email notifications

Here is that code:

// 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 );
    }
}

Code goes in functions.php file of the active child theme (or active theme). It should work.



Source: stackoverflow