persist_field_for_customer) and * WooCommerce then recalculates totals. Stored as customer meta * '_wc_shipping/' . FIELD_ID (CheckoutFields::SHIPPING_FIELDS_PREFIX). * - WooCommerce caches shipping rates per package. The package's built-in * destination doesn't include extra fields, so 'woocommerce_cart_shipping_packages' * adds 'ahcouriers_deliver_from' to each package - otherwise changing the time * would return the old cached rates. * - 'hidden' rule (JSON schema, WooCommerce 9.9+): the field is hidden unless * the selected shipping rate is an AfterHoursCouriers rate (rate ids start * with "ahcouriers_evening"), so shoppers choosing other carriers never see it. * - Options = the server's times for the earliest day the merchant's ticks * allow (from /api/v1/availability, cached 5 minutes). No time rules here. * - Not required: if nothing is chosen, the earliest time is used (as v0.1). * * REQUIREMENTS (Andrew: must be noted): WooCommerce 9.9+ and the block * checkout. On the classic checkout the field doesn't show; shoppers get the * earliest time. Known cosmetic limit: if the shopper unticks "use same * address for billing", WooCommerce also shows the field in the billing * address; only the shipping value is used. * * WHAT BREAKS IF REMOVED: shoppers can't choose when delivery starts; every * order goes out at the earliest time. * * [CHANGE-2026-11-18-4] v0.2.13 (approved by Andrew 18 Nov 2026): out-of-date * saved time is cleared. Found 18 Nov on the test shop: a time saved on an * earlier day that is not in today's list made the block checkout refuse the * address with "Invalid afterhourscouriers/deliver-from provided." * FRAMEWORK NOTE (declared before use, checked in WooCommerce source, * 18 Nov 2026): * - StoreApi AbstractAddressSchema::validate_callback() checks a select * field's value against its options BEFORE any sanitising, so a plugin * can't repair the value during that request. * - 'woocommerce_cart_loaded_from_session' fires once the customer is loaded: * on shop page loads (WC_Cart_Session::get_cart_from_session on wp_loaded, * before the checkout block reads the saved address) and on Store API * requests (CartController::load_cart). * - WC()->customer is saved on 'shutdown' (WooCommerce::initialize_cart). * So on that hook, if the saved time is not in today's list, it is removed * from the shipping value and the billing copy (WooCommerce keeps both). The * shopper sees the placeholder and gets the earliest time (existing rule). * If today's list can't be fetched (no key / API down), nothing is cleared. * Known limit: a checkout left open across the change of day shows the error * once; a reload clears it. * WHAT BREAKS IF REMOVED: returning shoppers with an old saved time can't * check out. */ defined( 'ABSPATH' ) || exit; class AHCouriers_Deliver_From { const FIELD_ID = 'afterhourscouriers/deliver-from'; public static function init() { add_action( 'woocommerce_init', array( __CLASS__, 'register_field' ) ); add_filter( 'woocommerce_cart_shipping_packages', array( __CLASS__, 'add_to_packages' ) ); add_action( 'woocommerce_cart_loaded_from_session', array( __CLASS__, 'clear_stale_choice' ) ); // [CHANGE-2026-11-18-4] } /** * [CHANGE-2026-11-18-4] Removes a saved time that is not in today's list. * See the file header. Reads only the shopper's own saved value; no * request input, no output. */ public static function clear_stale_choice() { $choice = self::current_choice(); if ( '' === $choice ) { return; } $options = self::options(); if ( empty( $options ) ) { return; // List unknown (no key / API down) - keep the value. } foreach ( $options as $o ) { if ( $o['value'] === $choice ) { return; // Still valid. } } WC()->customer->delete_meta_data( '_wc_shipping/' . self::FIELD_ID ); WC()->customer->delete_meta_data( '_wc_billing/' . self::FIELD_ID ); } /** Options for the dropdown: the earliest slot's times, as "HH:MM" => "7:00 pm". */ public static function options() { if ( '' === AHCouriers_Settings::get( 'api_key' ) ) { return array(); } $availability = AHCouriers_API::availability(); if ( ! $availability ) { return array(); } $slot = AHCouriers_Logic::pick_slot( $availability, AHCouriers_Settings::yes( 'collect_bh' ), AHCouriers_Settings::yes( 'collect_ah' ), AHCouriers_Settings::yes( 'collect_weekends' ) ); if ( ! $slot ) { return array(); } $out = array(); foreach ( $slot['times'] as $t ) { if ( empty( $t['time'] ) || empty( $t['deliveryOptions'] ) ) { continue; } $out[] = array( 'value' => $t['time'], 'label' => ! empty( $t['label'] ) ? $t['label'] : AHCouriers_Logic::time_label( $t['time'] ), ); } return $out; } public static function register_field() { if ( ! function_exists( 'woocommerce_register_additional_checkout_field' ) ) { return; // WooCommerce too old - earliest time is used. } $options = self::options(); if ( empty( $options ) ) { return; // Nothing to offer (no key / no availability) - earliest time is used. } woocommerce_register_additional_checkout_field( array( 'id' => self::FIELD_ID, // [CHANGE-2026-11-14-9] Label renamed (Andrew 14 Nov 2026): was "Deliver from" / "Deliver from (earliest if not chosen)". // WHY: the time is a delivery time (business hours) or a pickup time (after hours / weekends). // Also shown on the order screen and order-received page. WHAT BREAKS IF REMOVED: old wording returns. 'label' => 'Delivery or pickup time', 'optionalLabel' => 'Delivery or pickup time (earliest if not chosen)', 'location' => 'address', 'type' => 'select', 'required' => false, 'options' => $options, // [CHANGE-2026-11-14-8] Placeholder (Andrew 14 Nov 2026). WHY: without it WooCommerce builds // "Select a deliver from (earliest if not chosen)" from the label. The time is the delivery time // (business hours) or the pickup time (after hours / weekends). WC 11.3 SelectFieldType accepts // 'placeholder'. WHAT BREAKS IF REMOVED: the automatic wording comes back. 'placeholder' => 'Select a delivery or pickup time', 'hidden' => array( 'type' => 'object', 'properties' => array( 'cart' => array( 'type' => 'object', 'properties' => array( 'shipping_rates' => array( 'type' => 'array', 'not' => array( 'contains' => array( 'type' => 'string', 'pattern' => '^ahcouriers_evening', ), ), ), ), ), ), ), ) ); } /** The shopper's saved choice ("HH:MM") or ''. */ public static function current_choice() { if ( ! function_exists( 'WC' ) || ! WC()->customer ) { return ''; } $value = WC()->customer->get_meta( '_wc_shipping/' . self::FIELD_ID, true ); return is_string( $value ) ? $value : ''; } /** Puts the choice into each shipping package (and so into the rate cache key). */ public static function add_to_packages( $packages ) { $choice = self::current_choice(); foreach ( $packages as $i => $package ) { $packages[ $i ]['ahcouriers_deliver_from'] = $choice; } return $packages; } }