WooCommerce next day order delivery

For a client, we were hired as WordPress developers. One of the problem was simple request. How to use WooCommerce to only allow next day delivery if before 1pm and day after tomorrow if ordered after 1pm.

We tried to find a switch for this in settings but couldn’t so ended up writing this sample code. You may use it in the functions.php to set WooCommerce for next day delivery.

add_action( 'woocommerce_thankyou', 'custom_woocommerce_orderlordapi' );
function custom_woocommerce_orderlordapi( $order_id ) {
 
	// Lets grab the order
	$order = wc_get_order( $order_id );
 
	/**
	 * Put your tracking code here
	 * You can get the order total etc e.g. $order->get_total();
	 */
 
	foreach ( $order->get_items() as $item_id => $order_item ) {
		//$custom_field = get_post_meta( $item_id, 'Delivery Date', true); 
 
 
		 if(wc_get_order_item_meta( $item_id, 'Delivery Date', true )){
			  $delivery_date = wc_get_order_item_meta( $item_id, 'Delivery Date', true ); 
			   if(date('D') == 'Fri' && date('H') > 13) { 
				 $modified_delivery_date = date('Y-m-d', strtotime($delivery_date . ' +3 day'));
				 wc_update_order_item_meta($item_id, 'Delivery Date', $modified_delivery_date);
			  }
			  elseif(date('D') == 'Sat') { 
				 $modified_delivery_date = date('Y-m-d', strtotime($delivery_date . ' +2 day'));
				 wc_update_order_item_meta($item_id, 'Delivery Date', $modified_delivery_date);
			  }
			  elseif (date('H') > 13) {
				 $modified_delivery_date = date('Y-m-d', strtotime($delivery_date . ' +2 day'));
				 wc_update_order_item_meta($item_id, 'Delivery Date', $modified_delivery_date);
			  }
			  else{
				 $modified_delivery_date = date('Y-m-d', strtotime($delivery_date . ' +1 day'));
				 wc_update_order_item_meta($item_id, 'Delivery Date', $modified_delivery_date);
			  }
		 }
    }
 
}

This will set the WooCommerce delivery date based on the client settings. Let us know what you think.