See my previous answer if you want to add custom fields to the end of the form. If you want to add them for each shipping class, I used the following:
function shipping_instance_form_add_extra_fields($settings)
{
$new_settings = [];
foreach($settings as $key => $setting){
$new_settings[$key] = $setting;
if(str_starts_with($key, 'class_cost_')){
$new_settings[str_replace('class_cost_', 'class_pack_', $key)] = [
'title' => str_replace('shipping class cost', 'units per package', $setting['title']),
'type' => 'number',
'description' => '',
];
}
}
return $new_settings;
}
function shipping_instance_form_fields_filters()
{
$shipping_methods = WC()->shipping->get_shipping_methods();
foreach ($shipping_methods as $shipping_method) {
add_filter('woocommerce_shipping_instance_form_fields_' . $shipping_method->id, 'shipping_instance_form_add_extra_fields');
}
}
add_action('woocommerce_init', 'shipping_instance_form_fields_filters');
You don’t need to do any saving. It will be automatically saved.
Later, if you have WC_Shipping_Method instance, you can retrieve your custom fields with the following:
$method->get_instance_option('class_pack_'.$class_id);