array( label, value ) ). */ function dayslot_order_summary_rows( $order ) { $s = dayslot_get_settings(); $rows = array(); $choice = dayslot_order_choice( $order ); if ( ! $choice['date'] ) { return array(); } $modes = dayslot_modes(); if ( count( $modes ) > 1 || 'pickup' === $choice['mode'] ) { $rows['dayslot_mode'] = array( 'label' => $s['method_label'], 'value' => isset( $modes[ $choice['mode'] ] ) ? $modes[ $choice['mode'] ] : $choice['mode'], ); } $rows['dayslot_event_date'] = array( 'label' => $s['label'], 'value' => dayslot_format_date( $choice['date'] ), ); if ( $choice['slot'] ) { $variant = 'delivery' === $choice['mode'] ? dayslot_variant_for_slot( $choice['slot'], $choice['variant'] ) : null; $value = dayslot_format_slot( $choice['slot'] ); if ( dayslot_night_next_day( $variant, $choice['slot'] ) ) { $next = dayslot_parse_date( $choice['date'] )->modify( '+1 day' ); /* translators: 1: time, 2: date of the next day */ $value = sprintf( __( '%1$s (night delivery, %2$s)', 'dayslot' ), $value, wp_date( get_option( 'date_format' ), $next->getTimestamp() ) ); } elseif ( $variant && 'night' === $variant['type'] ) { /* translators: %s: time */ $value = sprintf( __( '%s (night)', 'dayslot' ), $value ); } $rows['dayslot_time_slot'] = array( 'label' => $s['time_label'], 'value' => $value, ); } $parts = $order->get_meta( DAYSLOT_PRICE_META ); if ( is_array( $parts ) && $parts ) { $rows['dayslot_price'] = array( 'label' => __( 'Delivery price', 'dayslot' ), 'value' => dayslot_parts_text( $parts ), ); } return apply_filters( 'dayslot_order_summary_rows', $rows, $order ); } /** * "2-hour window, Sunday 50 zł + 8 km × 2 zł 16 zł = 66 zł". */ function dayslot_parts_text( $parts ) { $total = 0; $bits = array(); foreach ( (array) $parts as $p ) { if ( ! isset( $p['amount'] ) ) { continue; } $total += (float) $p['amount']; // "4 km × 2,00 zł = 8,00 zł": a label that is a multiplication gets "=" before its amount. $label = isset( $p['label'] ) ? (string) $p['label'] : ''; $sep = false !== strpos( $label, '×' ) ? ' = ' : ' '; $bits[] = ( '' !== $label ? $label . $sep : '' ) . dayslot_money( $p['amount'] ); } $text = implode( ' + ', $bits ); if ( count( $bits ) > 1 ) { $text .= ' = ' . dayslot_money( $total ); } return $text; } /** * Thank-you page, My Account and emails (customer and admin). */ add_filter( 'woocommerce_get_order_item_totals', function ( $totals, $order ) { foreach ( dayslot_order_summary_rows( $order ) as $key => $row ) { $totals[ $key ] = array( 'label' => $row['label'] . ':', 'value' => esc_html( $row['value'] ), ); } return $totals; }, 10, 2 ); /** * Notes only the shop sees in its emails (e.g. "check the distance manually"). */ add_action( 'woocommerce_email_order_meta', function ( $order, $sent_to_admin, $plain_text ) { if ( ! $sent_to_admin || ! is_a( $order, 'WC_Order' ) ) { return; } $notes = (array) apply_filters( 'dayslot_admin_email_notes', array(), $order ); foreach ( $notes as $note ) { if ( $plain_text ) { echo "\n" . esc_html( wp_strip_all_tags( $note ) ) . "\n"; } else { echo '

' . esc_html( $note ) . '

'; } } }, 10, 3 ); // -------------------------------------------------------------------------- // Admin: order screen // -------------------------------------------------------------------------- /** * '; $found = false; if ( 'delivery' === $mode ) { foreach ( dayslot_variants( true ) as $v ) { $html .= ''; foreach ( dayslot_variant_slots( $v ) as $key ) { $sel = ( $key === $current_slot && ( ! $current_variant || $current_variant === $v['id'] ) && ! $found ); if ( $sel ) { $found = true; } $html .= ''; } $html .= ''; } } $html = (string) apply_filters( 'dayslot_admin_time_options_html', $html, $current_variant, $current_slot, $mode, $found ); if ( $current_slot && false === strpos( $html, 'selected' ) ) { $html .= ''; } return $html; } add_action( 'woocommerce_admin_order_data_after_order_details', function ( $order ) { $s = dayslot_get_settings(); $choice = dayslot_order_choice( $order ); $modes = dayslot_modes(); wp_nonce_field( 'dayslot_save_order_date', 'dayslot_order_nonce' ); ?>
1 || 'pickup' === $choice['mode'] ) : ?>

delete_meta_data( DAYSLOT_META ); $order->delete_meta_data( DAYSLOT_SLOT_META ); $order->delete_meta_data( DAYSLOT_VARIANT_META ); } $order->add_order_note( sprintf( /* translators: 1: old date/time, 2: new date/time */ __( 'Delivery date changed: %1$s → %2$s', 'dayslot' ), $old['date'] ? dayslot_format_date_slot( $old['date'], $old['slot'] ) : '—', $choice['date'] ? dayslot_format_date_slot( $choice['date'], $choice['slot'] ) : '—' ) ); $order->save(); if ( $notify && $choice['date'] ) { dayslot_email_date_change( $order ); } return true; } add_action( 'woocommerce_process_shop_order_meta', function ( $order_id ) { if ( ! isset( $_POST['dayslot_order_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['dayslot_order_nonce'] ) ), 'dayslot_save_order_date' ) ) { return; } if ( ! current_user_can( 'edit_shop_orders' ) ) { return; } $order = wc_get_order( $order_id ); if ( ! $order ) { return; } $old = dayslot_order_choice( $order ); $choice = $old; $choice['date'] = isset( $_POST['dayslot_admin_date'] ) ? sanitize_text_field( wp_unslash( $_POST['dayslot_admin_date'] ) ) : $old['date']; if ( isset( $_POST['dayslot_admin_time'] ) ) { $time = explode( '|', sanitize_text_field( wp_unslash( $_POST['dayslot_admin_time'] ) ), 2 ); $choice['variant'] = 2 === count( $time ) ? sanitize_key( $time[0] ) : ''; $choice['slot'] = 2 === count( $time ) && dayslot_valid_slot_key( $time[1] ) ? $time[1] : ''; } if ( isset( $_POST['dayslot_admin_mode'] ) ) { $mode = sanitize_text_field( wp_unslash( $_POST['dayslot_admin_mode'] ) ); $choice['mode'] = 0 === strpos( $mode, 'pickup' ) ? 'pickup' : 'delivery'; $choice['location'] = 0 === strpos( $mode, 'pickup:' ) ? sanitize_key( substr( $mode, 7 ) ) : ''; } $choice = apply_filters( 'dayslot_admin_posted_choice', $choice, $order ); dayslot_admin_change_order( $order, $choice, ! empty( $_POST['dayslot_admin_notify'] ) ); }, 50 ); /** * Tell the customer their delivery date/time changed, in the store's email template. */ function dayslot_email_date_change( $order ) { $sent = dayslot_email_send( 'date_change', $order->get_billing_email(), $order ); $order->add_order_note( $sent ? __( 'Customer notified about the new delivery date.', 'dayslot' ) : __( 'Could not email the customer about the new delivery date.', 'dayslot' ) ); $order->save(); return $sent; } /** * "Delivery" column in the orders list — HPOS and legacy storage. */ function dayslot_add_order_column( $columns ) { $new = array(); $label = __( 'Delivery', 'dayslot' ); foreach ( $columns as $key => $title ) { $new[ $key ] = $title; if ( 'order_date' === $key ) { $new['dayslot_event_date'] = $label; } } if ( ! isset( $new['dayslot_event_date'] ) ) { $new['dayslot_event_date'] = $label; } return $new; } function dayslot_render_order_column( $column, $order ) { if ( 'dayslot_event_date' !== $column ) { return; } if ( ! is_a( $order, 'WC_Order' ) ) { $order = wc_get_order( $order ); } $choice = $order ? dayslot_order_choice( $order ) : null; if ( ! $choice || ! $choice['date'] ) { echo ''; return; } echo '' . esc_html( dayslot_format_date( $choice['date'] ) ) . ''; if ( $choice['slot'] ) { echo '
' . esc_html( dayslot_format_slot( $choice['slot'] ) ) . ''; } if ( 'pickup' === $choice['mode'] ) { $modes = dayslot_modes(); echo '
' . esc_html( isset( $modes['pickup'] ) ? $modes['pickup'] : 'pickup' ) . ''; } } if ( dayslot_feature_on( 'order_column' ) ) { add_filter( 'manage_woocommerce_page_wc-orders_columns', 'dayslot_add_order_column', 20 ); add_action( 'manage_woocommerce_page_wc-orders_custom_column', 'dayslot_render_order_column', 20, 2 ); add_filter( 'manage_edit-shop_order_columns', 'dayslot_add_order_column', 20 ); add_action( 'manage_shop_order_posts_custom_column', 'dayslot_render_order_column', 20, 2 ); }