session ) { return 0; } return absint( WC()->session->get( self::SESSION_KEY ) ); } public static function set_selected_location_id( int $location_id ): void { if ( ! function_exists( 'WC' ) || ! WC()->session ) { return; } if ( $location_id > 0 ) { WC()->session->set( self::SESSION_KEY, $location_id ); return; } self::clear_selected_location_id(); } public static function clear_selected_location_id(): void { if ( ! function_exists( 'WC' ) || ! WC()->session ) { return; } WC()->session->__unset( self::SESSION_KEY ); } /** * @param array $posted_data */ public static function sync_from_posted_data( array $posted_data ): void { $selected_shipping = self::extract_selected_shipping_method( $posted_data ); if ( ! self::is_local_pickup_method( $selected_shipping ) ) { self::clear_selected_location_id(); return; } $location_id = absint( $posted_data[ self::FIELD_NAME ] ?? 0 ); self::set_selected_location_id( $location_id ); } public static function is_local_pickup_method( string $shipping_method ): bool { return 0 === strpos( $shipping_method, 'local_pickup' ); } /** * @param array $posted_data */ public static function extract_selected_shipping_method( array $posted_data ): string { $shipping_methods = $posted_data['shipping_method'] ?? []; if ( is_array( $shipping_methods ) ) { return isset( $shipping_methods[0] ) ? (string) $shipping_methods[0] : ''; } return is_scalar( $shipping_methods ) ? (string) $shipping_methods : ''; } }