3

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?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Danny
  • 95
  • 9

1 Answers1

2

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.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Thanks for the adaptation! I can see that the notice is added to the email notifications and customer order view. i can't seem to locate them in the admin single orders.... – Danny Feb 02 '21 at 08:41
  • hmm, thats weird. I added a https://i.imgur.com/lWli9te.png screenshot as wel. There's no message being displayed under the shipping address... – Danny Feb 02 '21 at 09:24
  • I can't seem to locate the problem. Weird. Maybe you could help me out with translating the delivery date? The message is in dutch, but the date is in english. 'Uw bestelling kan worden opgehaald op Wednesday 3th February' should be 'Uw bestelling kan worden opgehaald op woensdag 3 februari'. – Danny Feb 02 '21 at 10:59
  • I was wondering if we could adapt the code a bit. What if i would show the customer a dropdown with available dates? Let's say a date range between 5-2-2020 and 15-2-2020. The customer may choose a delivery date between that range. Ofcourse, when today would be 5-2-2020, that day needs to be disabled because there's no same day delivery. If today would be 5-2-2020 and it's > 22"00 o'clock, the next day needs to be disabled as well. Only deliveries before 22"00 o'clock are suitable for next day delivery. – Danny Feb 03 '21 at 08:02
  • Yes you can try that… Now after that, the best thing should be to save the shipping message as custom order meta data when order is placed add ing to checkout form a hidden field with the shipping message. It will be a lightweight solution. – LoicTheAztec Feb 03 '21 at 08:09