this seems to have done the trick, courtesy of chatgpt:
// Count total items in the cart
$total_items = 0;
foreach ( $package['contents'] as $item_id => $values ) {
$total_items += $values['quantity'];
}
$surcharge = $this->get_rate_field_value( 'surcharge', $rate, '' );
if ( ! strlen( $surcharge ) ) {
$surcharge = $this->surcharge;
}
if ( $surcharge ) {
$surcharge_type = $this->get_rate_field_value( 'surcharge_type', $rate, 'inherit' );
if ( ! $surcharge_type || 'inherit' === $surcharge_type ) {
$surcharge_type = $this->surcharge_type;
}
if ( ! $surcharge_type ) {
$surcharge_type = 'fixed';
}
if ( 'fixed' === $surcharge_type ) {
$cost += $surcharge * $total_items; // Apply surcharge per product
} elseif ( 'percent' === $surcharge_type ) {
$cost += ( ( $cost * $surcharge ) / 100 );
}
}