only if "weekends" ticked; uses the afterHours times * (weekend collection, 9am-5pm on the server). * Weekday -> business hours first if ticked and available (before * 3pm), else after hours if ticked. * Returns null if nothing fits. * * @param array $availability Decoded /api/v1/availability response. * @param bool $bh Business hours ticked. * @param bool $ah After hours ticked. * @param bool $we Weekends ticked. * @return array|null { date, day, mode: 'bh'|'ah', time, options[] } */ public static function pick_slot( $availability, $bh, $ah, $we ) { if ( empty( $availability['days'] ) || ! is_array( $availability['days'] ) ) { return null; } foreach ( $availability['days'] as $day ) { if ( ! empty( $day['weekend'] ) ) { if ( ! $we ) { continue; } $t = $day['afterHours']['collectionTimes'][0] ?? null; if ( $t ) { return self::slot( $day, 'ah', $t ); } continue; } if ( $bh && ! empty( $day['businessHours']['available'] ) ) { $t = $day['businessHours']['deliveryTimes'][0] ?? null; if ( $t ) { return self::slot( $day, 'bh', $t ); } } if ( $ah && ! empty( $day['afterHours']['available'] ) ) { $t = $day['afterHours']['collectionTimes'][0] ?? null; if ( $t ) { return self::slot( $day, 'ah', $t ); } } } return null; } /** * Is an exact slot (date + mode + time) still offered, and does it still * allow this delivery option? Used before sending a booking, because the * merchant may send an order some time after checkout. */ public static function slot_still_valid( $availability, $date, $mode, $time, $option ) { foreach ( $availability['days'] ?? array() as $day ) { if ( ( $day['date'] ?? '' ) !== $date ) { continue; } $list = 'bh' === $mode ? ( ! empty( $day['businessHours']['available'] ) ? ( $day['businessHours']['deliveryTimes'] ?? array() ) : array() ) : ( $day['afterHours']['collectionTimes'] ?? array() ); foreach ( $list as $t ) { if ( ( $t['time'] ?? '' ) === $time && in_array( $option, $t['deliveryOptions'] ?? array(), true ) ) { return true; } } } return false; } private static function slot( $day, $mode, $t ) { // [CHANGE-2026-11-13-4] 'times' = every time offered for this day and // collection type (from the server), so the shopper can choose // "Deliver from". What breaks if removed: no time choice at checkout. $times = 'bh' === $mode ? ( $day['businessHours']['deliveryTimes'] ?? array() ) : ( $day['afterHours']['collectionTimes'] ?? array() ); return array( 'date' => $day['date'], 'day' => $day['day'] ?? '', 'mode' => $mode, 'time' => $t['time'], 'options' => $t['deliveryOptions'] ?? array(), 'times' => is_array( $times ) ? array_values( $times ) : array(), ); } /** * [CHANGE-2026-11-13-4] Applies the shopper's "Deliver from" choice to a * slot. If $chosen ("HH:MM") is one of the slot's times, the slot's time * and valid options become that time's; otherwise the slot is returned * unchanged (earliest time). Never invents a time the server didn't offer. */ public static function choose_time( $slot, $chosen ) { if ( ! $slot || '' === (string) $chosen ) { return $slot; } foreach ( $slot['times'] ?? array() as $t ) { if ( ( $t['time'] ?? '' ) === $chosen ) { $slot['time'] = $t['time']; $slot['options'] = $t['deliveryOptions'] ?? array(); return $slot; } } return $slot; } /** [CHANGE-2026-11-13-4] "19:00" -> "7:00pm". */ public static function time_label( $hhmm ) { $parts = explode( ':', (string) $hhmm ); if ( 2 !== count( $parts ) ) { return (string) $hhmm; } $h = (int) $parts[0]; $m = (int) $parts[1]; $h12 = 0 === $h % 12 ? 12 : $h % 12; return $h12 . ':' . str_pad( (string) $m, 2, '0', STR_PAD_LEFT ) . ( $h < 12 ? 'am' : 'pm' ); } /** Weight in the store's unit -> kg. */ public static function to_kg( $value, $unit ) { $v = (float) $value; switch ( $unit ) { case 'g': return $v / 1000; case 'lbs': return $v * 0.45359237; case 'oz': return $v * 0.028349523125; default: return $v; // kg } } /** Length in the store's unit -> mm. */ public static function to_mm( $value, $unit ) { $v = (float) $value; switch ( $unit ) { case 'm': return $v * 1000; case 'cm': return $v * 10; case 'in': return $v * 25.4; case 'yd': return $v * 914.4; default: return $v; // mm } } /** * Parcel figures for the API from line items. * weightKg = sum of (weight x qty); items with no weight use the * merchant's default weight. * dims = the largest single item by volume (the API prices on the * largest item, like the website); items with no dimensions * use the merchant's default size. * itemCount = total quantity. * * @param array $items Each: [ weight, length, width, height, qty ] in store units ('' = missing). * @param array $defaults [ weightKg, lengthMm, widthMm, heightMm ]. */ public static function parcel( $items, $weight_unit, $dim_unit, $defaults ) { $total_kg = 0.0; $count = 0; $best = null; $best_vol = -1; foreach ( $items as $it ) { $qty = max( 1, (int) $it['qty'] ); $count += $qty; $kg = ( '' === $it['weight'] || null === $it['weight'] || (float) $it['weight'] <= 0 ) ? (float) $defaults['weightKg'] : self::to_kg( $it['weight'], $weight_unit ); $total_kg += $kg * $qty; $has_dims = (float) $it['length'] > 0 && (float) $it['width'] > 0 && (float) $it['height'] > 0; $dims = $has_dims ? array( self::to_mm( $it['length'], $dim_unit ), self::to_mm( $it['width'], $dim_unit ), self::to_mm( $it['height'], $dim_unit ) ) : array( (float) $defaults['lengthMm'], (float) $defaults['widthMm'], (float) $defaults['heightMm'] ); $vol = $dims[0] * $dims[1] * $dims[2]; if ( $vol > $best_vol ) { $best_vol = $vol; $best = $dims; } } if ( null === $best ) { $best = array( (float) $defaults['lengthMm'], (float) $defaults['widthMm'], (float) $defaults['heightMm'] ); } return array( 'weightKg' => max( 0.1, round( $total_kg, 2 ) ), 'lengthMm' => (int) round( $best[0] ), 'widthMm' => (int) round( $best[1] ), 'heightMm' => (int) round( $best[2] ), 'itemCount' => max( 1, $count ), ); } /** * Shopper's shipping price from an AHC quote. * $tax_enabled true -> start from the ex-GST subtotal (WooCommerce adds * GST on shipping itself); * false -> start from the GST-inclusive total. * Markup is clamped to 0-20%. */ public static function shopper_cost( $quote, $tax_enabled, $mode, $markup_percent ) { $base = $tax_enabled ? (float) $quote['subtotal'] : (float) $quote['total']; $pct = 'markup' === $mode ? max( 0, min( self::MAX_MARKUP_PERCENT, (float) $markup_percent ) ) : 0; return round( $base * ( 1 + $pct / 100 ), 2 ); } /** One-line Australian address for the API. Empty string if incomplete. */ public static function address_line( $a ) { if ( empty( $a['address_1'] ) || empty( $a['city'] ) ) { return ''; } if ( ! empty( $a['country'] ) && 'AU' !== $a['country'] ) { return ''; } $street = trim( $a['address_1'] . ( empty( $a['address_2'] ) ? '' : ' ' . $a['address_2'] ) ); $tail = trim( $a['city'] . ' ' . ( $a['state'] ?? '' ) . ' ' . ( $a['postcode'] ?? '' ) ); return $street . ', ' . $tail . ', Australia'; } /** "Fri 13 Nov" from YYYY-MM-DD. */ public static function short_date( $ymd ) { $ts = strtotime( $ymd . ' 12:00:00 UTC' ); return $ts ? gmdate( 'D j M', $ts ) : $ymd; } public static function option_label( $option ) { $labels = array( 'STANDARD' => 'Standard', 'URGENT_2H' => 'Urgent', 'VITAL_90M' => 'Vital', ); return $labels[ $option ] ?? $option; } }