Last active
February 26, 2026 07:34
-
-
Save pramodjodhani/dd09d6fe37597f2272b6ad159e998580 to your computer and use it in GitHub Desktop.
Add Orderable time slot and date to a given order ID.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Add Orderable time slot and date to a given order ID. | |
| * | |
| * @param int $order_id The WooCommerce Order ID. | |
| * @param string $date The Date (e.g., '2026-02-26'). | |
| * @param string $time The Time (e.g., '10:00 - 11:00' or '10:00'). | |
| * @param string $service_type The Service Type (e.g., 'delivery' or 'pickup'). Default 'delivery'. | |
| * @return bool True if successful, false otherwise. | |
| */ | |
| function orderable_add_time_slot_to_order( $order_id, $date, $time, $service_type = 'delivery' ) { | |
| $order = wc_get_order( $order_id ); | |
| if ( ! $order ) { | |
| return false; | |
| } | |
| // Save display metas | |
| $order->update_meta_data( 'orderable_order_date', sanitize_text_field( $date ) ); | |
| $order->update_meta_data( 'orderable_order_time', sanitize_text_field( $time ) ); | |
| // Build properly timezoned DateTime | |
| $date_and_time = new DateTime( 'now', wp_timezone() ); | |
| // Attempt to parse the date first | |
| $date_parts = explode( '-', $date ); | |
| if ( count( $date_parts ) === 3 ) { | |
| $date_and_time->setDate( (int) $date_parts[0], (int) $date_parts[1], (int) $date_parts[2] ); | |
| } else { | |
| $timestamp_fallback = strtotime( $date ); | |
| if ( $timestamp_fallback ) { | |
| $date_and_time->setTimestamp( $timestamp_fallback ); | |
| } | |
| } | |
| // Set time to midnight by default | |
| $date_and_time->setTime( 0, 0, 0 ); | |
| // If a specific time is provided (and it isn't "asap", try to extract HH:MM) | |
| if ( ! empty( $time ) && 'asap' !== strtolower( $time ) ) { | |
| $time_parts = explode( '-', $time ); | |
| $start_time = trim( $time_parts[0] ); | |
| $hours = 0; | |
| $minutes = 0; | |
| if ( strpos( $start_time, ':' ) !== false ) { | |
| $split = explode( ':', $start_time, 2 ); | |
| $hours = (int) $split[0]; | |
| $minutes = (int) $split[1]; | |
| } else { | |
| // Handle cases where time might be just "9" or "900" without a colon (less likely but safe fallback) | |
| $start_time = str_pad( $start_time, 4, '0', STR_PAD_LEFT ); | |
| $hours = (int) substr( $start_time, 0, 2 ); | |
| $minutes = (int) substr( $start_time, 2, 2 ); | |
| } | |
| $date_and_time->setTime( $hours, $minutes, 0 ); | |
| } | |
| $timestamp = $date_and_time->getTimestamp(); | |
| if ( $timestamp ) { | |
| // Save hidden capacity tracking metas | |
| $order->update_meta_data( '_orderable_order_timestamp', $timestamp ); | |
| $order->update_meta_data( '_orderable_service_type', strtolower( $service_type ) ); | |
| } | |
| // remove_action( 'woocommerce_before_order_object_save', array( 'Orderable_Services_Order', 'before_save_order' ), 10 ); | |
| $order->save(); | |
| return true; | |
| } | |
| // remove this code snippet after the order is updated | |
| add_action( 'init', function() { | |
| orderable_add_time_slot_to_order(179, '2026-02-26', '10:40', 'delivery'); | |
| } ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment